]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Read locale from .po if it has Language header 420/head
authorKenny Root <kenny@the-b.org>
Fri, 17 Jun 2016 06:11:56 +0000 (23:11 -0700)
committerKenny Root <kenny@the-b.org>
Sat, 18 Jun 2016 00:12:31 +0000 (17:12 -0700)
When a .po file was written out it would write the "Language"
header, but it wouldn't subsequently read that header back in. This
change makes that symmetric so you can write out a file and get back the
same Catalog by reading it in again.

Additionally other values like charset get overridden when the header is
read, so codify the behavior that the read "Language" header gets set as
the locale of the Catalog object.

babel/messages/catalog.py
tests/messages/test_pofile.py

index aeb9a9e406838499d4e02c2e54a6a199ad57bcaa..84cee432f6ce2aaa49e66358e571abe8ef4ac7d0 100644 (file)
@@ -402,6 +402,8 @@ class Catalog(object):
                 self.msgid_bugs_address = value
             elif name == 'last-translator':
                 self.last_translator = value
+            elif name == 'language':
+                self.locale = Locale.parse(value)
             elif name == 'language-team':
                 self.language_team = value
             elif name == 'content-type':
index 1bac3603c2759c6218b87087e14fe5d5e55877eb..b453076641fe8a9fa2776f16b0a9b40ddd46511a 100644 (file)
@@ -29,6 +29,14 @@ msgstr "Voh"''')
         catalog = pofile.read_po(buf, locale='en_US')
         self.assertEqual(Locale('en', 'US'), catalog.locale)
 
+    def test_locale_gets_overridden_by_file(self):
+        buf = StringIO(r'''
+msgid ""
+msgstr ""
+"Language: en_US\n"''')
+        catalog = pofile.read_po(buf, locale='de')
+        self.assertEqual(Locale('en', 'US'), catalog.locale)
+
     def test_preserve_domain(self):
         buf = StringIO(r'''msgid "foo"
 msgstr "Voh"''')
@@ -112,6 +120,7 @@ msgstr ""
 "POT-Creation-Date: 2007-09-27 11:19+0700\n"
 "PO-Revision-Date: 2007-09-27 21:42-0700\n"
 "Last-Translator: John <cleese@bavaria.de>\n"
+"Language: de\n"
 "Language-Team: German Lang <de@babel.org>\n"
 "Plural-Forms: nplurals=2; plural=(n != 1)\n"
 "MIME-Version: 1.0\n"
@@ -128,6 +137,7 @@ msgstr ""
                                   tzinfo=FixedOffsetTimezone(7 * 60)),
                          catalog.creation_date)
         self.assertEqual(u'John <cleese@bavaria.de>', catalog.last_translator)
+        self.assertEqual(Locale('de'), catalog.locale)
         self.assertEqual(u'German Lang <de@babel.org>', catalog.language_team)
         self.assertEqual(u'iso-8859-2', catalog.charset)
         self.assertEqual(True, list(catalog)[0].fuzzy)