| Class | PoTranslation |
| In: |
app/models/po_translation.rb
|
| Parent: | ActiveRecord::Base |
gettext による翻訳のモデル。
データベース由来の .po ファイルおよび .mo ファイルを更新する。
# File app/models/po_translation.rb, line 118
118: def self.database_only_update_po_and_make_mo(skip_if_exist_po=true)
119: po_path, mo_path = get_mo_path_and_po_path
120: return if skip_if_exist_po && File.exist?(po_path)
121: File.open(po_path, "wb") do |f|
122: export_to_po(f)
123: end
124: GetText.rmsgfmt(po_path, mo_path)
125: GetText.clear_cache
126: logger.info("%%% #{self}.database_only_update_po_and_make_mo updated")
127: end
全言語について database_only_update_po_and_make_mo を行う。
# File app/models/po_translation.rb, line 131
131: def self.database_only_update_po_and_make_mo_all(skip_if_exist_po=true)
132: @sub_classes ||= Language.all.collect do |lang|
133: "PoTranslation#{lang.code.capitalize}".constantize
134: end
135: @sub_classes.each do |po_translation|
136: po_translation.database_only_update_po_and_make_mo(skip_if_exist_po)
137: end
138: end
.po ファイルとしてエクスポートする。
# File app/models/po_translation.rb, line 69
69: def self.export_to_po(out)
70: messages = find_all_by_type(self.to_s, :order => "id")
71: require "gettext/rgettext"
72: out << GetText::RGetText.generate_pot_header.sub(/Plural-Forms: [^\\]+/) do
73: "Plural-Forms: nplurals=#{nplurals}; plural=#{plural};"
74: end.sub(/Project-Id-Version: PACKAGE VERSION/) do
75: "Project-Id-Version: #{App::PACKAGE} #{App::VERSION}"
76: end.sub(/Last-Translator: [^\\]+/) do
77: "Last-Translator: #{App::DB_LAST_TRANSLATOR}"
78: end.sub(/Language-Team: [^\\]+/) do
79: "Language-Team: #{App::DB_LANGUAGE_TEAM}"
80: end.sub(/^\#, fuzzy\n/, "")
81: out << "\n"
82: messages.each do |msg|
83: out << msg.to_po_entry
84: end
85: end
.mo ファイルおよび .po ファイルのパスを返す。
# File app/models/po_translation.rb, line 96
96: def self.get_mo_path_and_po_path
97: return @po_path, @mo_path if @po_path && @mo_path
98: mo_outdir = File.join(RAILS_ROOT, "locale", lang, "LC_MESSAGES")
99: FileUtils.mkdir_p(mo_outdir) unless File.directory?(mo_outdir)
100: po_outdir = File.dirname(mo_outdir)
101: @po_path = File.join(po_outdir, "database.po")
102: @mo_path = File.join(mo_outdir, "database.mo")
103: return @po_path, @mo_path
104: end
言語を2文字からなる文字列で返す。
# File app/models/po_translation.rb, line 88
88: def self.lang
89: to_s[/[A-Z][a-z]\z/].downcase
90: end
.po ファイルを削除する。
# File app/models/po_translation.rb, line 107
107: def self.remove_po
108: po_path, = get_mo_path_and_po_path
109: begin
110: File.unlink(po_path) if File.exist?(po_path)
111: rescue SystemCallError => e
112: logger.error(e)
113: # ignore
114: end
115: end
個人利用のためのコピーを返す。
# File app/models/po_translation.rb, line 141
141: def private_copy(po_message_id)
142: translation = self.class.new
143: translation.attributes = attributes
144: translation.po_message_id = po_message_id
145: translation.save!
146: return translation
147: end
.po ファイルの翻訳エントリに変換する。
# File app/models/po_translation.rb, line 36
36: def to_po_entry
37: str = "\n"
38: unless po_message
39: logger.warn("missing po message of #{self.to_s}")
40: return str
41: end
42: str << "\#: #{self.class}:#{id} #{po_message.class}:#{po_message.id}\n"
43: str << po_message.to_po_string
44: str << self.to_po_string
45: if po_message.respond_to?(:to_po_string_fast)
46: str << "\n"
47: str << "\#: #{self.class}:#{id} #{po_message.class}:#{po_message.id}\n"
48: str << po_message.to_po_string_fast
49: str << self.to_po_string
50: end
51: return str
52: end
.po ファイルでの文字列に変換する。
# File app/models/po_translation.rb, line 55
55: def to_po_string
56: if po_message.plural?
57: str = ""
58: self.class.nplurals.times do |i|
59: m = __send__("msgstr_#{i}")
60: str << "msgstr[#{i}] #{m.to_s.po_mangle}\n"
61: end
62: else
63: str = "msgstr #{msgstr.to_s.po_mangle}\n"
64: end
65: return str
66: end