Class: Egalite::M17N::Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/egalite/m17n.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(langcode) ⇒ Translation

Returns a new instance of Translation.



156
157
158
159
# File 'lib/egalite/m17n.rb', line 156

def initialize(langcode)
  @langcode = langcode
  @aliases = []
end

Class Attribute Details

.allow_content_negotiationObject

Returns the value of attribute allow_content_negotiation.



49
50
51
# File 'lib/egalite/m17n.rb', line 49

def allow_content_negotiation
  @allow_content_negotiation
end

.langsObject

Returns the value of attribute langs.



49
50
51
# File 'lib/egalite/m17n.rb', line 49

def langs
  @langs
end

.user_default_langObject

Returns the value of attribute user_default_lang.



49
50
51
# File 'lib/egalite/m17n.rb', line 49

def user_default_lang
  @user_default_lang
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



153
154
155
# File 'lib/egalite/m17n.rb', line 153

def aliases
  @aliases
end

#dataObject

Returns the value of attribute data.



153
154
155
# File 'lib/egalite/m17n.rb', line 153

def data
  @data
end

#english_nameObject

Returns the value of attribute english_name.



153
154
155
# File 'lib/egalite/m17n.rb', line 153

def english_name
  @english_name
end

#langcodeObject (readonly)

Returns the value of attribute langcode.



154
155
156
# File 'lib/egalite/m17n.rb', line 154

def langcode
  @langcode
end

#native_nameObject

Returns the value of attribute native_name.



153
154
155
# File 'lib/egalite/m17n.rb', line 153

def native_name
  @native_name
end

Class Method Details

.lang(s) ⇒ Object



117
118
119
120
121
122
# File 'lib/egalite/m17n.rb', line 117

def self.lang(s)
  return nil unless s
  a =   @@langs.find { |k,v| v.fullmatch?(s) }
  a ||= @@langs.find { |k,v| v.partialmatch?(s) }
  a ? a.last : nil
end

.load(path) ⇒ Object



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
# File 'lib/egalite/m17n.rb', line 51

def self.load(path)
  @@langs = {}
  
  s = open(path) { |f| f.read }
  
  langs = nil
  system_default = nil
  
  [:languages, :system_default, :english_name, :native_name, :aliases].each { |optname|
    s.gsub!(/\{\{#{optname}\s*(.+?)\s*\}\}\s*\n+/i) {
      values = $1.split(/\s*,\s*/)
      case optname
        when :languages
          langs = values
          values.each { |lang|
            @@langs[lang] = Translation.new(lang)
            @@langs[lang].data = {}
          }
        when :system_default
          lang = values.shift
          @@langs[lang] = Translation.new(lang)
          @@langs[lang].data = nil
          system_default = lang
        when :aliases
          lang = values.shift
          @@langs[lang].send("#{optname}=", values)
        else
          lang = values.shift
          @@langs[lang].send("#{optname}=", values.first)
      end
      ''
    }
  }
  
  s.split(/###+\s*\n+/).each { |part|
    next if part =~ /\A\s*\Z/
    lines = part.split(/\n+/)
    key = lines.shift
    (type, path) = key.split(/\s+/,2)
    raise "Egalite::M17N::Translation.load: type should be 'html', 'msg' or 'img' but it was '#{type}'" unless %w[msg html img].include?(type)
    lines.each { |line|
      if type == 'img'
        langs.each { |lang|
          next unless @@langs[lang].data
          img = line.sub(/\.(jpg|jpeg|gif|png)/i,"_#{lang}.\\1")
          @@langs[lang].data[:img] ||= {}
          @@langs[lang].data[:img][path] ||= {}
          @@langs[lang].data[:img][path][line] = img
        }
      else
        a = line.split(/\s*\t+\s*/)
        k = nil
        a.each_with_index { |s,i|
          unless @@langs[langs[i]].data
            k = s
          else
            @@langs[langs[i]].data[type.to_sym] ||= {}
            @@langs[langs[i]].data[type.to_sym][path] ||= {}
            @@langs[langs[i]].data[type.to_sym][path][k] = s
          end
        }
      end
    }
  }
  @@langs
end

Instance Method Details

#fullmatch?(lang) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
# File 'lib/egalite/m17n.rb', line 160

def fullmatch?(lang)
  lang = lang.to_s.downcase
  @langcode == lang or @aliases.include?(lang)
end

#partialmatch?(lang) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/egalite/m17n.rb', line 164

def partialmatch?(lang)
  fullmatch?(lang.to_s.split(/(-|_)/).first)
end

#translate_html(path, html) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/egalite/m17n.rb', line 167

def translate_html(path, html)
  return html unless @data
  list = @data[:html][path]
  return html unless list
  s = html.dup
  list.sort { |a,b| b[0].length <=> a[0].length }.each { |k,v| s.gsub!(k, v)}
  if @data[:img] and @data[:img][path]
    @data[:img][path].each { |k,v| s.gsub!(k, v) }
  end
  s
end

#translate_msg(controller, action, msg) ⇒ Object



178
179
180
181
182
183
# File 'lib/egalite/m17n.rb', line 178

def translate_msg(controller, action, msg)
  return msg unless @data
  list = @data[:msg][method_path(controller,action)]
  return msg unless list
  t_hash(list, msg)
end

#translate_string(controller, action, string, placeholders = []) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/egalite/m17n.rb', line 184

def translate_string(controller, action, string, placeholders = [])
  if @data
    list = @data[:msg][method_path(controller,action)]
    if list
      string = t_string(list, string)
    end
  end
  string = string.dup
  placeholders = placeholders.split(/\n/) unless placeholders.is_a?(Array)
  placeholders.each_with_index { |s2,i| string.gsub!(/\{#{i}\}/, s2) }
  string
end