Class: IsoDoc::I18n

Inherits:
Object
  • Object
show all
Defined in:
lib/isodoc/i18n.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lang, script, i18nyaml = nil) ⇒ I18n

Returns a new instance of I18n.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/isodoc/i18n.rb', line 51

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) { get[k] }
  end
end

Class Method Details

.l10n(text, lang = @lang, script = @script) ⇒ Object



63
64
65
# File 'lib/isodoc/i18n.rb', line 63

def self.l10n(text, lang = @lang, script = @script)
  l10n(text, lang, script)
end

Instance Method Details

#bidiwrap(text, lang, script) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/isodoc/i18n.rb', line 76

def bidiwrap(text, lang, script)
  my_script, my_rtl, outer_rtl = bidiwrap_vars(lang, script)
  if my_rtl && !outer_rtl
    mark = %w(Arab Aran).include?(my_script) ? "؜" : "‏"
    "#{mark}#{text}#{mark}"
  elsif !my_rtl && outer_rtl then "‎#{text}‎"
  else text
  end
end

#bidiwrap_vars(lang, script) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/isodoc/i18n.rb', line 86

def bidiwrap_vars(lang, script)
  my_script = script || Metanorma::Utils.default_script(lang)
  [my_script,
   Metanorma::Utils.rtl_script?(my_script),
   Metanorma::Utils.rtl_script?(@script || Metanorma::Utils
     .default_script(@lang))]
end

#getObject



43
44
45
# File 'lib/isodoc/i18n.rb', line 43

def get
  @labels
end

#l10n(text, 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



70
71
72
73
74
# File 'lib/isodoc/i18n.rb', line 70

def l10n(text, lang = @lang, script = @script)
  if lang == "zh" && script == "Hans" then l10n_zh(text)
  else bidiwrap(text, lang, script)
  end
end

#l10n_zh(text) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/isodoc/i18n.rb', line 94

def l10n_zh(text)
  xml = Nokogiri::HTML::DocumentFragment.parse(text)
  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(/<\?[^>]+>/, "")
end

#load_yaml(lang, script, i18nyaml = nil) ⇒ Object



5
6
7
8
9
10
# File 'lib/isodoc/i18n.rb', line 5

def load_yaml(lang, script, i18nyaml = nil)
  ret = load_yaml1(lang, script)
  return normalise_hash(ret.merge(YAML.load_file(i18nyaml))) if i18nyaml

  normalise_hash(ret)
end

#load_yaml1(lang, script) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/isodoc/i18n.rb', line 25

def load_yaml1(lang, script)
  case lang
  when "en", "fr", "ru", "de", "es", "ar"
    load_yaml2(lang)
  when "zh"
    if script == "Hans" then load_yaml2("zh-Hans")
    else load_yaml2("en")
    end
  else
    load_yaml2("en")
  end
end

#load_yaml2(str) ⇒ Object



38
39
40
41
# File 'lib/isodoc/i18n.rb', line 38

def load_yaml2(str)
  YAML.load_file(File.join(File.dirname(__FILE__),
                           "../isodoc-yaml/i18n-#{str}.yaml"))
end

#multiple_and(names, andword) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/isodoc/i18n.rb', line 105

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

#normalise_hash(ret) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/isodoc/i18n.rb', line 12

def normalise_hash(ret)
  case ret
  when Hash
    ret.each do |k, v|
      ret[k] = normalise_hash(v)
    end
    ret
  when Array then ret.map { |n| normalise_hash(n) }
  when String then ret.unicode_normalize(:nfc)
  else ret
  end
end

#set(key, val) ⇒ Object



47
48
49
# File 'lib/isodoc/i18n.rb', line 47

def set(key, val)
  @labels[key] = val
end