Commits

Carl Meyer  committed 416ce62

Fix detection of locales with >2 letter codes. Fixes BB-38.

  • Participants
  • Parent commits 0750050

Comments (0)

Files changed (3)

File docs/history.rst

 ---------
 
 Tip: (unreleased)
+  * Fix detection of locales with >2 letter codes. Fixes #38.
 
 Release 2.0.1: (2013-09-19)
   * Change canonical documentation links from

File localeurl/tests/tests.py

 from localeurl.tests import test_utils
 
 
-
 def settings_fixture(mgr):
     mgr.set(
-        USE_I18N = True,
-        LANGUAGES = (
+        USE_I18N=True,
+        LANGUAGES=(
             ('en', 'English'),
             ('nl-nl', 'Dutch'),
             ('nl-be', 'Flemish'),
             ('fr', 'French'),
             ('pt', 'Portuguese'),
             ('pt-br', 'Brazilian Portuguese'),
+            ('fi', 'Finnish'),
+            ('fil', 'Filipino'),
         ),
-        LANGUAGE_CODE = 'en-gb',
-        LOCALE_INDEPENDENT_PATHS = (
+        LANGUAGE_CODE='en-gb',
+        LOCALE_INDEPENDENT_PATHS=(
             re.compile('^/$'),
             '^/test/independent/',
         ),
-        LOCALE_INDEPENDENT_MEDIA_URL = True,
-        MEDIA_URL = '/media/',
-        LOCALE_INDEPENDENT_STATIC_URL = True,
-        STATIC_URL = '/static/',
-        TEMPLATE_CONTEXT_PROCESSORS = (
+        LOCALE_INDEPENDENT_MEDIA_URL=True,
+        MEDIA_URL='/media/',
+        LOCALE_INDEPENDENT_STATIC_URL=True,
+        STATIC_URL='/static/',
+        TEMPLATE_CONTEXT_PROCESSORS=(
             'django.core.context_processors.i18n',
         ),
     )
 
 
-
 class LocaleurlTestCase(TestCase):
     urls = 'localeurl.tests.test_urls'
 
-
     def setUp(self):
         self.settings_manager = test_utils.TestSettingsManager()
         settings_fixture(self.settings_manager)
         translation.activate("en")
         reload(localeurl_settings)
 
-
     def tearDown(self):
         self.settings_manager.revert()
         reload(localeurl_settings)
 
 
-
 class UtilsTestCase(LocaleurlTestCase):
     def test_is_locale_independent(self):
         self.assertFalse(utils.is_locale_independent('/fr/about'))
         self.assertTrue(utils.is_locale_independent('/static/img/logo.png'))
         self.assertTrue(utils.is_locale_independent('/'))
         self.assertTrue(utils.is_locale_independent(
-                '/test/independent/bla/bla'))
-
+            '/test/independent/bla/bla'))
 
     def test_strip_path(self):
         self.assertEqual(('', '/'), utils.strip_path('/'))
         self.assertEqual(('', '/about/'), utils.strip_path('/about/'))
         self.assertEqual(('', '/about/localeurl/'),
-                utils.strip_path('/about/localeurl/'))
+                         utils.strip_path('/about/localeurl/'))
         self.assertEqual(('fr', '/about/localeurl/'),
-                utils.strip_path('/fr/about/localeurl/'))
+                         utils.strip_path('/fr/about/localeurl/'))
         self.assertEqual(('nl-be', '/about/localeurl/'),
-                utils.strip_path('/nl-be/about/localeurl/'))
+                         utils.strip_path('/nl-be/about/localeurl/'))
         self.assertEqual(('', '/de/about/localeurl/'),
-                utils.strip_path('/de/about/localeurl/'))
-
+                         utils.strip_path('/de/about/localeurl/'))
 
     def test_strip_path_takes_longer_code_first(self):
         # Refs issue #15.
         self.assertEqual(('pt-br', '/about/localeurl/'),
-                utils.strip_path('/pt-br/about/localeurl/'))
-
+                         utils.strip_path('/pt-br/about/localeurl/'))
 
     def test_supported_language(self):
         self.assertEqual('fr', utils.supported_language('fr'))
         self.assertEqual('nl-be', utils.supported_language('nl-be'))
         self.assertEqual('en', utils.supported_language('en-gb'))
+        self.assertEqual('fil', utils.supported_language('fil-foo'))
         self.assertEqual(None, utils.supported_language('de'))
 
-
     def test_is_default_locale(self):
         self.assertTrue(utils.is_default_locale('en'))
         self.assertFalse(utils.is_default_locale('en-gb'))
         self.assertFalse(utils.is_default_locale('fr'))
         self.assertFalse(utils.is_default_locale('de'))
 
-
     def test_locale_path(self):
         self.assertEqual('/en/about/localeurl/',
                 utils.locale_path('/about/localeurl/'))

File localeurl/utils.py

 from django.core import urlresolvers
 from localeurl import settings as localeurl_settings
 
+
 def is_locale_independent(path):
     """
     Returns whether the path is locale-independent.
     """
     if (localeurl_settings.LOCALE_INDEPENDENT_MEDIA_URL and
-        settings.MEDIA_URL and
-        path.startswith(settings.MEDIA_URL)):
+            settings.MEDIA_URL and
+            path.startswith(settings.MEDIA_URL)):
         return True
     if (localeurl_settings.LOCALE_INDEPENDENT_STATIC_URL and
-        getattr(settings, "STATIC_URL", None) and
-        path.startswith(settings.STATIC_URL)):
+            getattr(settings, "STATIC_URL", None) and
+            path.startswith(settings.STATIC_URL)):
         return True
     for regex in localeurl_settings.LOCALE_INDEPENDENT_PATHS:
         if regex.search(path):
             return True
     return False
 
+
 def strip_path(path):
     """
     Separates the locale prefix from the rest of the path. If the path does not
             return check.group('locale'), path_info
     return '', path
 
+
 def supported_language(locale):
     """
     Returns the supported language (from settings.LANGUAGES) for the locale.
     """
     locale = locale.lower()
+    bits = locale.split('-')
     if locale in localeurl_settings.SUPPORTED_LOCALES:
         return locale
-    elif locale[:2] in localeurl_settings.SUPPORTED_LOCALES:
-        return locale[:2]
+    elif bits[0] in localeurl_settings.SUPPORTED_LOCALES:
+        return bits[0]
     else:
         return None
 
+
 def is_default_locale(locale):
     """
     Returns whether the locale is the default locale.
     """
     return locale == supported_language(settings.LANGUAGE_CODE)
 
+
 def locale_path(path, locale=''):
     """
     Generate the localeurl-enabled path from a path without locale prefix. If
     else:
         return ''.join([u'/', locale, path])
 
+
 def locale_url(path, locale=''):
     """
     Generate the localeurl-enabled URL from a path without locale prefix. If
     path = locale_path(path, locale)
     return add_script_prefix(path)
 
+
 def strip_script_prefix(url):
     """
     Strips the SCRIPT_PREFIX from the URL. Because this function is meant for
     pos = len(urlresolvers.get_script_prefix()) - 1
     return url[:pos], url[pos:]
 
+
 def add_script_prefix(path):
     """
     Prepends the SCRIPT_PREFIX to a path.