| Module | GLoc |
| In: |
lib/gloc-helpers.rb
lib/gloc-internal.rb lib/gloc-version.rb lib/gloc.rb |
Copyright © 2005-2006 David Barri
| LOCALIZED_STRINGS | = | {} |
| RULES | = | {} |
| LOWERCASE_LANGUAGES | = | {} |
| UTF_8 | = | 'utf-8' |
| SHIFT_JIS | = | 'sjis' |
| EUC_JP | = | 'euc-jp' |
| clear_strings | -> | _clear_strings |
Adds a collection of localized strings to the in-memory string store.
# File lib/gloc.rb, line 113
113: def add_localized_strings(lang, symbol_hash, override=true, strings_charset=nil)
114: _verbose_msg {"Adding #{symbol_hash.size} #{lang} strings."}
115: _add_localized_strings(lang, symbol_hash, override, strings_charset)
116: _verbose_msg :stats
117: end
Creates a backup of the internal state of GLoc (ie. strings, langs, rules, config) and optionally clears everything.
# File lib/gloc.rb, line 121
121: def backup_state(clear=false)
122: s= _get_internal_state_vars.map{|o| o.clone}
123: _get_internal_state_vars.each{|o| o.clear} if clear
124: s
125: end
Removes all localized strings from memory, either of a certain language (or languages), or entirely.
# File lib/gloc.rb, line 129
129: def clear_strings(*languages)
130: if languages.empty?
131: _verbose_msg {"Clearing all strings"}
132: LOCALIZED_STRINGS.clear
133: LOWERCASE_LANGUAGES.clear
134: else
135: languages.each {|l|
136: _verbose_msg {"Clearing :#{l} strings"}
137: l= l.to_sym
138: LOCALIZED_STRINGS.delete l
139: LOWERCASE_LANGUAGES.each_pair {|k,v| LOWERCASE_LANGUAGES.delete k if v == l}
140: }
141: end
142: end
Removes all localized strings from memory, except for those of certain specified languages.
# File lib/gloc.rb, line 146
146: def clear_strings_except(*languages)
147: clear= (LOCALIZED_STRINGS.keys - languages)
148: _clear_strings(*clear) unless clear.empty?
149: end
Returns the default language
# File lib/gloc.rb, line 108
108: def current_language
109: GLoc::CONFIG[:default_language]
110: end
Returns the charset used to store localized strings in memory.
# File lib/gloc.rb, line 152
152: def get_charset(lang)
153: CONFIG[:internal_charset_per_lang][lang] || CONFIG[:internal_charset]
154: end
Loads localized strings from all yml files in the specifed directory.
# File lib/gloc.rb, line 167
167: def load_localized_strings(dir=nil, override=true)
168: _charset_required
169: _get_lang_file_list(dir).each {|filename|
170:
171: # Load file
172: raw_hash = YAML::load(File.read(filename))
173: raw_hash={} unless raw_hash.kind_of?(Hash)
174: filename =~ /([^\/\\]+)\.ya?ml$/
175: lang = $1.to_sym
176: file_charset = raw_hash['file_charset'] || UTF_8
177:
178: # Convert string keys to symbols
179: dest_charset= get_charset(lang)
180: _verbose_msg {"Reading file #{filename} [charset: #{file_charset} --> #{dest_charset}]"}
181: symbol_hash = {}
182: Iconv.open(dest_charset, file_charset) do |i|
183: raw_hash.each {|key, value|
184: symbol_hash[key.to_sym] = i.iconv(value)
185: }
186: end
187:
188: # Add strings to repos
189: _add_localized_strings(lang, symbol_hash, override)
190: }
191: _verbose_msg :stats
192: end
Restores a backup of GLoc’s internal state that was made with backup_state.
# File lib/gloc.rb, line 195
195: def restore_state(state)
196: _get_internal_state_vars.each do |o|
197: o.clear
198: o.send o.respond_to?(:merge!) ? :merge! : :concat, state.shift
199: end
200: 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.
# File lib/gloc.rb, line 205
205: def set_charset(new_charset, *langs)
206: CONFIG[:internal_charset_per_lang] ||= {}
207:
208: # Convert symbol shortcuts
209: if new_charset.is_a?(Symbol)
210: new_charset= case new_charset
211: when :utf8, :utf_8 then UTF_8
212: when :sjis, :shift_jis, :shiftjis then SHIFT_JIS
213: when :eucjp, :euc_jp then EUC_JP
214: else new_charset.to_s
215: end
216: end
217:
218: # Convert existing strings
219: (langs.empty? ? LOCALIZED_STRINGS.keys : langs).each do |lang|
220: cur_charset= get_charset(lang)
221: if cur_charset && new_charset != cur_charset
222: _verbose_msg {"Converting :#{lang} strings from #{cur_charset} to #{new_charset}"}
223: Iconv.open(new_charset, cur_charset) do |i|
224: bundle= LOCALIZED_STRINGS[lang]
225: bundle.each_pair {|k,v| bundle[k]= i.iconv(v)}
226: end
227: end
228: end
229:
230: # Set new charset value
231: if langs.empty?
232: _verbose_msg {"Setting GLoc charset for all languages to #{new_charset}"}
233: CONFIG[:internal_charset]= new_charset
234: CONFIG[:internal_charset_per_lang].clear
235: else
236: langs.each do |lang|
237: _verbose_msg {"Setting GLoc charset for :#{lang} strings to #{new_charset}"}
238: CONFIG[:internal_charset_per_lang][lang]= new_charset
239: end
240: end
241: end
Sets the $KCODE global variable according to a specified charset, or else the current default charset for the default language.
# File lib/gloc.rb, line 250
250: def set_kcode(charset=nil)
251: _charset_required
252: charset ||= get_charset(current_language)
253: $KCODE= case charset
254: when UTF_8 then 'u'
255: when SHIFT_JIS then 's'
256: when EUC_JP then 'e'
257: else 'n'
258: end
259: _verbose_msg {"$KCODE set to #{$KCODE}"}
260: 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 nil if no similar languages are found.
# File lib/gloc.rb, line 265
265: def similar_language(lang)
266: return nil if lang.nil?
267: return lang.to_sym if valid_language?(lang)
268: # Check lowercase without dashes
269: lang= lang.to_s.downcase.gsub('-','_')
270: return LOWERCASE_LANGUAGES[lang] if LOWERCASE_LANGUAGES.has_key?(lang)
271: # Check without dialect
272: if lang.to_s =~ /^([a-z]+?)[^a-z].*/
273: lang= $1
274: return LOWERCASE_LANGUAGES[lang] if LOWERCASE_LANGUAGES.has_key?(lang)
275: end
276: # Check other dialects
277: lang= "#{lang}_"
278: LOWERCASE_LANGUAGES.keys.each {|k| return LOWERCASE_LANGUAGES[k] if k.starts_with?(lang)}
279: # Nothing found
280: nil
281: end
Returns true if there are any localized strings for a specified language. Note that although set_langauge nil is perfectly valid, nil is not a valid language.
# File lib/gloc.rb, line 290
290: def valid_language?(language)
291: LOCALIZED_STRINGS.has_key? language.to_sym rescue false
292: end
Returns an array of (currently) valid languages (ie. languages for which localized data exists).
# File lib/gloc.rb, line 284
284: def valid_languages
285: LOCALIZED_STRINGS.keys
286: end