Module: AlGetText

Defined in:
lib/al_gettext.rb

Overview

alone : application framework for embedded systems.

Copyright (c) 2025 Hirohito Higashi All Rights Reserved.

gettext エミュレーション

Constant Summary collapse

@@trans_tables =

Returns 翻訳テーブル “ja_JP”=>{“orig”=>“trans”,…, … }.

Returns:

  • (Hash)

    翻訳テーブル “ja_JP”=>{“orig”=>“trans”,…, … }

{}
@@current_locale =

Returns カレントロケール.

Returns:

  • (String)

    カレントロケール

defined?(AL_DEFAULT_LOCALE) ? AL_DEFAULT_LOCALE : nil
@@domain_name =

Returns ドメイン名.

Returns:

  • (String)

    ドメイン名

"messages"
@@path_mo =

Returns 翻訳ファイルパス.

Returns:

  • (String)

    翻訳ファイルパス

nil

Instance Method Summary collapse

Instance Method Details

#_(s) ⇒ Object

gettext



102
103
104
# File 'lib/al_gettext.rb', line 102

def _(s)
  (@@trans_tables[ @@current_locale ] || _load_trans_file())[s] || s
end

#_load_trans_fileObject

load transration file.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/al_gettext.rb', line 26

def _load_trans_file()
  if @@path_mo
    file_path = "#{@@path_mo}/#{@@current_locale}/LC_MESSAGES/#{@@domain_name}.mo"
  else
    file_path = "#{AL_BASE_DIR}/locale/#{@@current_locale}/LC_MESSAGES/#{@@domain_name}.mo"
    if !File.exist?(file_path)
      file_path = "#{AL_LIB_DIR}/locale/#{@@current_locale}/LC_MESSAGES/#{@@domain_name}.mo"
    end
  end

  # check file exist.
  if !File.exist?(file_path)
    return @@trans_tables[@@current_locale] = {}
  end

  # read the .mo file
  table = {}
  File.open(file_path, "rb") {|f|
    # read header.
    magic, rev, n_strings, orig_tbl_offset, trans_tbl_offset = f.read(20).unpack("L<5")

    # check magic number.
    if magic != 0x950412de
      raise "Invalid MO file '#{file_path}'"
    end

    # read the original and trans table informations.
    orig_descriptors = []
    f.seek(orig_tbl_offset)
    n_strings.times {
      orig_descriptors << f.read(8).unpack("L<2") # [length, offset]
    }

    trans_descriptors = []
    f.seek(trans_tbl_offset)
    n_strings.times {
      trans_descriptors << f.read(8).unpack("L<2") # [length, offset]
    }

    # read actual string.
    n_strings.times {|i|
      f.seek( orig_descriptors[i][1] )
      original_string = f.read( orig_descriptors[i][0] )
      next if original_string.empty?

      f.seek( trans_descriptors[i][1] )
      translated_string = f.read( trans_descriptors[i][0] )

      table[ original_string.force_encoding(Encoding::UTF_8) ] =
        translated_string.force_encoding(Encoding::UTF_8)
    }
  }

  @@trans_tables[@@current_locale] = table
end

#bindtextdomain(domain_name, **options) ⇒ Object

bind text domain



85
86
87
88
# File 'lib/al_gettext.rb', line 85

def bindtextdomain(domain_name, **options)
  @@domain_name = domain_name
  @@path_mo = options[:path]
end

#p_(msgctxt, s) ⇒ Object

gettext with context



109
110
111
# File 'lib/al_gettext.rb', line 109

def p_(msgctxt, s)
  (@@trans_tables[ @@current_locale ] || _load_trans_file())["#{msgctxt}\x04#{s}"] || s
end

#set_locale(locale) ⇒ Object

set the locale

Parameters:

  • locale (String)

    ロケール名



95
96
97
# File 'lib/al_gettext.rb', line 95

def set_locale( locale )
  @@current_locale = locale
end