-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmini_i18n.rb
More file actions
186 lines (143 loc) Β· 4.31 KB
/
mini_i18n.rb
File metadata and controls
186 lines (143 loc) Β· 4.31 KB
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
require "yaml"
require "mini_i18n/version"
require "mini_i18n/utils"
require "mini_i18n/localization"
require "mini_i18n/pluralization"
require "mini_i18n/kernel_extensions"
module MiniI18n
class << self
include Localization
DEFAULT_LOCALE = :en
DEFAULT_SEPARATOR = '.'
attr_accessor :fallbacks
def default_locale
@@default_locale ||= DEFAULT_LOCALE
end
def default_locale=(new_locale)
@@default_locale = available_locale?(new_locale) || default_locale
end
def available_locales
@@available_locales ||= translations.keys
end
def available_locales=(new_locales)
@@available_locales = Array(new_locales).map(&:to_s)
# Load built-in localization defaults
@@available_locales.each do |locale|
default_locale_path = File.join(File.dirname(__FILE__), "mini_i18n", "locales", "#{locale}.yml")
if File.exist?(default_locale_path)
YAML.load_file(default_locale_path).each do |loc, translations|
add_translations(loc, translations)
end
end
end
end
def translations
@@translations ||= {}
end
def locale
Thread.current[:mini_i18n_locale] ||= default_locale
end
def locale=(new_locale)
set_locale(new_locale)
end
def separator
@@separator ||= DEFAULT_SEPARATOR
end
def separator=(new_separator)
@@separator = new_separator || DEFAULT_SEPARATOR
end
def pluralization_rules
@@pluralization_rules ||= {}
end
def pluralization_rules=(new_rules)
@@pluralization_rules = new_rules
end
def configure
yield(self) if block_given?
end
def load_translations(path)
Dir[path.to_s].sort.each do |file|
YAML.load_file(file).each do |locale, new_translations|
add_translations(locale.to_s, new_translations)
end
end
end
def translate(key, options = {})
return if key.empty? || translations.empty?
return multiple_translate(key, options) if key.is_a?(Array)
options = expand_all_locales(options)
return multiple_locales(key, options) if options[:locale].is_a?(Array)
_locale = available_locale?(options[:locale]) || locale
scope = options[:scope]
keys = [_locale.to_s]
keys.concat(scope.to_s.split(separator)) if scope
keys.concat(key.to_s.split(separator))
result = lookup(*keys)
result = with_fallbacks(result, keys)
result = with_pluralization(result, options, _locale)
result = with_interpolation(result, options)
result || options[:default]
end
alias t translate
private
def set_locale(new_locale)
new_locale = new_locale.to_s
if available_locale?(new_locale)
Thread.current[:mini_i18n_locale] = new_locale
end
locale
end
def available_locale?(new_locale)
locale_string = new_locale.to_s
available_locales.include?(locale_string) && locale_string
end
def lookup(*keys)
translations.dig(*keys)
end
def add_translations(locale, new_translations)
@@available_locales << locale unless available_locale?(locale)
if translations[locale]
translations[locale] = Utils.deep_merge(translations[locale], new_translations)
else
translations[locale] = new_translations
end
end
def with_fallbacks(result, keys)
if fallbacks && result.empty?
keys[0] = default_locale.to_s
result = lookup(*keys)
end
result
end
def with_pluralization(result, options, locale)
count = options[:count]
if count && result.is_a?(Hash)
result = Pluralization.pluralize(result, count, locale)
end
result
end
def with_interpolation(result, options)
if result.is_a?(String) && result.include?('%{')
result = Utils.interpolate(result, options)
end
result
end
def multiple_translate(keys, options)
keys.map do |key|
t(key, options)
end
end
def multiple_locales(key, options)
options[:locale].map do |_locale|
t(key, options.merge(locale: _locale))
end
end
def expand_all_locales(options)
if options[:locale] == '*'
options.merge(locale: available_locales)
else
options
end
end
end
end