Commit 95b0c60f authored by Claude Paroz's avatar Claude Paroz

Fix language_from_ianacode method (#664197)

parent c81332a9
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext as _
from teams.models import Team, FakeTeam
......@@ -20,9 +21,10 @@ class Language(models.Model):
""" Return a matching Language object corresponding to LANGUAGE_CODE (BCP47-formatted)
This is a sort of BCP47 to ISO639 conversion function """
iana_splitted = ianacode.split("-", 1)
lang_code = iana_splitted[0]
iana_suffix = len(iana_splitted) > 1 and iana_splitted[1] or ""
iana_suffix = iana_suffix.replace('Latn','latin').replace('Cyrl','cyrillic')
lang_list = cls.objects.filter(locale__startswith=iana_splitted[0])
lang_list = cls.objects.filter(Q(locale=lang_code) | Q(locale__regex=r'^%s[@_].+' % lang_code))
if len(lang_list) == 0:
return None
elif len(lang_list) > 1:
......
......@@ -21,9 +21,23 @@
from django.core.urlresolvers import reverse
from django.test import TestCase
from languages.models import Language
class LanguageTestCase(TestCase):
fixtures = ['sample_data.json']
def testLanguageReleaseXML(self):
response = self.client.get(reverse("languages.views.language_release_xml", args=['fr', 'gnome-2-30']))
self.assertContains(response, """<stats language="fr" release="gnome-2-30">""")
def test_language_from_ianacode(self):
Language.objects.create(name='Belarussian', locale='be')
Language.objects.create(name='French (Belgium)', locale='fr_BE')
Language.objects.create(name='Chinese (Taiwan)', locale='zh_TW')
self.assertEqual(Language.get_language_from_ianacode('fr-ch').locale, 'fr')
self.assertEqual(Language.get_language_from_ianacode('fr-be').locale, 'fr_BE')
self.assertEqual(Language.get_language_from_ianacode('be').locale, 'be')
self.assertEqual(Language.get_language_from_ianacode('be-latin-RU').locale, 'be')
self.assertEqual(Language.get_language_from_ianacode('zh-tw').locale, 'zh_TW')
self.assertEqual(Language.get_language_from_ianacode('xx'), None)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment