]> git.gir.st - VimFx.git/blob - extension/packages/l10n.coffee
Remove chrome.coffee
[VimFx.git] / extension / packages / l10n.coffee
1 { classes: Cc, interfaces: Ci } = Components
2
3 # Generates the underscore function
4 l10n = do ->
5 splitter = /(\w+)-\w+/
6
7 # Current locale
8 locale = Cc["@mozilla.org/chrome/chrome-registry;1"]
9 .getService(Ci.nsIXULChromeRegistry).getSelectedLocale("global")
10
11 getStr = (aStrBundle, aKey) ->
12 try return aStrBundle.GetStringFromName(aKey)
13
14 return (filename, defaultLocale="en-US") ->
15
16 filePath = (locale) ->
17 getResourceURI("locale/#{ locale }/#{ filename }").spec
18
19 # Folder in the format `en-US`
20 defaultBundle = Services.strings.createBundle filePath(locale)
21
22 if (locale_base = locale.match(splitter))
23 # Folder in the basic format: `en`
24 defaultBasicBundle = Services.strings.createBundle filePath(locale_base[1])
25
26 # Folder named after `defaultLocale`
27 addonsDefaultBundle = Services.strings.createBundle filePath(defaultLocale)
28
29 # The underscore function
30 l10n_underscore = (aKey, aLocale) ->
31 localeBundle = null
32 localeBasicBundle = null
33
34 # Yet another way to specify a folder
35 if aLocale
36 localeBundle = Services.strings.createBundle filePath(aLocale)
37
38 if locale_base = aLocale.match(splitter)
39 localeBasicBundle = Services.strings.createBundle filePath(locale_base[1])
40
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)
46
47 return aVal
48
49 unload Services.strings.flushBundles
50
51 return l10n_underscore
52
53 exports.l10n = l10n
Imprint / Impressum