def test_memcache_invalidates_when_the_request_ends(self): translation.activate("de") gettext("Hello World!") # Generates the cache # Wait for any background threads to finish while translations_loading(): pass # Set the invalidation key key = _language_invalidation_key("de") cache.set(key, datetime.datetime.utcnow()) # This shouldn't make a query, the invalidation hasn't applied yet with sleuth.watch("google.appengine.api.datastore.Query.Run") as query: trans = gettext("Goodbye World!") self.assertEqual(trans, "Auf Wiedersehen Welt!") self.assertFalse(query.called) # Run the finished signal invalidate_caches_if_necessary(None) # This should now cause a query with sleuth.watch("google.appengine.api.datastore.Query.Run") as query: trans = gettext("Goodbye World!") self.assertEqual(trans, "Auf Wiedersehen Welt!") self.assertTrue(query.called)
def test_pl_po_manually(self): lang = settings.LANGUAGE_CODE invalidate_language("pl") self.assertEqual(Translation.objects.count(), 0) MasterTranslation.objects.create( language_code=lang, text="%(n)s result", plural_text="%(n)s results", ) MasterTranslation.objects.create( language_code=lang, text="cat", ) mock_file_contents = u'''# Something something # Translators list msgid "" msgstr "" "Project-Id-Version: django\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2015-01-17 11:07+0100\n" "PO-Revision-Date: 2015-01-18 15:19+0000\n" "Last-Translator: Janusz Harkot <*****@*****.**>\n" "Language-Team: Polish (http://www.transifex.com/projects/p/django/language/" "pl/)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: pl\n" "Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 " "|| n%100>=20) ? 1 : 2);\n" #, python-format msgid "%(n)s result" msgid_plural "%(n)s results" msgstr[0] "%(n)s wynik" msgstr[1] "%(n)s wyniki" msgstr[2] "%(n)s wyników" #, python-format msgid "cat" msgstr "kot" ''' #msgctxt "context hint" import_translations_from_po(mock_file_contents, "pl", lang) translation.activate("pl") self.assertEqual(ngettext("%(n)s result", "", 0).decode('utf-8'), u"%(n)s wyników") self.assertEqual(ngettext("%(n)s result", "", 1).decode('utf-8'), u"%(n)s wynik") self.assertEqual(ngettext("%(n)s result", "", 2).decode('utf-8'), u"%(n)s wyniki") self.assertEqual(ngettext("%(n)s result", "", 5).decode('utf-8'), u"%(n)s wyników") # Singlar translation test self.assertEqual(gettext("cat"), u"kot") # This form is wrong because po don't support the fraction plural form! self.assertEqual(ngettext("%(n)s result", "", 0.5).decode('utf-8'), u"%(n)s wyników") # u") self.assertEqual(get_plural_index("pl", 0), cldr.MANY) self.assertEqual(get_plural_index("pl", 1), cldr.ONE) self.assertEqual(get_plural_index("pl", 2), cldr.FEW) self.assertEqual(get_plural_index("pl", 5), cldr.MANY) self.assertEqual(get_plural_index("pl", 1.1), cldr.OTHER)
def test_languages_cached(self): # This should make a query, because the translations were invalidated translation.activate("de") with sleuth.watch("google.appengine.api.datastore.Query.Run") as query: trans = gettext("Hello World!") self.assertEqual(trans, "Hallo Welt!") self.assertTrue(query.called) # Wait for any background threads to finish while translations_loading(): pass # This shouldn't make a query with sleuth.watch("google.appengine.api.datastore.Query.Run") as query: trans = gettext("Goodbye World!") self.assertEqual(trans, "Auf Wiedersehen Welt!") self.assertFalse(query.called)
def test_gettext(self): translation.activate("es") trans = gettext("Hello World!") self.assertEqual(trans, "Hola Mundo!") translation.activate("de") trans = gettext("Hello World!") self.assertEqual(trans, "Hallo Welt!") translation.activate("en") trans = gettext("Hello World!") self.assertEqual(trans, "Hello World!") # Untranslated translation.activate("fr") trans = gettext("Hello World!") self.assertEqual(trans, "Hello World!")
def test_pl_arb_manually(self): lang = settings.LANGUAGE_CODE invalidate_language("pl") self.assertEqual(Translation.objects.count(), 0) pk1 = MasterTranslation.objects.create( language_code=lang, text="result", plural_text="results", ).pk pk2 = MasterTranslation.objects.create( language_code=lang, text="cat", ).pk data = { "@@locale": "pl", # two form isn't needed or used by the pl lookup function so it should be ignored pk1: u"{NUM, plural, one {wynik} few {wyniki} two {blabla} many {wyników} other {wyniku}}", "@" + pk1: { "context": "", "source_text": "result", "type": "text" }, pk2: u"kot", "@" + pk2: { "context": "", "source_text": "cat", "type": "text" }, } mock_file = StringIO(json.dumps(data)) import_translations_from_arb(mock_file, "pl") translation.activate("pl") self.assertEqual(ngettext("result", "", 0).decode('utf-8'), u"wyników") self.assertEqual(ngettext("result", "", 1).decode('utf-8'), u"wynik") self.assertEqual(ngettext("result", "", 2).decode('utf-8'), u"wyniki") self.assertEqual(ngettext("result", "", 5).decode('utf-8'), u"wyników") self.assertEqual( ngettext("result", "", 0.5).decode('utf-8'), u"wyniku") # Singlar translation test self.assertEqual(gettext("cat"), u"kot") self.assertEqual(get_plural_index("pl", 0), cldr.MANY) self.assertEqual(get_plural_index("pl", 1), cldr.ONE) self.assertEqual(get_plural_index("pl", 2), cldr.FEW) self.assertEqual(get_plural_index("pl", 5), cldr.MANY) self.assertEqual(get_plural_index("pl", 1.1), cldr.OTHER)
def test_pl_arb_manually(self): lang = settings.LANGUAGE_CODE invalidate_language("pl") self.assertEqual(Translation.objects.count(), 0) pk1 = MasterTranslation.objects.create( language_code=lang, text="result", plural_text="results", ).pk pk2 = MasterTranslation.objects.create( language_code=lang, text="cat", ).pk data = { "@@locale": "pl", # two form isn't needed or used by the pl lookup function so it should be ignored pk1: u"{NUM, plural, one {wynik} few {wyniki} two {blabla} many {wyników} other {wyniku}}", "@"+pk1: { "context": "", "source_text": "result", "type": "text" }, pk2: u"kot", "@"+pk2: { "context": "", "source_text": "cat", "type": "text" }, } mock_file = StringIO(json.dumps(data)) import_translations_from_arb(mock_file, "pl") translation.activate("pl") self.assertEqual(ngettext("result", "", 0).decode('utf-8'), u"wyników") self.assertEqual(ngettext("result", "", 1).decode('utf-8'), u"wynik") self.assertEqual(ngettext("result", "", 2).decode('utf-8'), u"wyniki") self.assertEqual(ngettext("result", "", 5).decode('utf-8'), u"wyników") self.assertEqual(ngettext("result", "", 0.5).decode('utf-8'), u"wyniku") # Singlar translation test self.assertEqual(gettext("cat"), u"kot") self.assertEqual(get_plural_index("pl", 0), cldr.MANY) self.assertEqual(get_plural_index("pl", 1), cldr.ONE) self.assertEqual(get_plural_index("pl", 2), cldr.FEW) self.assertEqual(get_plural_index("pl", 5), cldr.MANY) self.assertEqual(get_plural_index("pl", 1.1), cldr.OTHER)
def test_pl_po_manually(self): lang = settings.LANGUAGE_CODE invalidate_language("pl") self.assertEqual(Translation.objects.count(), 0) MasterTranslation.objects.create( language_code=lang, text="%(n)s result", plural_text="%(n)s results", ) MasterTranslation.objects.create( language_code=lang, text="cat", ) mock_file_contents = u'''# Something something # Translators list msgid "" msgstr "" "Project-Id-Version: django\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2015-01-17 11:07+0100\n" "PO-Revision-Date: 2015-01-18 15:19+0000\n" "Last-Translator: Janusz Harkot <*****@*****.**>\n" "Language-Team: Polish (http://www.transifex.com/projects/p/django/language/" "pl/)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: pl\n" "Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 " "|| n%100>=20) ? 1 : 2);\n" #, python-format msgid "%(n)s result" msgid_plural "%(n)s results" msgstr[0] "%(n)s wynik" msgstr[1] "%(n)s wyniki" msgstr[2] "%(n)s wyników" #, python-format msgid "cat" msgstr "kot" ''' #msgctxt "context hint" import_translations_from_po(mock_file_contents, "pl", lang) translation.activate("pl") self.assertEqual( ngettext("%(n)s result", "", 0).decode('utf-8'), u"%(n)s wyników") self.assertEqual( ngettext("%(n)s result", "", 1).decode('utf-8'), u"%(n)s wynik") self.assertEqual( ngettext("%(n)s result", "", 2).decode('utf-8'), u"%(n)s wyniki") self.assertEqual( ngettext("%(n)s result", "", 5).decode('utf-8'), u"%(n)s wyników") # Singlar translation test self.assertEqual(gettext("cat"), u"kot") # This form is wrong because po don't support the fraction plural form! self.assertEqual( ngettext("%(n)s result", "", 0.5).decode('utf-8'), u"%(n)s wyników") # u") self.assertEqual(get_plural_index("pl", 0), cldr.MANY) self.assertEqual(get_plural_index("pl", 1), cldr.ONE) self.assertEqual(get_plural_index("pl", 2), cldr.FEW) self.assertEqual(get_plural_index("pl", 5), cldr.MANY) self.assertEqual(get_plural_index("pl", 1.1), cldr.OTHER)