1 { classes: Cc, interfaces: Ci } = Components
3 # Generates the underscore function
8 locale = Cc["@mozilla.org/chrome/chrome-registry;1"]
9 .getService(Ci.nsIXULChromeRegistry).getSelectedLocale("global")
11 getStr = (aStrBundle, aKey) ->
12 try return aStrBundle.GetStringFromName(aKey)
14 return (filename, defaultLocale="en-US") ->
16 filePath = (locale) ->
17 getResourceURI("locale/#{ locale }/#{ filename }").spec
19 # Folder in the format `en-US`
20 defaultBundle = Services.strings.createBundle filePath(locale)
22 if (locale_base = locale.match(splitter))
23 # Folder in the basic format: `en`
24 defaultBasicBundle = Services.strings.createBundle filePath(locale_base[1])
26 # Folder named after `defaultLocale`
27 addonsDefaultBundle = Services.strings.createBundle filePath(defaultLocale)
29 # The underscore function
30 l10n_underscore = (aKey, aLocale) ->
32 localeBasicBundle = null
34 # Yet another way to specify a folder
36 localeBundle = Services.strings.createBundle filePath(aLocale)
38 if locale_base = aLocale.match(splitter)
39 localeBasicBundle = Services.strings.createBundle filePath(locale_base[1])
41 aVal = getStr(localeBundle, aKey) \
42 or getStr(localeBasicBundle, aKey) \
43 or (defaultBundle && (getStr(defaultBundle, aKey) or (defaultBundle = null))) \
44 or (defaultBasicBundle && (getStr(defaultBasicBundle, aKey) or (defaultBasicBundle = null))) \
45 or getStr(addonsDefaultBundle, aKey)
49 unload Services.strings.flushBundles
51 return l10n_underscore