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