l10n.js (2678B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { LocalizationHelper } = require("resource://devtools/shared/l10n.js"); 8 const helper = new LocalizationHelper( 9 "devtools/client/locales/webconsole.properties" 10 ); 11 12 let dateFormatter; 13 14 const l10n = { 15 /** 16 * Generates a formatted timestamp string for displaying in console messages. 17 * 18 * @param integer [milliseconds] 19 * Optional, allows you to specify the timestamp in milliseconds since 20 * the UNIX epoch. If not provided, the current timestamp will be used. 21 * @return string 22 * The timestamp formatted for display. 23 */ 24 timestampString(milliseconds) { 25 const d = new Date(milliseconds ? milliseconds : null); 26 const hours = d.getHours(); 27 const minutes = d.getMinutes(); 28 const seconds = d.getSeconds(); 29 milliseconds = d.getMilliseconds(); 30 const parameters = [hours, minutes, seconds, milliseconds]; 31 return l10n.getFormatStr("timestampFormat", parameters); 32 }, 33 34 /** 35 * Generates a formatted date string for displaying in console messages. 36 * 37 * @param integer [milliseconds] 38 * Optional, allows you to specify the timestamp in milliseconds since 39 * the UNIX epoch from which the date will be derived. If not provided, 40 * the current timestamp will be used. 41 * @returns string 42 * The date formatted for display. 43 */ 44 dateString(milliseconds) { 45 if (!dateFormatter) { 46 dateFormatter = new Intl.DateTimeFormat(); 47 } 48 return dateFormatter.format(milliseconds || Date.now()); 49 }, 50 51 /** 52 * Retrieve a localized string. 53 * 54 * @param string name 55 * The string name you want from the Web Console string bundle. 56 * @return string 57 * The localized string. 58 */ 59 getStr(name) { 60 try { 61 return helper.getStr(name); 62 } catch (ex) { 63 console.error("Failed to get string: " + name); 64 throw ex; 65 } 66 }, 67 68 /** 69 * Retrieve a localized string formatted with values coming from the given 70 * array. 71 * 72 * @param string name 73 * The string name you want from the Web Console string bundle. 74 * @param array array 75 * The array of values you want in the formatted string. 76 * @return string 77 * The formatted local string. 78 */ 79 getFormatStr(name, array) { 80 try { 81 return helper.getFormatStr(name, ...array); 82 } catch (ex) { 83 console.error("Failed to format string: " + name); 84 throw ex; 85 } 86 }, 87 }; 88 89 module.exports = l10n;