-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtap_i18n_db-common.coffee
253 lines (193 loc) · 7.26 KB
/
tap_i18n_db-common.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
TAPi18n.Collection = (name, options={}) ->
# Set the transform option
if Meteor.isClient
original_transform = options.transform or (doc) -> doc
options.transform = (doc) ->
share.i18nCollectionTransform(original_transform(doc), collection)
collection = share.i18nCollectionExtensions(commonCollectionExtensions(new Meteor.Collection(name, options)))
if Meteor.isClient
if Package["yogiben:admin"]?
collection._disableTransformationOnRoute(/^\/admin(\/?$|\/)/)
collection._base_language = if "base_language" of options then options["base_language"] else globals.fallback_language
return collection
share.helpers = {}
share.helpers.dialectOf = (lang) ->
if lang? and "-" in lang
return lang.replace(/-.*/, "")
return null
share.helpers.removeTrailingUndefs = (arr) ->
while (not _.isEmpty arr) and (_.isUndefined _.last arr)
arr.pop()
return arr
removeTrailingUndefs = share.helpers.removeTrailingUndefs
commonCollectionExtensions = (obj) ->
reportError = (error, attempted_operation, callback) ->
if _.isFunction callback
Meteor.setTimeout (-> callback(error, false)), 0
else
console.log "#{attempted_operation} failed: #{error.reason}"
return error
throwError = (error, attempted_operation, callback) ->
throw reportError(error, attempted_operation, callback)
verifyI18nEnabled = (attempted_operation, callback) ->
if TAPi18n._enabled()
return
throwError new Meteor.Error(400, "TAPi18n is not supported"), attempted_operation, callback
isSupportedLanguage = (lang, attempted_operation, callback) ->
if lang in TAPi18n.conf.supported_languages
return
throwError new Meteor.Error(400, "Not supported language: #{lang}"), attempted_operation, callback
getLanguageOrEnvLanguage = (language_tag, attempted_operation, callback) ->
# if no language_tag & isClient, try to get env lang
if Meteor.isClient
if not language_tag?
language_tag = TAPi18n.getLanguage()
if language_tag?
return language_tag
throwError new Meteor.Error(400, "Missing language_tag"), attempted_operation, callback
obj.insertTranslations = (doc, translations, callback) ->
try
verifyI18nEnabled("insert", callback)
catch
return null
doc = _.extend {}, doc
translations = _.extend {}, translations
if translations?
for lang of translations
# make sure all languages in translations are supported
try
isSupportedLanguage lang, "insert", callback
catch
return null
# merge base language's fields with regular fields
if lang == @._base_language
doc = _.extend doc, translations[lang]
delete translations[lang]
if not _.isEmpty translations
doc = _.extend doc, {i18n: translations}
@.insert.apply @, removeTrailingUndefs([doc, callback])
obj.updateTranslations = (selector, translations, options, callback) ->
if _.isFunction options
callback = options
options = undefined
try
verifyI18nEnabled("update", callback)
catch
return null
updates = {}
if translations?
for lang of translations
# make sure all languages in translations are supported
try
isSupportedLanguage lang, "update", callback
catch
return null
# treat base language's fields as regular fields
if lang == @._base_language
_.extend updates, translations[lang]
else
_.extend updates, _.object(_.map(translations[lang], ((val, field) -> ["i18n.#{lang}.#{field}", val])))
@.update.apply @, removeTrailingUndefs([selector, {$set: updates}, options, callback])
obj.removeTranslations = (selector, fields, options, callback) ->
if _.isFunction options
callback = options
options = undefined
try
verifyI18nEnabled("remove translations", callback)
catch
return null
if not fields?
reportError new Meteor.Error(400, "Missing arugment: fields"), "remove translations", callback
return null
if not _.isArray fields
reportError new Meteor.Error(400, "fields argument should be an array"), "remove translations", callback
return null
updates = {}
for field in fields
lang = _.first field.split(".")
# make sure all languages are supported
try
isSupportedLanguage lang, "remove translations", callback
catch
return null
# treat base language's fields as regular fields
if lang == @._base_language
field = field.replace("#{lang}.", "")
if field == @._base_language
reportError new Meteor.Error(400, "Complete removal of collection's base language from a document is not permitted"), "remove translations", callback
return null
updates[field] = ""
else
updates["i18n.#{field}"] = ""
@.update.apply @, removeTrailingUndefs([selector, {$unset: updates}, options, callback])
obj.insertLanguage = (doc, translations, language_tag, callback) ->
try
verifyI18nEnabled("insert", callback)
catch
return null
# in case language_tag omitted
if _.isFunction language_tag
callback = language_tag
language_tag = undefined
try
language_tag = getLanguageOrEnvLanguage language_tag, "insert", callback
catch
return null
_translations = {}
_translations[language_tag] = translations
@.insertTranslations(doc, _translations, callback)
obj.updateLanguage = (selector, translations) ->
try
verifyI18nEnabled("update", callback)
catch
return null
language_tag = options = callback = undefined
args = _.toArray arguments
for arg in args.slice(2)
if _.isFunction arg
callback = arg
break
else if _.isObject arg
options = arg
else if _.isUndefined(options) and _.isString(arg)
# language_tag can't come after options
language_tag = arg
try
language_tag = getLanguageOrEnvLanguage language_tag, "update", callback
catch
return null
_translations = {}
_translations[language_tag] = translations
@updateTranslations(selector, _translations, options, callback)
# Alias
obj.translate = obj.updateLanguage
obj.removeLanguage = (selector, fields) ->
try
verifyI18nEnabled("remove translations", callback)
catch
return null
language_tag = options = callback = undefined
args = _.toArray arguments
for arg in args.slice(2)
if _.isFunction arg
callback = arg
break
else if _.isObject arg
options = arg
else if _.isUndefined(options) and _.isString(arg)
# language_tag can't come after options
language_tag = arg
try
language_tag = getLanguageOrEnvLanguage language_tag, "remove", callback
catch
return null
if fields isnt null and not _.isArray fields
reportError new Meteor.Error(400, "fields argument should be an array"), "remove translations", callback
return null
if fields is null
# remove entire language
_fields_to_remove = ["#{language_tag}"]
else
_fields_to_remove = _.map fields, (field) -> "#{language_tag}.#{field}"
@removeTranslations(selector, _fields_to_remove, options, callback)
return obj