rosetta 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ # Use this hook to configure the repository and other Rosetta options.
2
+ Rosetta.setup do |config|
3
+ # Uncomment the following lines to use the OneSky repository.
4
+ # config.repository = Rosetta::Repositories::Onesky.new
5
+ # config.repository.project_id = ENV['ONESKY_PROJECT_ID']
6
+ # config.repository.subdomain = ENV['ONESKY_SUBDOMAIN']
7
+
8
+ # Uncomment the following lines to use the Local repository.
9
+ # config.repository = Rosetta::Repositories::Local.new
10
+ end
data/lib/rosetta.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'i18n'
2
+ require 'request_store'
3
+
4
+ require 'rosetta/config'
5
+ require 'rosetta/engine'
6
+ require 'rosetta/i18n_with_stored_phrases'
7
+ require 'rosetta/phrase'
8
+ require 'rosetta/repository'
9
+ require 'rosetta/controller_additions'
10
+
11
+ require 'rosetta/repositories/local'
12
+ require 'rosetta/repositories/onesky'
13
+
14
+ module Rosetta
15
+ class << self
16
+
17
+ delegate :repository, :setup, to: :config
18
+
19
+ def config
20
+ @config ||= Config.new
21
+ end
22
+
23
+ def add_phrase(**args)
24
+ fail ArgumentError, 'missing keys argument' if args[:keys].blank?
25
+ fail ArgumentError, 'missing phrase argument' if args[:phrase].blank?
26
+
27
+ repository_link = config.repository.build_link(args[:keys], locale: locale)
28
+ phrases << Phrase.new(args[:keys], args[:phrase], repository_link)
29
+ end
30
+
31
+ def phrases
32
+ request[:used_phrases] ||= []
33
+ end
34
+
35
+ def locale
36
+ I18n.locale
37
+ end
38
+
39
+ def enabled?
40
+ enabled
41
+ end
42
+
43
+ def enable
44
+ self.enabled = true
45
+ end
46
+
47
+ def disable
48
+ self.enabled = false
49
+ end
50
+
51
+ private
52
+
53
+ def enabled
54
+ request[:enabled].nil? ? false : request[:enabled]
55
+ end
56
+
57
+ def enabled=(value)
58
+ request[:enabled] = value
59
+ end
60
+
61
+ def request
62
+ RequestStore.store[:rosetta] ||= {}
63
+ end
64
+
65
+ end
66
+ end
67
+
68
+ module I18n
69
+ class << self
70
+
71
+ prepend Rosetta::I18nWithStoredPhrases
72
+
73
+ end
74
+ end
@@ -0,0 +1,19 @@
1
+ module Rosetta
2
+ class Config
3
+
4
+ attr_accessor :repository
5
+
6
+ def initialize
7
+ set_defaults!
8
+ end
9
+
10
+ def set_defaults!
11
+ @repository = Rosetta::Repositories::Local.new
12
+ end
13
+
14
+ def setup
15
+ yield self
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ module Rosetta
2
+ module ControllerAdditions
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :rosetta?
7
+ helper_method :rosetta_menu
8
+ end
9
+
10
+ def enable_rosetta
11
+ Rosetta.enable
12
+ end
13
+
14
+ def rosetta?
15
+ Rosetta.enabled?
16
+ end
17
+
18
+ def rosetta_menu
19
+ 'rosetta/translate_menu'
20
+ end
21
+ end
22
+ end
23
+
24
+ ActiveSupport.on_load(:action_controller) do
25
+ include Rosetta::ControllerAdditions
26
+ end
@@ -0,0 +1,12 @@
1
+ module Rosetta
2
+ class Engine < ::Rails::Engine
3
+
4
+ isolate_namespace Rosetta
5
+
6
+ initializer :assets do |_config|
7
+ Rails.application.config.assets.precompile += %w[rosetta/language_icon.svg
8
+ rosetta/close.svg]
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Rosetta
2
+ module I18nWithStoredPhrases
3
+ def translate(*args)
4
+ output = super
5
+ return output unless Rosetta.enabled?
6
+
7
+ keys = normalize_keys('', args.dig(0), args.dig(1, :scope))
8
+ code = keys.join('.')
9
+
10
+ Rosetta.add_phrase(keys: keys, phrase: output) if exists?(code, config.locale)
11
+
12
+ output
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module Rosetta
2
+ class Phrase
3
+
4
+ attr_reader :keys, :text, :repository_link
5
+
6
+ def initialize(keys, text, repository_link)
7
+ @keys = keys
8
+ @text = text
9
+ @repository_link = repository_link
10
+ end
11
+
12
+ def code
13
+ @keys.join('.')
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ module Rosetta
2
+ module Repositories
3
+ class Local < Rosetta::Repository
4
+
5
+ def id
6
+ 'local'
7
+ end
8
+
9
+ def label
10
+ 'Repository Local'
11
+ end
12
+
13
+ def build_link(keys, **_args)
14
+ path = I18n.load_path.flatten.find { |filename| file_has_phrase?(filename, keys) }
15
+ path && Pathname.new(path).relative_path_from(Rails.root)
16
+ end
17
+
18
+ private
19
+
20
+ def file_has_phrase?(filename, keys)
21
+ type = File.extname(filename).tr('.', '').downcase
22
+ return unless type == 'yml'
23
+
24
+ unless file_translations(filename).is_a?(Hash)
25
+ fail I18n::InvalidLocaleData.new(filename,
26
+ 'expects it to return a hash, but does not')
27
+ end
28
+
29
+ file_translations(filename).dig(I18n.config.locale.to_s, *keys.map(&:to_s))
30
+ end
31
+
32
+ # rubocop:disable Style/Send
33
+ def file_translations(filename)
34
+ translations[filename] ||= I18n.backend.send(:load_yml, filename)
35
+ end
36
+ # rubocop:enable Style/Send
37
+
38
+ def translations
39
+ @translations ||= {}
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,39 @@
1
+ module Rosetta
2
+ module Repositories
3
+ class Onesky < Rosetta::Repository
4
+
5
+ attr_accessor :project_id, :subdomain
6
+
7
+ def label
8
+ 'Onesky'
9
+ end
10
+
11
+ def build_link(keys, locale: nil)
12
+ phrase_url(keys, locale: locale.to_s)
13
+ end
14
+
15
+ def phrase_url(keys, locale: nil)
16
+ code = keys.join('.')
17
+ "#{base_url}/#{project_id}/language/#{lang_id(locale)}#/?keyword=#{code}"
18
+ end
19
+
20
+ private
21
+
22
+ DEFAULT_LANG_ID = '1'.freeze
23
+ LANGUAGE_FILE_PATH = 'lib/rosetta/repositories/onesky/languages_ids.yml'.freeze
24
+
25
+ def base_url
26
+ "https://#{subdomain}.oneskyapp.com/collaboration/translate/project/project"
27
+ end
28
+
29
+ def lang_id(lang_iso_code = nil)
30
+ languages[lang_iso_code || I18n.locale] || DEFAULT_LANG_ID
31
+ end
32
+
33
+ def languages
34
+ YAML.safe_load(File.read(File.expand_path(LANGUAGE_FILE_PATH, Engine.root)))
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,487 @@
1
+ ---
2
+ ab: 119
3
+ aa: 120
4
+ af: 60
5
+ af-NA: 224
6
+ af-ZA: 538
7
+ agq: 225
8
+ ak: 121
9
+ sq: 63
10
+ am: 116
11
+ ar: 28
12
+ ar-DZ: 228
13
+ ar-BH: 229
14
+ ar-EG: 230
15
+ ar-IQ: 231
16
+ ar-JO: 232
17
+ ar-KW: 233
18
+ ar-LB: 234
19
+ ar-LY: 235
20
+ ar-MA: 236
21
+ ar-OM: 237
22
+ ar-QA: 238
23
+ ar-SA: 239
24
+ ar-SD: 240
25
+ ar-SY: 241
26
+ ar-TN: 242
27
+ ar-AE: 243
28
+ ar-001: 244
29
+ ar-YE: 245
30
+ an: 122
31
+ hy: 62
32
+ as: 123
33
+ asa: 247
34
+ av: 124
35
+ ae: 125
36
+ ay: 126
37
+ az: 249
38
+ az-AZ: 64
39
+ az-Cyrl-AZ: 251
40
+ az-Cyrl: 250
41
+ ksf: 252
42
+ bm: 127
43
+ bas: 255
44
+ ba: 128
45
+ eu-ES: 39
46
+ be-BY: 66
47
+ bem: 259
48
+ bez: 261
49
+ bn: 263
50
+ bn-BD: 264
51
+ bn-IN: 65
52
+ bh: 129
53
+ bi: 130
54
+ brx: 265
55
+ bs-BA: 61
56
+ br: 131
57
+ bg: 576
58
+ bg-BG: 37
59
+ my: 132
60
+ ca: 23
61
+ ceb: 584
62
+ tzm: 271
63
+ ch: 133
64
+ ce: 134
65
+ chr: 273
66
+ ny: 135
67
+ cgg: 275
68
+ zh-CN: 3
69
+ zh-Hans-CN: 278
70
+ zh-Hans-HK: 279
71
+ zh-Hans-MO: 281
72
+ zh-SG: 84
73
+ zh-TW: 2
74
+ zh-HK: 78
75
+ zh-Hant-MO: 280
76
+ zh-Hant-TW: 282
77
+ cu: 184
78
+ cv-CV: 98
79
+ swc: 283
80
+ kw: 136
81
+ co: 137
82
+ cr: 138
83
+ hr: 22
84
+ cs: 13
85
+ da: 27
86
+ dv: 139
87
+ dua: 286
88
+ nl-LU: 111
89
+ nl: 12
90
+ nl-AW: 288
91
+ nl-BE: 112
92
+ nl-CW: 289
93
+ nl-NL: 546
94
+ nl-SX: 290
95
+ dz: 140
96
+ ebu: 291
97
+ en-DZ: 618
98
+ en-AS: 293
99
+ en-AU: 82
100
+ en-BH: 578
101
+ en-BB: 294
102
+ en-BE: 115
103
+ en-BZ: 295
104
+ en-BM: 296
105
+ en-BW: 297
106
+ en-CA: 83
107
+ en-EG: 615
108
+ en-EE: 594
109
+ en-GU: 298
110
+ en-GY: 299
111
+ en-HK: 300
112
+ en-IN: 87
113
+ en-ID: 583
114
+ en-IE: 118
115
+ en-IL: 597
116
+ en-JM: 301
117
+ en-JO: 612
118
+ en-KW: 579
119
+ en-LV: 591
120
+ en-LT: 588
121
+ en-LU: 108
122
+ en-MO: 575
123
+ en-MY: 606
124
+ en-MT: 302
125
+ en-MH: 303
126
+ en-MU: 304
127
+ en-NA: 305
128
+ en-NZ: 306
129
+ en-MP: 307
130
+ en-OM: 582
131
+ en-PK: 308
132
+ en-PH: 309
133
+ en-QA: 577
134
+ en-SA: 581
135
+ en-SG: 310
136
+ en-ZA: 311
137
+ en-CH: 574
138
+ en-TH: 609
139
+ en-TT: 312
140
+ en-UM: 313
141
+ en-VI: 314
142
+ en-AE: 580
143
+ en-GB: 45
144
+ en-US-POSIX: 315
145
+ en-US: 547
146
+ en-VN: 603
147
+ en-ZW: 316
148
+ eo: 24
149
+ et: 624
150
+ et-EE: 46
151
+ eur: 47
152
+ tlh-KL: 48
153
+ nap-IT: 49
154
+ pms-IT: 32
155
+ ee: 141
156
+ ee-GH: 318
157
+ ee-TG: 319
158
+ ewo: 320
159
+ fo: 627
160
+ fo-FO: 67
161
+ fj: 142
162
+ fil: 31
163
+ fi: 18
164
+ fr: 5
165
+ fr-BE: 109
166
+ fr-BJ: 324
167
+ fr-BF: 325
168
+ fr-BI: 326
169
+ fr-CM: 327
170
+ fr-CA: 53
171
+ fr-CF: 328
172
+ fr-TD: 329
173
+ fr-KM: 330
174
+ fr-CG: 331
175
+ fr-CD: 332
176
+ fr-CI: 333
177
+ fr-DJ: 334
178
+ fr-GQ: 335
179
+ fr-FR: 549
180
+ fr-GF: 336
181
+ fr-GA: 337
182
+ fr-GP: 338
183
+ fr-GN: 339
184
+ fr-LU: 110
185
+ fr-MG: 340
186
+ fr-ML: 341
187
+ fr-MQ: 342
188
+ fr-MU: 621
189
+ fr-YT: 343
190
+ fr-MC: 344
191
+ fr-MA: 600
192
+ fr-NE: 345
193
+ fr-RW: 347
194
+ fr-RE: 346
195
+ fr-BL: 348
196
+ fr-MF: 349
197
+ fr-SN: 350
198
+ fr-CH: 86
199
+ fr-TG: 351
200
+ fr-TN: 601
201
+ fy-NL: 59
202
+ ff: 143
203
+ gl-ES: 26
204
+ lg: 144
205
+ ka: 632
206
+ ka-GE: 35
207
+ de: 6
208
+ de-AT: 114
209
+ de-BE: 113
210
+ de-DE: 550
211
+ de-LI: 356
212
+ de-LU: 107
213
+ de-CH: 102
214
+ el: 20
215
+ el-CY: 357
216
+ el-GR: 551
217
+ gn: 145
218
+ gu: 630
219
+ gu-IN: 103
220
+ guz: 359
221
+ ht-HT: 96
222
+ ha: 146
223
+ ha-GH: 361
224
+ ha-NE: 362
225
+ ha-NG: 363
226
+ haw: 364
227
+ he: 42
228
+ hz: 147
229
+ hi: 587
230
+ hi-IN: 30
231
+ ho: 148
232
+ hu: 21
233
+ is-IS: 43
234
+ io: 149
235
+ ig: 150
236
+ id: 54
237
+ ia: 151
238
+ ie: 152
239
+ iu: 153
240
+ ik: 154
241
+ ga-IE: 68
242
+ it: 10
243
+ it-IT: 555
244
+ it-CH: 106
245
+ ja: 4
246
+ jv: 155
247
+ dyo: 370
248
+ kea: 372
249
+ kab: 374
250
+ kl: 156
251
+ kln: 377
252
+ kam: 379
253
+ kn: 638
254
+ kn-IN: 25
255
+ kr: 157
256
+ ks: 158
257
+ kk: 159
258
+ km: 635
259
+ km-KH: 117
260
+ ki: 160
261
+ rw: 161
262
+ kv: 163
263
+ kg: 164
264
+ kok: 386
265
+ ko: 9
266
+ khq: 388
267
+ ses: 390
268
+ ku-TR: 74
269
+ kj: 165
270
+ nmg: 392
271
+ ky: 166
272
+ lag: 394
273
+ lo: 167
274
+ la-VA: 73
275
+ lv-LV: 38
276
+ li: 168
277
+ ln: 169
278
+ ln-CG: 396
279
+ ln-CD: 397
280
+ lt: 641
281
+ lt-LT: 44
282
+ lu: 170
283
+ luo: 400
284
+ lb: 171
285
+ luy: 402
286
+ mk: 644
287
+ mk-MK: 69
288
+ jmc: 405
289
+ mgh: 407
290
+ kde: 409
291
+ mg: 172
292
+ ms: 55
293
+ ms-BN: 413
294
+ ms-MY: 558
295
+ ms-SG: 572
296
+ ml: 647
297
+ ml-IN: 75
298
+ mt-MT: 99
299
+ gv: 173
300
+ mr: 97
301
+ mh: 174
302
+ mas: 417
303
+ mas-KE: 418
304
+ mas-TZ: 419
305
+ mer: 420
306
+ mn-MN: 100
307
+ mn: 650
308
+ mfe: 422
309
+ mua: 424
310
+ mi-NZ: 105
311
+ naq: 426
312
+ na: 175
313
+ nv: 176
314
+ ng: 177
315
+ ne: 428
316
+ ne-IN: 429
317
+ ne-NP: 72
318
+ nd: 178
319
+ se: 179
320
+ nb: 77
321
+ nn: 15
322
+ 'no': 180
323
+ nus: 431
324
+ nyn: 433
325
+ oc: 182
326
+ oj: 183
327
+ or: 653
328
+ or-IN: 104
329
+ om: 185
330
+ om-ET: 436
331
+ om-KE: 437
332
+ os: 186
333
+ ps: 656
334
+ ps-AF: 57
335
+ fa: 29
336
+ fa-AF: 439
337
+ fa-IR: 561
338
+ pl: 16
339
+ pt-AO: 440
340
+ pt-BR: 76
341
+ pt-GW: 441
342
+ pt-MO: 570
343
+ pt-MZ: 442
344
+ pt-PT: 8
345
+ pt-ST: 443
346
+ pa: 58
347
+ pa-Arab-PK: 445
348
+ pa-Arab: 444
349
+ pa-IN: 564
350
+ pi: 187
351
+ qu: 188
352
+ ro: 19
353
+ ro-MD: 446
354
+ ro-RO: 565
355
+ rm: 189
356
+ rof: 448
357
+ rn: 162
358
+ ru: 11
359
+ ru-MD: 451
360
+ ru-RU: 566
361
+ ru-UA: 452
362
+ rwk: 453
363
+ saq: 455
364
+ sm: 190
365
+ sg: 191
366
+ sbp: 458
367
+ sa: 192
368
+ sc-IT: 40
369
+ gd: 193
370
+ seh: 460
371
+ sr: 462
372
+ sr-BA: 464
373
+ sr-Latn-BA: 465
374
+ sr-Latn-ME: 467
375
+ sr-Latn-RS: 468
376
+ sr-Latn: 463
377
+ sr-ME: 466
378
+ sr-RS: 52
379
+ ksb: 469
380
+ sn: 194
381
+ ii: 181
382
+ sd: 195
383
+ si: 196
384
+ sk: 34
385
+ sl: 659
386
+ sl-SI: 51
387
+ xog: 475
388
+ so: 197
389
+ so-DJ: 477
390
+ so-ET: 478
391
+ so-KE: 479
392
+ so-SO: 480
393
+ nr: 198
394
+ st: 199
395
+ es: 79
396
+ es-AR: 92
397
+ es-BO: 481
398
+ es-CL: 89
399
+ es-CO: 90
400
+ es-CR: 482
401
+ es-DO: 483
402
+ es-EC: 484
403
+ es-SV: 485
404
+ es-GQ: 486
405
+ es-GT: 487
406
+ es-HN: 488
407
+ es-419: 7
408
+ es-MX: 91
409
+ es-NI: 490
410
+ es-PA: 94
411
+ es-PY: 491
412
+ es-PE: 95
413
+ es-PR: 93
414
+ es-ES: 568
415
+ es-US: 492
416
+ es-UY: 493
417
+ es-VE: 88
418
+ su: 200
419
+ sw: 494
420
+ sw-KE: 71
421
+ sw-TZ: 495
422
+ ss: 201
423
+ sv: 17
424
+ sv-FI: 496
425
+ sv-SE: 569
426
+ gsw: 497
427
+ shi: 499
428
+ shi-MA: 501
429
+ shi-Tfng-MA: 502
430
+ shi-Tfng: 500
431
+ ty: 202
432
+ dav: 503
433
+ tg: 203
434
+ ta: 505
435
+ ta-IN: 33
436
+ ta-SG: 573
437
+ ta-LK: 506
438
+ twq: 507
439
+ tt: 204
440
+ te: 662
441
+ te-IN: 41
442
+ teo: 510
443
+ teo-KE: 511
444
+ teo-UG: 512
445
+ th: 56
446
+ bo: 205
447
+ bo-CN: 513
448
+ bo-IN: 514
449
+ ti: 206
450
+ ti-ER: 515
451
+ ti-ET: 516
452
+ tpi: 585
453
+ to: 207
454
+ ts: 208
455
+ tn: 209
456
+ tr: 14
457
+ tk: 210
458
+ tw: 211
459
+ uk: 36
460
+ ur: 101
461
+ ur-IN: 518
462
+ ur-PK: 519
463
+ ug: 212
464
+ uz: 213
465
+ uz-Arab-AF: 522
466
+ uz-Arab: 521
467
+ uz-Latn-UZ: 524
468
+ uz-Latn: 520
469
+ uz-UZ: 523
470
+ vai: 525
471
+ vai-Latn-LR: 527
472
+ vai-Latn: 526
473
+ vai-LR: 528
474
+ ve: 214
475
+ vi: 50
476
+ vo: 215
477
+ vun: 529
478
+ wa: 216
479
+ cy-GB: 70
480
+ wo: 217
481
+ xh: 218
482
+ yav: 532
483
+ yi: 219
484
+ yo: 220
485
+ dje: 535
486
+ za: 221
487
+ zu: 222