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