class Egalite::M17N::Translation

Attributes

allow_content_negotiation[RW]
langs[RW]
user_default_lang[RW]
aliases[RW]
data[RW]
english_name[RW]
langcode[R]
native_name[RW]

Public Class Methods

lang(s) click to toggle source
# 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) click to toggle source
# 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
new(langcode) click to toggle source
# File lib/egalite/m17n.rb, line 156
def initialize(langcode)
  @langcode = langcode
  @aliases = []
end

Public Instance Methods

fullmatch?(lang) click to toggle source
# File lib/egalite/m17n.rb, line 160
def fullmatch?(lang)
  lang = lang.to_s.downcase
  @langcode == lang or @aliases.include?(lang)
end
partialmatch?(lang) click to toggle source
# File lib/egalite/m17n.rb, line 164
def partialmatch?(lang)
  fullmatch?(lang.to_s.split(/(-|_)/).first)
end
translate_html(path, html) click to toggle source
# 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) click to toggle source
# 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 = []) click to toggle source
# 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

Private Instance Methods

method_path(c,a) click to toggle source
# File lib/egalite/m17n.rb, line 124
def method_path(c,a)
  c.class.name.to_s + '#' + a.to_s
end
t_hash(list, h) click to toggle source
# File lib/egalite/m17n.rb, line 130
def t_hash(list, h)
  if h.is_a?(EgaliteResponse)
    h.param = t_hash(list, h.param)
    return h
  end
  return h unless h.is_a?(Hash)
  h2 = {}
  h.each { |k,v|
    h2[k] = case v
      when String
        t_string(list,v)
      when Array
        v.map { |x| t_hash(list,x) }
      when Hash
        t_hash(list, v)
      else v
    end
  }
  h2
end
t_string(list, s) click to toggle source
# File lib/egalite/m17n.rb, line 127
def t_string(list, s)
  list[s] ? list[s] : s
end