-
Notifications
You must be signed in to change notification settings - Fork 24
/
i18n-bundle.coffee
61 lines (45 loc) 路 1.53 KB
/
i18n-bundle.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# (C) Uri Wilensky. https://github.com/NetLogo/Tortoise
EN_US = require('./en_us')
# Manually importing these is kind-of a pain, but it's easy enough to keep updated and I don't feel like wrestling with
# getting the dyanmic `require()` working with Tortoise's packaging. *TODO*: Once we've switched over to ES modules
# would be a good time to revisit this. -Jeremy B March 2023
ZH_CN = require('./zh_cn')
ES_ES = require('./es_es')
JA_JP = require('./ja_jp')
PT_PT = require('./pt_pt')
BUNDLES = {
'en_us': EN_US
, 'zh_cn': ZH_CN
, 'es_es' : ES_ES
, 'ja_jp' : JA_JP
, 'pt_pt' : PT_PT
}
{ exceptionFactory: exceptions } = require('util/exception')
# At the moment this doesn't do much but it'd be a good place to add
# the ability to swap the current locale as needed.
# -Jeremy B November 2020
class I18nBundle
_current = null
_warnings = new Set()
constructor: () ->
@_current = EN_US
get: (key, args...) ->
bundle = if @_current.hasOwnProperty(key)
@_current
else if @_current isnt EN_US and EN_US.hasOwnProperty(key)
EN_US
else
throw exceptions.internal("Could not find a message for this key: #{key}")
message = bundle[key]
message(args...)
supports: (locale) ->
BUNDLES.hasOwnProperty(locale)
switch: (locale) ->
if @supports(locale)
@_current = BUNDLES[locale]
else
if not @_warnings.has(locale)
@_warning.add(locale)
console.warn("Unsupported locale '#{locale}', reverting to 'en_us'.")
@_current = EN_US
module.exports = I18nBundle