]> git.gir.st - VimFx.git/blob - extension/packages/l10n.coffee
Reorganized code, removed memory leaks
[VimFx.git] / extension / packages / l10n.coffee
1 { classes: Cc, interfaces: Ci } = Components
2
3 PROPERTIES_FILE = 'vimfx.properties'
4 DEFAULT_LOCALE = 'en-US'
5
6 utils = require 'utils'
7 { unload } = require 'unload'
8 { getFirefoxPref } = require 'prefs'
9
10 # Generates the underscore function
11 l10n = do ->
12 splitter = /(\w+)-\w+/
13
14 # Current locale
15 locale = getFirefoxPref('general.useragent.locale')
16
17 getStr = (aStrBundle, aKey) ->
18 try return aStrBundle.GetStringFromName(aKey)
19
20 filePath = (locale) ->
21 utils.getResourceURI("locale/#{ locale }/#{ PROPERTIES_FILE }").spec
22
23 # Folder in the format `en-US`, e.g. locale/en-US/vimfx.properties
24 defaultBundle = Services.strings.createBundle(filePath(locale))
25
26 if locale_base = locale.match(splitter)
27 # Folder in the basic format: `en`, e.g. locale/en/vimfx.properties
28 defaultBasicBundle = Services.strings.createBundle(filePath(locale_base[1]))
29
30 # Folder named after extension default locale
31 addonsDefaultBundle = Services.strings.createBundle(filePath(DEFAULT_LOCALE))
32
33 # The underscore function
34 l10n_underscore = (aKey, aLocale) ->
35 localeBundle = null
36 localeBasicBundle = null
37
38 # Yet another way to specify a folder: both `en` or `en-US` are possible here
39 if aLocale
40 localeBundle = Services.strings.createBundle(filePath(aLocale))
41
42 # And locale version without the region, e.g. `en`
43 if locale_base = aLocale.match(splitter)
44 localeBasicBundle = Services.strings.createBundle(filePath(locale_base[1]))
45
46 aVal = getStr(localeBundle, aKey) \
47 or getStr(localeBasicBundle, aKey) \
48 or (defaultBundle && (getStr(defaultBundle, aKey) or (defaultBundle = null))) \
49 or (defaultBasicBundle && (getStr(defaultBasicBundle, aKey) or (defaultBasicBundle = null))) \
50 or getStr(addonsDefaultBundle, aKey)
51
52 return aVal
53
54 unload(Services.strings.flushBundles)
55
56 return l10n_underscore
57
58 exports._ = l10n
Imprint / Impressum