Class: Bisu::Localizer

Inherits:
Object
  • Object
show all
Defined in:
lib/bisu/localizer.rb

Instance Method Summary collapse

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_languages = []) ⇒ 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
40
41
42
43
44
45
# File 'lib/bisu/localizer.rb', line 13

def localize(text, language, locale, fallback_languages=[])
  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 = localize_key(l[:key], [language] + fallback_languages)
      localized = process(localized)

      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

      unless t.gsub!(l[:match], localized)
        Logger.warn("Could not find translation for #{l[:match]} in #{language}")
      end

      unless @type.eql?(:ror) || l[:ignore_param_warn] == true
        localized.scan(/%{[^}]+}/) { |match| Logger.error("Could not find translation param for #{match} in #{language}") }
      end
    else
      Logger.warn("Could not find translation for #{l[:match]} in #{language}")
    end
  end

  t
end

#localize_key(key, ordered_languages) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/bisu/localizer.rb', line 47

def localize_key(key, ordered_languages)
  ordered_languages.each do |language|
    if localized = @dict.localize(language, key)
      return localized
    end
  end

  nil
end