Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into exception_in_import
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Jan 4, 2017
2 parents 086ebf3 + 7ee159e commit 54c9160
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 12 deletions.
7 changes: 7 additions & 0 deletions package/yast2-ruby-bindings.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Thu Dec 15 16:29:13 UTC 2016 - igonzalezsosa@suse.com

- Do not crash when FastGettext is unable to find the empty.mo
file (bsc#1014458)
- 3.2.3

-------------------------------------------------------------------
Tue Nov 15 10:34:51 UTC 2016 - lslezak@suse.cz

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-ruby-bindings.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2-ruby-bindings
Version: 3.2.2
Version: 3.2.3
Url: https://github.com/yast/yast-ruby-bindings
Release: 0
BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down
60 changes: 49 additions & 11 deletions src/ruby/yast/i18n.rb
@@ -1,4 +1,5 @@
require "fast_gettext"
require "logger"

module Yast
# Provides translation wrapper.
Expand Down Expand Up @@ -44,16 +45,16 @@ def _(str)
# no textdomain configured yet
return str unless @my_textdomain

found = true
# Switching textdomain clears gettext caches so avoid it if possible.
if !@my_textdomain.include?(FastGettext.text_domain) ||
!FastGettext.key_exist?(str)
if !@my_textdomain.include?(FastGettext.text_domain) || !key_exist?(str)
# Set domain where key is defined.
@my_textdomain.each do |domain|
found = @my_textdomain.any? do |domain|
FastGettext.text_domain = domain
break if FastGettext.key_exist?(str)
key_exist?(str)
end
end
FastGettext::Translation._ str
found ? FastGettext::Translation._(str) : str
end

# No translation, only marks the text to be found by gettext when creating POT file,
Expand Down Expand Up @@ -98,19 +99,19 @@ def Nn_(*keys)
# @param (String) plural text for translators for bigger value
def n_(singular, plural, num)
# no textdomain configured yet
return (num == 1) ? singular : plural unless @my_textdomain
return fallback_n_(singular, plural, num) unless @my_textdomain

# Switching textdomain clears gettext caches so avoid it if possible.
# difference between _ and n_ is hat we need special cache for plural forms
if !@my_textdomain.include?(FastGettext.text_domain) ||
!FastGettext.cached_plural_find(singular, plural)
found = true
if !@my_textdomain.include?(FastGettext.text_domain) || !cached_plural_find(singular, plural)
# Set domain where key is defined.
@my_textdomain.each do |domain|
found = @my_textdomain.any? do |domain|
FastGettext.text_domain = domain
break if FastGettext.cached_plural_find(singular, plural)
cached_plural_find(singular, plural)
end
end
FastGettext::Translation.n_(singular, plural, num)
found ? FastGettext::Translation.n_(singular, plural, num) : fallback_n_(singular, plural, num)
end

private
Expand Down Expand Up @@ -141,5 +142,42 @@ def current_language

lang
end

# Determines whether a key exist in the current textdomain
#
# It wraps FastGettext.key_exist? and logs Errno::ENOENT errors.
#
# @return [Boolean] true if it exists; false otherwise.
# @see FastGettext.key_exist?
def key_exist?(key)
FastGettext.key_exist?(key)
rescue Errno::ENOENT => error
Yast.y2warning("File not found when translating '#{key}' on textdomain #{@my_textdomain}'. "\
"Error: #{error}. Backtrace: #{error.backtrace}")
false
end

# Determines whether a plural is cached in the current textdomain
#
# It wraps FastGettext.cached_plural_find and logs Errno::ENOENT errors.
#
# @return [Boolean] true if it exists; false otherwise.
# @see FastGettext.cached_plural_find
def cached_plural_find(singular, plural)
FastGettext.cached_plural_find(singular, plural)
rescue Errno::ENOENT => error
Yast.y2warning("File not found when translating '#{singular}/#{plural}' "\
"on textdomain #{@my_textdomain}'. Error: #{error}. Backtrace: #{error.backtrace}")
false
end

# Returns the singular or the plural form depending on a number
#
# It's used as a fallback to {n_}.
#
# @return [String] {singular} if {num} == 1; {plural} otherwise.
def fallback_n_(singular, plural, num)
(num == 1) ? singular : plural
end
end
end
73 changes: 73 additions & 0 deletions tests/i18n_spec.rb
Expand Up @@ -24,5 +24,78 @@ module Yast
expect(Nn_(singular, plural, count)).to eq [singular, plural, count]
end
end

describe "._" do
TRANSLATED = "translated".freeze
SINGULAR = "untranslated".freeze
PLURAL = "plural".freeze

before do
allow(File).to receive(:exist?).with(Yast::I18n::LOCALE_DIR)
.and_return(true)
textdomain("base")
end

it "returns the translated string" do
allow(FastGettext).to receive(:key_exist?).and_return(true)
expect(FastGettext::Translation).to receive(:_).with(SINGULAR)
.and_return(TRANSLATED)
expect(_(SINGULAR)).to eq(TRANSLATED)
end

context "when FastGettext throws an Errno::ENOENT exception" do
before do
allow(FastGettext).to receive(:key_exist?)
.and_raise(Errno::ENOENT)
end

it "returns the untranslated string" do
expect(_(SINGULAR)).to eq(SINGULAR)
end

it "logs a warning message" do
expect(Yast).to receive(:y2warning).at_least(1)
.with(/File not found/)
_(SINGULAR)
end
end
end

describe ".n_" do
before do
allow(File).to receive(:exist?).with(Yast::I18n::LOCALE_DIR)
.and_return(true)
textdomain("base")
end

it "returns the translated string" do
allow(FastGettext).to receive(:cached_plural_find)
.and_return(true)
expect(FastGettext::Translation).to receive(:n_)
.with(SINGULAR, PLURAL, 1).and_return(TRANSLATED)
expect(n_(SINGULAR, PLURAL, 1)).to eq(TRANSLATED)
end

context "when FastGettext throws an Errno::ENOENT exception" do
before do
allow(FastGettext).to receive(:cached_plural_find)
.and_raise(Errno::ENOENT)
end

it "returns the singular untranslated string if num is 1" do
expect(n_(SINGULAR, PLURAL, 1)).to eq(SINGULAR)
end

it "returns the plural untranslated string if num > 1" do
expect(n_(SINGULAR, PLURAL, 2)).to eq(PLURAL)
end

it "logs a warning message" do
expect(Yast).to receive(:y2warning).at_least(1)
.with(/File not found/)
expect(n_(SINGULAR, PLURAL, 1)).to eq(SINGULAR)
end
end
end
end
end

0 comments on commit 54c9160

Please sign in to comment.