Repository: joke2k/faker
Pull request #425 (2016-11-16 11:01:26)

Improve the method `locale`


Patch:
diff --git a/faker/providers/misc/__init__.py b/faker/providers/misc/__init__.py
index 2cd1d981..dbd19b3d 100644
--- a/faker/providers/misc/__init__.py
+++ b/faker/providers/misc/__init__.py
@@ -13,7 +13,65 @@ from .. import BaseProvider
 
 
 class Provider(BaseProvider):
-    language_codes = ('zh', 'de', 'el', 'en', 'es', 'fr', 'it', 'pt', 'ru')
+    # Locales supported by Linux Mint from `/usr/share/i18n/SUPPORTED`
+    language_locale_codes = {
+        'aa': ('DJ', 'ER', 'ET'), 'af': ('ZA',), 'ak': ('GH',), 'am': ('ET',),
+        'an': ('ES',), 'apn': ('IN',),
+        'ar': ('AE', 'BH', 'DZ', 'EG', 'IN', 'IQ', 'JO', 'KW', 'LB', 'LY',
+               'MA', 'OM', 'QA', 'SA', 'SD', 'SS', 'SY', 'TN', 'YE'),
+        'as': ('IN',), 'ast': ('ES',), 'ayc': ('PE',), 'az': ('AZ', 'IN'),
+        'be': ('BY',), 'bem': ('ZM',), 'ber': ('DZ', 'MA'), 'bg': ('BG',),
+        'bhb': ('IN',), 'bho': ('IN',), 'bn': ('BD', 'IN'), 'bo': ('CN', 'IN'),
+        'br': ('FR',), 'brx': ('IN',), 'bs': ('BA',), 'byn': ('ER',),
+        'ca': ('AD', 'ES', 'FR', 'IT'), 'ce': ('RU',), 'ckb': ('IQ',),
+        'cmn': ('TW',), 'crh': ('UA',), 'cs': ('CZ',), 'csb': ('PL',),
+        'cv': ('RU',), 'cy': ('GB',), 'da': ('DK',),
+        'de': ('AT', 'BE', 'CH', 'DE', 'LI', 'LU'), 'doi': ('IN',),
+        'dv': ('MV',), 'dz': ('BT',), 'el': ('GR', 'CY'),
+        'en': ('AG', 'AU', 'BW', 'CA', 'DK', 'GB', 'HK', 'IE', 'IN', 'NG',
+               'NZ', 'PH', 'SG', 'US', 'ZA', 'ZM', 'ZW'),
+        'eo': ('US',),
+        'es': ('AR', 'BO', 'CL', 'CO', 'CR', 'CU', 'DO', 'EC', 'ES', 'GT',
+               'HN', 'MX', 'NI', 'PA', 'PE', 'PR', 'PY', 'SV', 'US', 'UY', 'VE'
+               ), 'et': ('EE',), 'eu': ('ES', 'FR'), 'fa': ('IR',),
+        'ff': ('SN',), 'fi': ('FI',), 'fil': ('PH',), 'fo': ('FO',),
+        'fr': ('CA', 'CH', 'FR', 'LU'), 'fur': ('IT',), 'fy': ('NL', 'DE'),
+        'ga': ('IE',), 'gd': ('GB',), 'gez': ('ER', 'ET'), 'gl': ('ES',),
+        'gu': ('IN',), 'gv': ('GB',), 'ha': ('NG',), 'hak': ('TW',),
+        'he': ('IL',), 'hi': ('IN',), 'hne': ('IN',), 'hr': ('HR',),
+        'hsb': ('DE',), 'ht': ('HT',), 'hu': ('HU',), 'hy': ('AM',),
+        'ia': ('FR',), 'id': ('ID',), 'ig': ('NG',), 'ik': ('CA',),
+        'is': ('IS',), 'it': ('CH', 'IT'), 'iu': ('CA',), 'iw': ('IL',),
+        'ja': ('JP',), 'ka': ('GE',), 'kk': ('KZ',), 'kl': ('GL',),
+        'km': ('KH',), 'kn': ('IN',), 'ko': ('KR',), 'kok': ('IN',),
+        'ks': ('IN',), 'ku': ('TR',), 'kw': ('GB',), 'ky': ('KG',),
+        'lb': ('LU',), 'lg': ('UG',), 'li': ('BE', 'NL'), 'lij': ('IT',),
+        'ln': ('CD',), 'lo': ('LA',), 'lt': ('LT',), 'lv': ('LV',),
+        'lzh': ('TW',), 'mag': ('IN',), 'mai': ('IN',), 'mg': ('MG',),
+        'mhr': ('RU',), 'mi': ('NZ',), 'mk': ('MK',), 'ml': ('IN',),
+        'mn': ('MN',), 'mni': ('IN',), 'mr': ('IN',), 'ms': ('MY',),
+        'mt': ('MT',), 'my': ('MM',), 'nan': ('TW',), 'nb': ('NO',),
+        'nds': ('DE', 'NL'), 'ne': ('NP',), 'nhn': ('MX',),
+        'niu': ('NU', 'NZ'), 'nl': ('AW', 'BE', 'NL'), 'nn': ('NO',),
+        'nr': ('ZA',), 'nso': ('ZA',), 'oc': ('FR',), 'om': ('ET', 'KE'),
+        'or': ('IN',), 'os': ('RU',), 'pa': ('IN', 'PK'),
+        'pap': ('AN', 'AW', 'CW'), 'pl': ('PL',), 'ps': ('AF',),
+        'pt': ('BR', 'PT'), 'quz': ('PE',), 'raj': ('IN',), 'ro': ('RO',),
+        'ru': ('RU', 'UA'), 'rw': ('RW',), 'sa': ('IN',), 'sat': ('IN',),
+        'sc': ('IT',), 'sd': ('IN', 'PK'), 'se': ('NO',), 'shs': ('CA',),
+        'si': ('LK',), 'sid': ('ET',), 'sk': ('SK',), 'sl': ('SI',),
+        'so': ('DJ', 'ET', 'KE', 'SO'), 'sq': ('AL', 'ML'), 'sr': ('ME', 'RS'),
+        'ss': ('ZA',), 'st': ('ZA',), 'sv': ('FI', 'SE'), 'sw': ('KE', 'TZ'),
+        'szl': ('PL',), 'ta': ('IN', 'LK'), 'tcy': ('IN',), 'te': ('IN',),
+        'tg': ('TJ',), 'th': ('TH',), 'the': ('NP',), 'ti': ('ER', 'ET'),
+        'tig': ('ER',), 'tk': ('TM',), 'tl': ('PH',), 'tn': ('ZA',),
+        'tr': ('CY', 'TR'), 'ts': ('ZA',), 'tt': ('RU',), 'ug': ('CN',),
+        'uk': ('UA',), 'unm': ('US',), 'ur': ('IN', 'PK'), 'uz': ('UZ',),
+        've': ('ZA',), 'vi': ('VN',), 'wa': ('BE',), 'wae': ('CH',),
+        'wal': ('ET',), 'wo': ('SN',), 'xh': ('ZA',), 'yi': ('US',),
+        'yo': ('NG',), 'yue': ('HK',), 'zh': ('CN', 'HK', 'SG', 'TW'),
+        'zu': ('ZA',)
+    }
 
     @classmethod
     def boolean(cls, chance_of_getting_true=50):
