-
Notifications
You must be signed in to change notification settings - Fork 148
/
missing.rb
72 lines (65 loc) · 2.28 KB
/
missing.rb
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
# frozen_string_literal: true
# This extension stores translation stub records for missing translations to
# the database.
#
# This is useful if you have a web based translation tool. It will populate
# the database with untranslated keys as the application is being used. A
# translator can then go through these and add missing translations.
#
# Example usage:
#
# I18n::Backend::Chain.send(:include, I18n::Backend::ActiveRecord::Missing)
# I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18n::Backend::Simple.new)
#
# Stub records for pluralizations will also be created for each key defined
# in i18n.plural.keys.
#
# For example:
#
# # en.yml
# en:
# i18n:
# plural:
# keys: [:zero, :one, :other]
#
# # pl.yml
# pl:
# i18n:
# plural:
# keys: [:zero, :one, :few, :other]
#
# It will also persist interpolation keys in Translation#interpolations so
# translators will be able to review and use them.
module I18n
module Backend
class ActiveRecord
module Missing
include Flatten
include TranslationModel
def store_default_translations(locale, key, options = {})
count, scope, _, separator = options.values_at(:count, :scope, :default, :separator)
separator ||= I18n.default_separator
key = normalize_flat_keys(locale, key, scope, separator)
return if translation_model.locale(locale).lookup(key).exists?
interpolations = options.keys - I18n::RESERVED_KEYS
keys = count ? I18n.t('i18n.plural.keys', locale: locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
keys.each { |k| store_default_translation(locale, k, interpolations) }
end
def store_default_translation(locale, key, interpolations)
translation = translation_model.new locale: locale.to_s, key: key
translation.interpolations = interpolations
translation.save
end
def translate(locale, key, options = {})
result = catch(:exception) { super }
if result.is_a?(I18n::MissingTranslation)
store_default_translations(locale, key, options)
throw(:exception, result)
else
result
end
end
end
end
end
end