Module: Runo::I18n

Included in:
Field, Workflow
Defined in:
lib/_i18n.rb

Overview

Author

Akira FUNAI

Copyright

Copyright © 2009 Akira FUNAI

Defined Under Namespace

Modules: REX Classes: Msgstr

Class Method Summary collapse

Class Method Details

._(msgid) ⇒ Object



128
129
130
# File 'lib/_i18n.rb', line 128

def _(msgid)
  Runo::I18n::Msgstr.new(Array(Runo::I18n.msg[msgid]).first || msgid)
end

.bindtextdomain(domain, po_dir) ⇒ Object



69
70
71
72
# File 'lib/_i18n.rb', line 69

def self.bindtextdomain(domain, po_dir)
  self.domain = domain
  self.po_dir = po_dir
end

.domainObject



53
54
55
# File 'lib/_i18n.rb', line 53

def self.domain
  @@domain ||= 'index'
end

.domain=(domain) ⇒ Object



57
58
59
# File 'lib/_i18n.rb', line 57

def self.domain=(domain)
  @@domain = domain
end

.find_msg(lang = self.lang) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/_i18n.rb', line 80

def self.find_msg(lang = self.lang)
  lang.each {|range|
    [
      range,
      range.sub(/-.*/, ''),
    ].uniq.each {|r|
      po_file = ::File.join(self.po_dir, r, "#{self.domain}.po")
      return ::File.open(po_file, ((RUBY_VERSION < '1.9') ? 'r' : 'r:utf-8')) {|f|
        self.parse_msg f
      } if ::File.readable? po_file
      return {} if r == 'en' # default
    }
  }
  {}
end

.langObject



34
35
36
# File 'lib/_i18n.rb', line 34

def self.lang
  Thread.current[:lang] || []
end

.lang=(http_accept_language) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/_i18n.rb', line 38

def self.lang=(http_accept_language)
  Thread.current[:msg] = nil
  tokens = http_accept_language.to_s.split(/,/)
  Thread.current[:lang] = tokens.sort_by {|t| # rfc3282
    [
      (t =~ /q=([\d\.]+)/) ? $1.to_f : 1.0,
      -tokens.index(t)
    ]
  }.reverse.collect {|i|
    if i =~ /([a-z]{1,8})(-[a-z]{1,8})?/i # rfc2616
      $2 ? ($1.downcase + $2.upcase) : $1.downcase
    end
  }
end

.merge_msg!(m) ⇒ Object



121
122
123
124
# File 'lib/_i18n.rb', line 121

def self.merge_msg!(m)
  m.delete :plural
  Thread.current[:msg] = self.msg.merge m
end

.msgObject



74
75
76
77
78
# File 'lib/_i18n.rb', line 74

def self.msg
  @@msg ||= {}
  @@msg[self.lang] ||= self.find_msg
  Thread.current[:msg] ||= @@msg[self.lang]
end

.n_(msgid, msgid_plural, n) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/_i18n.rb', line 132

def n_(msgid, msgid_plural, n)
  msgstrs = Runo::I18n.msg[msgid].is_a?(::Array) ? Runo::I18n.msg[msgid] : [msgid, msgid_plural]
  case v = Runo::I18n.msg[:plural] ? Runo::I18n.msg[:plural].call(n) : (n != 1)
    when true
      Runo::I18n::Msgstr.new msgstrs[1]
    when false
      Runo::I18n::Msgstr.new msgstrs[0]
    else
      Runo::I18n::Msgstr.new msgstrs[v.to_i]
  end
end

.parse_msg(f) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/_i18n.rb', line 96

def self.parse_msg(f)
  msg   = {}
  msgid = nil
  f.each_line {|line|
    case line
      when REX::COMMENT_FUZZY
        msgid = :skip_next
      when REX::COMMENT
        next
      when REX::PLURAL_EXPRESSION
        msg[:plural] = instance_eval "Proc.new {|n| #{$1} }"
      when REX::MSGID
        msgid = (msgid == :skip_next) ? :skip : $1
      when REX::MSGSTR_PLURAL
        if msgid.is_a? ::String
          msg[msgid] = [] unless msg[msgid].is_a? ::Array
          msg[msgid][$1.to_i] = $2
        end
      when REX::MSGSTR
        msg[msgid] = $1 if msgid.is_a? ::String
    end
  }
  msg
end

.po_dirObject



61
62
63
# File 'lib/_i18n.rb', line 61

def self.po_dir
  @@po_dir ||= ::File.expand_path('../locale', ::File.dirname(__FILE__))
end

.po_dir=(po_dir) ⇒ Object



65
66
67
# File 'lib/_i18n.rb', line 65

def self.po_dir=(po_dir)
  @@po_dir = po_dir
end