Skip to content

Commit

Permalink
add RTL and LTR markers if i18n text has different directionality fro…
Browse files Browse the repository at this point in the history
…m document text: #166
  • Loading branch information
opoudjis committed Dec 13, 2021
1 parent 8ab4450 commit 42dcc6a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
39 changes: 30 additions & 9 deletions lib/isodoc/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def initialize(lang, script, i18nyaml = nil)
@labels = y
@labels["language"] = @lang
@labels["script"] = @script
@labels.each do |k, v|
@labels.each do |k, _v|
self.class.send(:define_method, k.downcase) { get[k] }
end
end
Expand All @@ -68,19 +68,40 @@ def self.l10n(text, lang = @lang, script = @script)
# function localising spaces and punctuation.
# Not clear if period needs to be localised for zh
def l10n(text, lang = @lang, script = @script)
if lang == "zh" && script == "Hans"
xml = Nokogiri::HTML::DocumentFragment.parse(text)
xml.traverse do |n|
next unless n.text?
if lang == "zh" && script == "Hans" then l10n_zh(text)
else bidiwrap(text, lang, script)
end
end

n.replace(n.text.gsub(/ /, "").gsub(/:/, ":").gsub(/,/, "、")
.gsub(/\(/, "(").gsub(/\)/, ")").gsub(/\[/, "【").gsub(/\]/, "】"))
end
xml.to_xml.gsub(/<b>/, "").gsub("</b>", "").gsub(/<\?[^>]+>/, "")
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) ? "&#x61c;" : "&#x200f;"
"#{mark}#{text}#{mark}"
elsif !my_rtl && outer_rtl then "&#x200e;#{text}&#x200e;"
else text
end
end

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

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

def multiple_and(names, andword)
return "" if names.empty?
return names[0] if names.length == 1
Expand Down
38 changes: 38 additions & 0 deletions spec/isodoc/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,44 @@
.to be_equivalent_to xmlpp(output)
end

it "processes LTR within RTL" do
c = IsoDoc::Convert.new({})
c.convert_init(<<~"INPUT", "test", false)
<iso-standard xmlns="http://riboseinc.com/isoxml">
<bibdata type="standard">
<language>fa</language>
<script>Arab</script>
</bibdata>
</iso-standard>
INPUT
expect(c.i18n.l10n("hello!", "en", "Latn")).to eq "&#x200e;hello!&#x200e;"
end

it "processes Hebrew RTL within LTR" do
c = IsoDoc::Convert.new({})
c.convert_init(<<~"INPUT", "test", false)
<iso-standard xmlns="http://riboseinc.com/isoxml">
<bibdata type="standard">
<language>en</language>
</bibdata>
</iso-standard>
INPUT
expect(c.i18n.l10n("hello!", "he", "Hebr")).to eq "&#x200f;hello!&#x200f;"
end

it "processes Arabic RTL within LTR" do
c = IsoDoc::Convert.new({})
c.convert_init(<<~"INPUT", "test", false)
<iso-standard xmlns="http://riboseinc.com/isoxml">
<bibdata type="standard">
<language>en</language>
</bibdata>
</iso-standard>
INPUT
expect(c.i18n.l10n("hello!", "fa", "Arab")).to eq "&#x61c;hello!&#x61c;"
end


private

def mock_i18n
Expand Down

0 comments on commit 42dcc6a

Please sign in to comment.