@@ -68,8 +126,12 @@ class Provider(BaseProvider):
             return res.digest()
         return res.hexdigest()
 
-    def locale(self):
-        return self.language_code() + '_' + self.country_code()
+    @classmethod
+    def locale(cls):
+        language_code = cls.language_code()
+        return language_code + '_' + cls.random_element(
+            cls.language_locale_codes[language_code]
+        )
 
     @classmethod
     def country_code(cls):
@@ -77,7 +139,7 @@ class Provider(BaseProvider):
 
     @classmethod
     def language_code(cls):
-        return cls.random_element(cls.language_codes)
+        return cls.random_element(cls.language_locale_codes.keys())
 
     @classmethod
     def uuid4(cls):
@@ -126,4 +188,4 @@ class Provider(BaseProvider):
         for i, index in enumerate(random_indexes):
             chars[index] = required_tokens[i]
 
-        return ''.join(chars)
\ No newline at end of file
+        return ''.join(chars)


Test patch:
diff --git a/faker/tests/__init__.py b/faker/tests/__init__.py
index 214bf5f1..546a2758 100644
--- a/faker/tests/__init__.py
+++ b/faker/tests/__init__.py
@@ -465,7 +465,14 @@ class FactoryTestCase(unittest.TestCase):
         for _ in range(99):
             language_code = Provider.language_code()
             self.assertTrue(isinstance(language_code, string_types))
-            Factory.create(locale=language_code)
+            self.assertTrue(re.match(r'^[a-z]{2,3}$', language_code))
+
+    def test_locale(self):
+        from faker.providers.misc import Provider
+
+        for _ in range(99):
+            locale = Provider.locale()
+            self.assertTrue(re.match(r'^[a-z]{2,3}_[A-Z]{2}$', locale))
 
     def test_password(self):
         from faker.providers.misc import Provider