Skip to content

Commit

Permalink
Support for FastGettext 2.0 (bsc#1121865)
Browse files Browse the repository at this point in the history
(backward compatible with FastGettext 1.6)
  • Loading branch information
lslezak committed Jan 22, 2019
1 parent f4a7f9e commit 611e557
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/build
/.yardoc
/package/*.tar.*
*.pot
8 changes: 4 additions & 4 deletions package/yast2-ruby-bindings.spec
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ BuildRequires: gcc-c++
BuildRequires: yast2-core-devel
BuildRequires: yast2-devtools >= 3.1.10
%if 0%{suse_version} == 1310
BuildRequires: rubygem-fast_gettext
BuildRequires: rubygem-fast_gettext < 3.0
BuildRequires: rubygem-rspec
Requires: rubygem-fast_gettext
Requires: rubygem-fast_gettext < 3.0
%else
BuildRequires: rubygem(%{rb_default_ruby_abi}:fast_gettext)
BuildRequires: rubygem(%{rb_default_ruby_abi}:fast_gettext) < 3.0
BuildRequires: rubygem(%{rb_default_ruby_abi}:rspec)
Requires: rubygem(%{rb_default_ruby_abi}:fast_gettext)
Requires: rubygem(%{rb_default_ruby_abi}:fast_gettext) < 3.0
%endif
BuildRequires: ruby-devel
Requires: yast2-core >= 3.2.2
Expand Down
1 change: 1 addition & 0 deletions src/ruby/yast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
require "yast/path"
require "yast/scr"
require "yast/term"
require "yast/translation"
require "yast/ui"
require "yast/ui_shortcuts"
require "yast/wfm"
Expand Down
2 changes: 1 addition & 1 deletion src/ruby/yast/builtins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ def self.dpgettext(domain, dirname, text)
@textdomain_mapping[domain.dup] = dirname.dup
end
FastGettext.text_domain = domain
return FastGettext::Translation._(text)
return Translation._(text)
ensure
FastGettext.text_domain = old_text_domain
end
Expand Down
6 changes: 4 additions & 2 deletions src/ruby/yast/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "fast_gettext"
require "logger"

require "yast/translation"

module Yast
# Provides translation wrapper.
module I18n
Expand Down Expand Up @@ -58,7 +60,7 @@ def _(str)
key_exist?(str)
end
end
found ? FastGettext::Translation._(str) : str
found ? Translation._(str) : str
end

# No translation, only marks the text to be found by gettext when creating POT file,
Expand Down Expand Up @@ -120,7 +122,7 @@ def n_(singular, plural, num)
cached_plural_find(singular, plural)
end
end
found ? FastGettext::Translation.n_(singular, plural, num) : fallback_n_(singular, plural, num)
found ? Translation.n_(singular, plural, num) : fallback_n_(singular, plural, num)
end

private
Expand Down
9 changes: 9 additions & 0 deletions src/ruby/yast/translation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "fast_gettext"

module Yast
# Just a wrapper around FastGettext::Translation, we cannot include it
# directly because we define our own _() and n_() methods.
module Translation
extend FastGettext::Translation
end
end
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ foreach(test ${Specs})
endforeach(test)

ADD_TEST("integration" ruby ${CMAKE_CURRENT_SOURCE_DIR}/integration/run.rb)
ADD_TEST("translations" ${CMAKE_CURRENT_SOURCE_DIR}/integration/translations.rb)
2 changes: 1 addition & 1 deletion tests/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module Yast
it "returns the translated string" do
allow(FastGettext).to receive(:cached_plural_find)
.and_return(true)
expect(FastGettext::Translation).to receive(:n_)
expect(Yast::Translation).to receive(:n_)
.with(SINGULAR, PLURAL, 1).and_return(TRANSLATED)
expect(n_(SINGULAR, PLURAL, 1)).to eq(TRANSLATED)
end
Expand Down
Binary file added tests/integration/locale/cs/LC_MESSAGES/example.mo
Binary file not shown.
34 changes: 34 additions & 0 deletions tests/integration/locale/cs/example.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR SuSE Linux Products GmbH, Nuernberg
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Run this command to compile itto an .mo file:
# msgfmt -o LC_MESSAGES/example.mo example.po
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-01-22 14:06+0100\n"
"PO-Revision-Date: 2019-01-22 14:06+0100\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"

#. set the Czech language for these tests
#: tests/integration/translations.rb:18 tests/integration/translations.rb:26
msgid "Example"
msgstr "Příklad"

#: tests/integration/translations.rb:22
msgid "%s Example"
msgid_plural "%s Examples"
msgstr[0] "%s Příklad"
msgstr[1] "%s Příklady"
msgstr[2] "%s Příkladů"
71 changes: 71 additions & 0 deletions tests/integration/translations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#! /usr/bin/env rspec

require_relative "../test_helper"

require "yast"

# set the Czech language for these tests
ENV["LC_ALL"] = "cs_CZ.UTF-8"

class TranslationExample
include Yast::I18n

def initialize
textdomain "example"
end

def translation_simple
_("Example")
end

def translation_plural(n)
format(n_("%s Example", "%s Examples", n), n)
end

def translation_mark
N_("Example")
end

def translation_mark_plural(n)
Nn_("%s Example", "%s Examples", n)
end
end

describe "translations in YaST" do
subject { TranslationExample.new }

before do
# override the default path with translations
stub_const("Yast::I18n::LOCALE_DIR", File.expand_path("../locale", __FILE__))
end

it "translates string using _()" do
expect(subject.translation_simple).to eq("Příklad")
end

it "translates plural string using n_() with 0 value" do
expect(subject.translation_plural(0)).to eq("0 Příkladů")
end

it "translates plural string using n_() with 1 value" do
expect(subject.translation_plural(1)).to eq("1 Příklad")
end

it "translates plural string using n_() with 2 value" do
expect(subject.translation_plural(2)).to eq("2 Příklady")
end

it "translates plural string using n_() with 5 value" do
expect(subject.translation_plural(5)).to eq("5 Příkladů")
end


it "does not translate string using N_()" do
expect(subject.translation_mark).to eq("Example")
end

it "does not translate string using Nn_()" do
expect(subject.translation_mark_plural(0)).to eq(["%s Example", "%s Examples", 0])
end

end

0 comments on commit 611e557

Please sign in to comment.