Class: ReVIEW::I18n
Constant Summary collapse
- ALPHA_U =
%w[0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z].freeze
- ALPHA_L =
%w[0 a b c d e f g h i j k l m n o p q r s t u v w x y z].freeze
- ROMAN_U =
%w[0 I II III IV V VI VII VIII IX X XI XII XIII XIV XV XVI XVII XVIII XIX XX XXI XXII XXIII XXIV XXV XXVI XXVII].freeze
- ROMAN_L =
%w[0 i ii iii iv v vi vii viii ix x xi xii xiii xiv xv xvi xvii xviii xix xx xxi xxii xxiii xxiv xxv xxvi xxvii].freeze
- ALPHA_UW =
%w[0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z].freeze
- ALPHA_LW =
%w[0 a b c d e f g h i j k l m n o p q r s t u v w x y z].freeze
- ROMAN_UW =
%w[0 Ⅰ Ⅱ Ⅲ Ⅳ V Ⅵ Ⅶ Ⅷ Ⅸ X Ⅺ Ⅻ].freeze
- ARABIC_UW =
%w[〇 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].freeze
- ARABIC_LW =
%w[〇 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].freeze
- JAPAN =
%w[〇 一 二 三 四 五 六 七 八 九 十 十一 十二 十三 十四 十五 十六 十七 十八 十九 二十 二十一 二十二 二十三 二十四 二十五 二十六 二十七].freeze
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
Class Method Summary collapse
- .get(word, locale = nil) ⇒ Object
- .i18n(*_args) ⇒ Object
- .locale=(locale) ⇒ Object
- .setup(locale = 'ja', ymlfile = 'locale.yml') ⇒ Object
- .t(str, args = nil) ⇒ Object
- .update(user_i18n, locale = nil) ⇒ Object
-
.v ⇒ Object
for EPUBMaker backward compatibility.
Instance Method Summary collapse
- #get(word, locale = nil) ⇒ Object
-
#initialize(locale = nil) ⇒ I18n
constructor
A new instance of I18n.
- #load_default ⇒ Object
- #load_file(path) ⇒ Object
- #t(str, args = nil) ⇒ Object
- #update(user_i18n, locale = nil) ⇒ Object
- #update_localefile(path) ⇒ Object
Constructor Details
#initialize(locale = nil) ⇒ I18n
Returns a new instance of I18n.
60 61 62 63 |
# File 'lib/review/i18n.rb', line 60 def initialize(locale = nil) @locale = locale load_default end |
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
58 59 60 |
# File 'lib/review/i18n.rb', line 58 def locale @locale end |
Class Method Details
.get(word, locale = nil) ⇒ Object
54 55 56 |
# File 'lib/review/i18n.rb', line 54 def self.get(word, locale = nil) @i18n.get(word, locale) end |
.i18n(*_args) ⇒ Object
30 31 32 |
# File 'lib/review/i18n.rb', line 30 def self.i18n(*_args) raise NotImplementedError, 'I18n.i18n is obsoleted. Please use I18n.setup(locale, [ymlfile])' end |
.locale=(locale) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/review/i18n.rb', line 38 def self.locale=(locale) if @i18n @i18n.locale = locale else I18n.setup(locale) end end |
.setup(locale = 'ja', ymlfile = 'locale.yml') ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/review/i18n.rb', line 16 def self.setup(locale = 'ja', ymlfile = 'locale.yml') @i18n = ReVIEW::I18n.new(locale) lfile = nil if ymlfile lfile = File.(ymlfile, Dir.pwd) # backward compatibility raise ReVIEW::ConfigError, 'locale.yaml is obsoleted. Please use locale.yml.' if !File.exist?(lfile) && (ymlfile == 'locale.yml') && File.exist?(File.('locale.yaml', Dir.pwd)) end @i18n.update_localefile(lfile) if lfile && File.file?(lfile) end |
.t(str, args = nil) ⇒ Object
34 35 36 |
# File 'lib/review/i18n.rb', line 34 def self.t(str, args = nil) @i18n.t(str, args) end |
.update(user_i18n, locale = nil) ⇒ Object
50 51 52 |
# File 'lib/review/i18n.rb', line 50 def self.update(user_i18n, locale = nil) @i18n.update(user_i18n, locale) end |
.v ⇒ Object
for EPUBMaker backward compatibility
47 48 49 |
# File 'lib/review/i18n.rb', line 47 def self.t(str, args = nil) @i18n.t(str, args) end |
Instance Method Details
#get(word, locale = nil) ⇒ Object
100 101 102 103 |
# File 'lib/review/i18n.rb', line 100 def get(word, locale = nil) locale ||= @locale @store[locale][word] end |
#load_default ⇒ Object
65 66 67 |
# File 'lib/review/i18n.rb', line 65 def load_default load_file(File.('i18n.yml', File.dirname(__FILE__))) end |
#load_file(path) ⇒ Object
69 70 71 |
# File 'lib/review/i18n.rb', line 69 def load_file(path) @store = YAML.load_file(path) end |
#t(str, args = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/review/i18n.rb', line 105 def t(str, args = nil) frmt = @store[@locale][str].dup frmt.gsub!('%%', '##') unless args.is_a?(Array) args = args.nil? && frmt !~ /\%/ ? [] : [args] end percents = frmt.scan(/%[A-Za-z]{1,3}/) remove_args = [] percents.each_with_index do |i, idx| case i when '%pA' frmt.sub!(i, ALPHA_U[args[idx]]) remove_args << idx when '%pa' frmt.sub!(i, ALPHA_L[args[idx]]) remove_args << idx when '%pAW' frmt.sub!(i, ALPHA_UW[args[idx]]) remove_args << idx when '%paW' frmt.sub!(i, ALPHA_LW[args[idx]]) remove_args << idx when '%pR' frmt.sub!(i, ROMAN_U[args[idx]]) remove_args << idx when '%pr' frmt.sub!(i, ROMAN_L[args[idx]]) remove_args << idx when '%pRW' frmt.sub!(i, ROMAN_UW[args[idx]]) remove_args << idx when '%pJ' frmt.sub!(i, JAPAN[args[idx]]) remove_args << idx when '%pdW' frmt.sub!(i, ARABIC_LW[args[idx]]) remove_args << idx when '%pDW' frmt.sub!(i, ARABIC_UW[args[idx]]) remove_args << idx end end remove_args.reverse_each do |idx| args.delete_at idx end args_matched = (frmt.count('%') <= args.size) frmt.gsub!('##', '%%') args_matched ? (frmt % args) : frmt rescue str end |
#update(user_i18n, locale = nil) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/review/i18n.rb', line 91 def update(user_i18n, locale = nil) locale ||= @locale if @store[locale] @store[locale].merge!(user_i18n) else @store[locale] = user_i18n end end |
#update_localefile(path) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/review/i18n.rb', line 73 def update_localefile(path) user_i18n = YAML.load_file(path) locale = user_i18n['locale'] if locale user_i18n.delete('locale') if @store[locale] @store[locale].merge!(user_i18n) else @store[locale] = user_i18n end else user_i18n.each do |key, values| raise KeyError, "Invalid locale file: #{path}" unless values.is_a? Hash @store[key].merge!(values) end end end |