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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
# Copyright (c) 2005-2006 David Barri
require 'yaml'
require 'gloc-internal'
require 'gloc-helpers'
module GLoc
UTF_8= 'utf-8'
SHIFT_JIS= 'sjis'
EUC_JP= 'euc-jp'
# This module will be included in both instances and classes of GLoc includees.
# It is also included as class methods in the GLoc module itself.
module InstanceMethods
include Helpers
# Returns a localized string.
def l(symbol, *arguments)
return GLoc._l(symbol,current_language,*arguments)
end
# Returns a localized string in a specified language.
# This does not effect <tt>current_language</tt>.
def ll(lang, symbol, *arguments)
return GLoc._l(symbol,lang.to_sym,*arguments)
end
# Returns a localized string if the argument is a Symbol, else just returns the argument.
def ltry(possible_key)
possible_key.is_a?(Symbol) ? l(possible_key) : possible_key
end
# Uses the default GLoc rule to return a localized string.
# See lwr_() for more info.
def lwr(symbol, *arguments)
lwr_(:default, symbol, *arguments)
end
# Uses a <em>rule</em> to return a localized string.
# A rule is a function that uses specified arguments to return a localization key prefix.
# The prefix is appended to the localization key originally specified, to create a new key which
# is then used to lookup a localized string.
def lwr_(rule, symbol, *arguments)
GLoc._l("#{symbol}#{GLoc::_l_rule(rule,current_language).call(*arguments)}",current_language,*arguments)
end
# Returns <tt>true</tt> if a localized string with the specified key exists.
def l_has_string?(symbol)
return GLoc._l_has_string?(symbol,current_language)
end
# Sets the current language for this instance/class.
# Setting the language of a class effects all instances unless the instance has its own language defined.
def set_language(language)
@gloc_language= language.nil? ? nil : language.to_sym
end
# Sets the current language if the language passed is a valid language.
# If the language was valid, this method returns <tt>true</tt> else it will return <tt>false</tt>.
# Note that <tt>nil</tt> is not a valid language.
# See set_language(language) for more info.
def set_language_if_valid(language)
if GLoc.valid_language?(language)
set_language(language)
true
else
false
end
end
end
#---------------------------------------------------------------------------
# Instance
include ::GLoc::InstanceMethods
# Returns the instance-level current language, or if not set, returns the class-level current language.
def current_language
@gloc_language || self.class.current_language
end
#---------------------------------------------------------------------------
# Class
# All classes/modules that include GLoc will also gain these class methods.
# Notice that the GLoc::InstanceMethods module is also included.
module ClassMethods
include ::GLoc::InstanceMethods
# Returns the current language, or if not set, returns the GLoc current language.
def current_language
@gloc_language || GLoc.current_language
end
end
def self.included(target) #:nodoc:
super
class << target
include ::GLoc::ClassMethods
end
end
#---------------------------------------------------------------------------
# GLoc module
class << self
include ::GLoc::InstanceMethods
# Returns the default language
def current_language
GLoc::CONFIG[:default_language]
end
# Adds a collection of localized strings to the in-memory string store.
def add_localized_strings(lang, symbol_hash, override=true, strings_charset=nil)
_verbose_msg {"Adding #{symbol_hash.size} #{lang} strings."}
_add_localized_strings(lang, symbol_hash, override, strings_charset)
_verbose_msg :stats
end
# Creates a backup of the internal state of GLoc (ie. strings, langs, rules, config)
# and optionally clears everything.
def backup_state(clear=false)
s= _get_internal_state_vars.map{|o| o.clone}
_get_internal_state_vars.each{|o| o.clear} if clear
s
end
# Removes all localized strings from memory, either of a certain language (or languages),
# or entirely.
def clear_strings(*languages)
if languages.empty?
_verbose_msg {"Clearing all strings"}
LOCALIZED_STRINGS.clear
LOWERCASE_LANGUAGES.clear
else
languages.each {|l|
_verbose_msg {"Clearing :#{l} strings"}
l= l.to_sym
LOCALIZED_STRINGS.delete l
LOWERCASE_LANGUAGES.each_pair {|k,v| LOWERCASE_LANGUAGES.delete k if v == l}
}
end
end
alias :_clear_strings :clear_strings
# Removes all localized strings from memory, except for those of certain specified languages.
def clear_strings_except(*languages)
clear= (LOCALIZED_STRINGS.keys - languages)
_clear_strings(*clear) unless clear.empty?
end
# Returns the charset used to store localized strings in memory.
def get_charset(lang)
CONFIG[:internal_charset_per_lang][lang] || CONFIG[:internal_charset]
end
# Returns a GLoc configuration value.
def get_config(key)
CONFIG[key]
end
# Loads the localized strings that are included in the GLoc library.
def load_gloc_default_localized_strings(override=false)
GLoc.load_localized_strings "#{File.dirname(__FILE__)}/../lang", override
end
# Loads localized strings from all yml files in the specifed directory.
def load_localized_strings(dir=nil, override=true)
_charset_required
_get_lang_file_list(dir).each {|filename|
# Load file
raw_hash = YAML::load(File.read(filename))
raw_hash={} unless raw_hash.kind_of?(Hash)
filename =~ /([^\/\\]+)\.ya?ml$/
lang = $1.to_sym
file_charset = raw_hash['file_charset'] || UTF_8
# Convert string keys to symbols
dest_charset= get_charset(lang)
_verbose_msg {"Reading file #{filename} [charset: #{file_charset} --> #{dest_charset}]"}
symbol_hash = {}
Iconv.open(dest_charset, file_charset) do |i|
raw_hash.each {|key, value|
symbol_hash[key.to_sym] = i.iconv(value)
}
end
# Add strings to repos
_add_localized_strings(lang, symbol_hash, override)
}
_verbose_msg :stats
end
# Restores a backup of GLoc's internal state that was made with backup_state.
def restore_state(state)
_get_internal_state_vars.each do |o|
o.clear
o.send o.respond_to?(:merge!) ? :merge! : :concat, state.shift
end
end
# Sets the charset used to internally store localized strings.
# You can set the charset to use for a specific language or languages,
# or if none are specified the charset for ALL localized strings will be set.
def set_charset(new_charset, *langs)
CONFIG[:internal_charset_per_lang] ||= {}
# Convert symbol shortcuts
if new_charset.is_a?(Symbol)
new_charset= case new_charset
when :utf8, :utf_8 then UTF_8
when :sjis, :shift_jis, :shiftjis then SHIFT_JIS
when :eucjp, :euc_jp then EUC_JP
else new_charset.to_s
end
end
# Convert existing strings
(langs.empty? ? LOCALIZED_STRINGS.keys : langs).each do |lang|
cur_charset= get_charset(lang)
if cur_charset && new_charset != cur_charset
_verbose_msg {"Converting :#{lang} strings from #{cur_charset} to #{new_charset}"}
Iconv.open(new_charset, cur_charset) do |i|
bundle= LOCALIZED_STRINGS[lang]
bundle.each_pair {|k,v| bundle[k]= i.iconv(v)}
end
end
end
# Set new charset value
if langs.empty?
_verbose_msg {"Setting GLoc charset for all languages to #{new_charset}"}
CONFIG[:internal_charset]= new_charset
CONFIG[:internal_charset_per_lang].clear
else
langs.each do |lang|
_verbose_msg {"Setting GLoc charset for :#{lang} strings to #{new_charset}"}
CONFIG[:internal_charset_per_lang][lang]= new_charset
end
end
end
# Sets GLoc configuration values.
def set_config(hash)
CONFIG.merge! hash
end
# Sets the $KCODE global variable according to a specified charset, or else the
# current default charset for the default language.
def set_kcode(charset=nil)
_charset_required
charset ||= get_charset(current_language)
$KCODE= case charset
when UTF_8 then 'u'
when SHIFT_JIS then 's'
when EUC_JP then 'e'
else 'n'
end
_verbose_msg {"$KCODE set to #{$KCODE}"}
end
# Tries to find a valid language that is similar to the argument passed.
# Eg. :en, :en_au, :EN_US are all similar languages.
# Returns <tt>nil</tt> if no similar languages are found.
def similar_language(lang)
return nil if lang.nil?
return lang.to_sym if valid_language?(lang)
# Check lowercase without dashes
lang= lang.to_s.downcase.gsub('-','_')
return LOWERCASE_LANGUAGES[lang] if LOWERCASE_LANGUAGES.has_key?(lang)
# Check without dialect
if lang.to_s =~ /^([a-z]+?)[^a-z].*/
lang= $1
return LOWERCASE_LANGUAGES[lang] if LOWERCASE_LANGUAGES.has_key?(lang)
end
# Check other dialects
lang= "#{lang}_"
LOWERCASE_LANGUAGES.keys.each {|k| return LOWERCASE_LANGUAGES[k] if k.starts_with?(lang)}
# Nothing found
nil
end
# Returns an array of (currently) valid languages (ie. languages for which localized data exists).
def valid_languages
LOCALIZED_STRINGS.keys
end
# Returns <tt>true</tt> if there are any localized strings for a specified language.
# Note that although <tt>set_langauge nil</tt> is perfectly valid, <tt>nil</tt> is not a valid language.
def valid_language?(language)
LOCALIZED_STRINGS.has_key? language.to_sym rescue false
end
end
end
|