| 1 |
|
| 2 |
|
| 3 |
require "gettext" |
| 4 |
|
| 5 |
module Plugin::UITranslate |
| 6 |
LocaleDirectory = File.join(CHIConfig::CACHE, "uitranslator", "locale").freeze |
| 7 |
PODirectoryPrefix = 'po'.freeze |
| 8 |
LanguageMatcher = %r<#{PODirectoryPrefix}#{File::SEPARATOR}(\w+)#{File::SEPARATOR}.+?\.po\Z>.freeze |
| 9 |
LanguageFileInfo = Data.define(:po, :mo) |
| 10 |
end |
| 11 |
|
| 12 |
Plugin.create :uitranslator do |
| 13 |
FileUtils.mkdir_p Plugin::UITranslate::LocaleDirectory |
| 14 |
end |
| 15 |
|
| 16 |
class Plugin |
| 17 |
include GetText |
| 18 |
|
| 19 |
alias __spec_uitranslate__ spec= |
| 20 |
def spec=(spec) |
| 21 |
__spec_uitranslate__(spec) |
| 22 |
|
| 23 |
po_root = File.join spec[:path], Plugin::UITranslate::PODirectoryPrefix |
| 24 |
if FileTest.exist?(po_root) |
| 25 |
Dir.glob(File.join(po_root, '*/*.po'.freeze)).map{|po_path| |
| 26 |
lang = po_path.match(Plugin::UITranslate::LanguageMatcher)[1] |
| 27 |
mo_path = File.join(Plugin::UITranslate::LocaleDirectory, lang, 'LC_MESSAGES'.freeze, "#{spec[:slug]}.mo") |
| 28 |
Plugin::UITranslate::LanguageFileInfo.new(po_path, mo_path) |
| 29 |
}.select{|info| |
| 30 |
if File.exist?(info.mo) |
| 31 |
File.mtime(info.po) > File.mtime(info.mo) |
| 32 |
else |
| 33 |
true end |
| 34 |
}.each{|info| |
| 35 |
require 'gettext/tools' |
| 36 |
FileUtils.mkdir_p(File.dirname(info.mo)) |
| 37 |
GetText::Tools::MsgFmt.run(info.po, '-o'.freeze, info.mo) |
| 38 |
notice "generated mo file #{info.po} => #{info.mo}" |
| 39 |
} |
| 40 |
bindtextdomain(to_s, path: Plugin::UITranslate::LocaleDirectory) |
| 41 |
end |
| 42 |
spec |
| 43 |
end |
| 44 |
end |