File: uitranslator.rb

package info (click to toggle)
mikutter 4.1.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,260 kB
  • sloc: ruby: 20,126; sh: 183; makefile: 19
file content (44 lines) | stat: -rw-r--r-- 1,440 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-

require "gettext"

module Plugin::UITranslate
  LocaleDirectory = File.join(CHIConfig::CACHE, "uitranslator", "locale").freeze
  PODirectoryPrefix = 'po'.freeze
  LanguageMatcher = %r<#{PODirectoryPrefix}#{File::SEPARATOR}(\w+)#{File::SEPARATOR}.+?\.po\Z>.freeze
  LanguageFileInfo = Struct.new(:po, :mo)
end

Plugin.create :uitranslator do
  FileUtils.mkdir_p Plugin::UITranslate::LocaleDirectory
end

class Plugin
  include GetText

  alias __spec_uitranslate__ spec=
  def spec=(spec)
	__spec_uitranslate__(spec)

    po_root = File.join spec[:path], Plugin::UITranslate::PODirectoryPrefix
    if FileTest.exist?(po_root)
      Dir.glob(File.join(po_root, '*/*.po'.freeze)).map{|po_path|
        lang = po_path.match(Plugin::UITranslate::LanguageMatcher)[1]
        mo_path = File.join(Plugin::UITranslate::LocaleDirectory, lang, 'LC_MESSAGES'.freeze, "#{spec[:slug]}.mo")
        Plugin::UITranslate::LanguageFileInfo.new(po_path, mo_path)
      }.select{|info|
        if File.exist?(info.mo)
          File.mtime(info.po) > File.mtime(info.mo)
        else
          true end
      }.each{|info|
        require 'gettext/tools'
        FileUtils.mkdir_p(File.dirname(info.mo))
        GetText::Tools::MsgFmt.run(info.po, '-o'.freeze, info.mo)
        notice "generated mo file #{info.po} => #{info.mo}"
      }
      bindtextdomain(to_s, path: Plugin::UITranslate::LocaleDirectory)
    end
    spec
  end
end