Class: Bisu::Localizer
- Inherits:
-
Object
- Object
- Bisu::Localizer
- Defined in:
- lib/bisu/localizer.rb
Instance Method Summary collapse
-
#initialize(dictionary, type) ⇒ Localizer
constructor
A new instance of Localizer.
- #localize(text, language, locale, fallback_language = nil) ⇒ Object
Constructor Details
#initialize(dictionary, type) ⇒ Localizer
Returns a new instance of Localizer.
3 4 5 6 7 8 9 10 11 |
# File 'lib/bisu/localizer.rb', line 3 def initialize(dictionary, type) @dict = dictionary @type = type.to_s.downcase.to_sym unless [:ios, :android, :ror].include?(@type) Logger.error("Unknown type #{@type}") raise "Unknown type #{@type}" end end |
Instance Method Details
#localize(text, language, locale, fallback_language = nil) ⇒ Object
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 |
# File 'lib/bisu/localizer.rb', line 13 def localize(text, language, locale, fallback_language=nil) t = text t = t.gsub("$specialKLanguage$", language) t = t.gsub("$specialKLocale$", locale) t = t.gsub("$specialKComment1$", "This file was automatically generated based on a translation template.") t = t.gsub("$specialKComment2$", "Remember to CHANGE THE TEMPLATE and not this file!") to_localize(t).map do |l| if localized = @dict.localize(language, l[:key]) || @dict.localize(fallback_language, l[:key]) l[:params].each do |param, value| if localized.match("%{#{param}}") localized = localized.gsub("%{#{param}}", value) else Logger.error("Parameter #{param} not found in translation for #{l[:key]} in #{language}") end end t = t.gsub(l[:match], process(localized)) end end t.scan(/\$(k[^\$\{]+)(?:\{(.+)\})?\$/) { |match| Logger.warn("Could not find translation for #{match[0]} in #{language}") } unless @type.eql?(:ror) t.scan(/%{[^}]+}/) { |match| Logger.error("Could not find translation param for #{match} in #{language}") } end t end |