From 0f10aacae682feb48bbb55b24172173ac332ef7b Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Mon, 12 Dec 2011 17:55:16 +0100 Subject: [PATCH] Add support for i18n_plural(): Translation of plural forms To recognize our custom i18n_plural keyword in poedit, use this syntax: i18n_plural:1,2 We also need to set a "plural form" in poedit. Example for German: nplurals=2; plural=n != 1; --- src/i18n.h | 1 + test/test_i18n.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 0 deletions(-) diff --git a/src/i18n.h b/src/i18n.h index e388fd0..d23f07b 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -32,6 +32,7 @@ on this file might be covered by the GNU General Public License. #include #define i18n(String) gettext(String) +#define i18n_plural(String1, String2, Count) ngettext(String1, String2, Count) #define i18n_noop(String) (String) void i18n_init(const std::string& domain, const std::string& path="."); diff --git a/test/test_i18n.cpp b/test/test_i18n.cpp index c7fa4d1..c43ea7d 100644 --- a/test/test_i18n.cpp +++ b/test/test_i18n.cpp @@ -97,4 +97,36 @@ BOOST_AUTO_TEST_CASE(StdStringInput) BOOST_CHECK_EQUAL("dummy test string", result); } +BOOST_AUTO_TEST_CASE(PluralString0) +{ + // Zero elements get the plural message (in the default "plural" configuration) + BOOST_CHECK_EQUAL("xxx new messages", i18n_plural("One new message", "xxx new messages", 0)); +} + +BOOST_AUTO_TEST_CASE(PluralString1) +{ + BOOST_CHECK_EQUAL("One new message", i18n_plural("One new message", "xxx new messages", 1)); +} + +BOOST_AUTO_TEST_CASE(PluralString2) +{ + BOOST_CHECK_EQUAL("xxx new messages", i18n_plural("One new message", "xxx new messages", 2)); +} + +BOOST_AUTO_TEST_CASE(PluralStringWithParamter1) +{ + vector data; + data.push_back("1"); + + BOOST_CHECK_EQUAL("1 new message", i18n_get_string(i18n_plural("$0 new message", "$0 new messages", 1), data)); +} + +BOOST_AUTO_TEST_CASE(PluralStringWithParamter2) +{ + vector data; + data.push_back("500"); + + BOOST_CHECK_EQUAL("500 new messages", i18n_get_string(i18n_plural("$0 new message", "$0 new messages", 500), data)); +} + BOOST_AUTO_TEST_SUITE_END() -- 1.7.1