Add support for i18n_plural(): Translation of plural forms
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 12 Dec 2011 16:55:16 +0000 (17:55 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 12 Dec 2011 16:55:16 +0000 (17:55 +0100)
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
test/test_i18n.cpp

index e388fd0..d23f07b 100644 (file)
@@ -32,6 +32,7 @@ on this file might be covered by the GNU General Public License.
 #include <vector>
 
 #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=".");
index c7fa4d1..c43ea7d 100644 (file)
@@ -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<string> 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<string> 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()