-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n_easy_client.coffee
101 lines (80 loc) · 2.92 KB
/
i18n_easy_client.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
ironRouterPackage = 'iron:router'
class I18nClient extends I18nBase
_checkCallbacks = (options = {})->
OptionalFunction = Match.Optional(
Match.Where (val)-> typeof val is 'function'
)
check options.translationsReady, OptionalFunction
check options.languagesReady, OptionalFunction
check options.sectionsReady, OptionalFunction
_checkSections = (options = {})->
if options.sections
check options.sections, [String]
options.sections
else
[Package[ironRouterPackage].Router.current().route.getName()] if Package[ironRouterPackage] and Package[ironRouterPackage].Router.current()
subscribe: (options)->
defaultLanguage = options?.default
check defaultLanguage, Match.Optional(String)
@setDefault defaultLanguage if defaultLanguage?.length
do @defaultSubscribe
defaultSubscribe: (options)->
check @getDefault(), String
_checkCallbacks options
sections = _checkSections options
[
Meteor.subscribe(
I18nBase.TRANSLATION_PUBLICATION
{default: @getDefault(), actual: @getLanguage(), sections: sections}
options?.translationsReady
)
Meteor.subscribe(
I18nBase.LANGUAGES_PUBLICATION
options?.languagesReady
)
]
subscribeForTranslation: (options)->
_checkCallbacks options
sections = _checkSections options
subscriptions = @defaultSubscribe options
subscriptions.push(
Meteor.subscribe(
I18nBase.TRANSLATION_PUBLICATION
{default: @getDefault(), actual: @getDefault(), sections: sections}
options?.translationsReady
)
Meteor.subscribe(
I18nBase.SECTIONS_PUBLICATION
options?.sectionsReady
)
)
subscriptions
mapAll: -> Meteor._debug 'mapAll client simulation (no effect – usefull for remote method call)'
#==================================
I18nEasy = new I18nClient()
UI.registerHelper('i18n', (key, options)-> I18nEasy.i18n key, options.hash)
UI.registerHelper('i18ns', (key, options)-> I18nEasy.i18ns key, options.hash)
UI.registerHelper('translate', I18nEasy.translate)
UI.registerHelper('translatePlural', I18nEasy.translatePlural)
UI.registerHelper('i18nDefault', (key, options)-> I18nEasy.i18nDefault key, options.hash)
UI.registerHelper('ghost', -> ghostSuffix: '-ghost')
UI.registerHelper(
'pathToLanguage'
(language)->
if Package[ironRouterPackage]
try
parameters = language: language
if Package[ironRouterPackage].Router.current()?.params.section
parameters.section = Package[ironRouterPackage].Router.current().params.section
Package[ironRouterPackage].Router.current()?.route.path(parameters)
catch error
Meteor._debug """
Warning: #{error.message}
|_route: #{Package[ironRouterPackage].Router.current()?.route.getName()}
|_path: #{Package[ironRouterPackage].Router.current()?.path}
|_language: #{language}
"""
"/#{language}"
else
Meteor._debug 'To benefit from the "pathToLanguage" helper you need to install "iron:router" package (http://atmospherejs.com/iron/router)'
)