Class: IsoDoc::I18n
- Inherits:
-
Object
- Object
- IsoDoc::I18n
- Defined in:
- lib/isodoc/i18n.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(lang, script, i18nyaml = nil) ⇒ I18n
constructor
A new instance of I18n.
-
#l10n(x, lang = @lang, script = @script) ⇒ Object
TODO: move to localization file function localising spaces and punctuation.
- #load_yaml(lang, script, i18nyaml = nil) ⇒ Object
- #load_yaml1(lang, script) ⇒ Object
- #multiple_and(names, andword) ⇒ Object
- #set(x, y) ⇒ Object
Constructor Details
#initialize(lang, script, i18nyaml = nil) ⇒ I18n
Returns a new instance of I18n.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/isodoc/i18n.rb', line 35 def initialize(lang, script, i18nyaml = nil) @lang = lang @script = script y = load_yaml(lang, script, i18nyaml) @labels = y @labels["language"] = @lang @labels["script"] = @script @labels.each do |k, v| self.class.send(:define_method, k.downcase) { v } end end |
Class Method Details
.l10n(x, lang = @lang, script = @script) ⇒ Object
47 48 49 |
# File 'lib/isodoc/i18n.rb', line 47 def self.l10n(x, lang = @lang, script = @script) l10n(x, lang, script) end |
Instance Method Details
#get ⇒ Object
27 28 29 |
# File 'lib/isodoc/i18n.rb', line 27 def get @labels end |
#l10n(x, lang = @lang, script = @script) ⇒ Object
TODO: move to localization file function localising spaces and punctuation. Not clear if period needs to be localised for zh
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/isodoc/i18n.rb', line 54 def l10n(x, lang = @lang, script = @script) if lang == "zh" && script == "Hans" xml = Nokogiri::HTML::DocumentFragment.parse(x) xml.traverse do |n| next unless n.text? n.replace(n.text.gsub(/ /, "").gsub(/:/, ":").gsub(/,/, "、"). gsub(/\(/, "(").gsub(/\)/, ")"). gsub(/\[/, "【").gsub(/\]/, "】")) end xml.to_xml.gsub(/<b>/, "").gsub("</b>", "").gsub(/<\?[^>]+>/, "") else x end end |
#load_yaml(lang, script, i18nyaml = nil) ⇒ Object
5 6 7 8 9 |
# File 'lib/isodoc/i18n.rb', line 5 def load_yaml(lang, script, i18nyaml = nil) ret = load_yaml1(lang, script) return ret.merge(YAML.load_file(i18nyaml)) if i18nyaml ret end |
#load_yaml1(lang, script) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/isodoc/i18n.rb', line 11 def load_yaml1(lang, script) if lang == "en" YAML.load_file(File.join(File.dirname(__FILE__), "../isodoc-yaml/i18n-en.yaml")) elsif lang == "fr" YAML.load_file(File.join(File.dirname(__FILE__), "../isodoc-yaml/i18n-fr.yaml")) elsif lang == "zh" && script == "Hans" YAML.load_file(File.join(File.dirname(__FILE__), "../isodoc-yaml/i18n-zh-Hans.yaml")) else YAML.load_file(File.join(File.dirname(__FILE__), "../isodoc-yaml/i18n-en.yaml")) end end |
#multiple_and(names, andword) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/isodoc/i18n.rb', line 69 def multiple_and(names, andword) return '' if names.empty? return names[0] if names.length == 1 (names.length == 2) && (return l10n("#{names[0]} #{andword} #{names[1]}", @lang, @script)) l10n(names[0..-2].join(', ') + " #{andword} #{names[-1]}", @lang, @script) end |
#set(x, y) ⇒ Object
31 32 33 |
# File 'lib/isodoc/i18n.rb', line 31 def set(x, y) @labels[x] = y end |