We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Default locale to language if no territory given.
It would be great that if faker was initialized with only a locale and no territory, that it would use  a sensible default.

For example I currently have to do the following if using something such as "en" instead of "en_US".  

``` py
from faker import Factory
from faker import AVAILABLE_LOCALES

locale = 'en'
if locale not in AVAILABLE_LOCALES:
    locale = next(l for l in AVAILABLE_LOCALES if l.startswith(locale))

factory = Factory.create(locale)
```

This happens when using dynamic mock data in local development where django sets the locale to "en" because we do not define territories.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `faker/factory.py`
Content:
```
1 from __future__ import unicode_literals
2 from __future__ import absolute_import
3 import sys
4 from faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES
5 from faker import Generator
6 from faker import providers as providers_mod
7 
8 
9 class Factory(object):
10 
11     @classmethod
12     def create(cls, locale=None, providers=None, generator=None, **config):
13 
14         # fix locale to package name
15         locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE
16         if '_' in locale:
17             locale = locale[:2] + locale[2:].upper()
18         if locale not in AVAILABLE_LOCALES:
19             raise AttributeError('Invalid configuration for faker locale "{0}"'.format(locale))
20 
21         providers = providers or DEFAULT_PROVIDERS
22 
23         faker = generator or Generator(**config)
24         faker.add_provider(providers_mod.BaseProvider)
25         for provider_name in providers:
26 
27             provider_class, lang_found = cls._get_provider_class(provider_name, locale)
28             provider = provider_class(faker)
29             provider.__provider__ = provider_name
30             provider.__lang__ = lang_found
31             faker.add_provider(provider)
32 
33         return faker
34 
35     @classmethod
36     def _get_provider_class(cls, provider, locale=''):
37 
38         provider_class = cls._find_provider_class(provider, locale)
39 
40         if provider_class:
41             return provider_class, locale
42 
43         if locale and locale != DEFAULT_LOCALE:
44             # fallback to default locale
45             provider_class = cls._find_provider_class(provider, DEFAULT_LOCALE)
46             if provider_class:
47                 return provider_class, DEFAULT_LOCALE
48 
49         # fallback to no locale
50         provider_class = cls._find_provider_class(provider)
51         if provider_class:
52             return provider_class, None
53 
54         raise ValueError('Unable to find provider "{0}" with locale "{1}"'.format(provider, locale))
55 
56     @classmethod
57     def _find_provider_class(cls, provider, locale=''):
58 
59         path = "{providers}{lang}.{provider}".format(
60             providers=providers_mod.__package__ or providers_mod.__name__,
61             lang='.' + locale if locale else '',
62             provider=provider
63         )
64 
65         try:
66             __import__(path)
67         except ImportError:
68             return None
69 
70         return sys.modules[path].Provider
71
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/faker/factory.py b/faker/factory.py
--- a/faker/factory.py
+++ b/faker/factory.py
@@ -1,5 +1,6 @@
 from __future__ import unicode_literals
 from __future__ import absolute_import
+import locale as pylocale
 import sys
 from faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES
 from faker import Generator
@@ -13,8 +14,7 @@
 
         # fix locale to package name
         locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE
-        if '_' in locale:
-            locale = locale[:2] + locale[2:].upper()
+        locale = pylocale.normalize(locale).split('.')[0]
         if locale not in AVAILABLE_LOCALES:
             raise AttributeError('Invalid configuration for faker locale "{0}"'.format(locale))

{"golden_diff": "diff --git a/faker/factory.py b/faker/factory.py\n--- a/faker/factory.py\n+++ b/faker/factory.py\n@@ -1,5 +1,6 @@\n from __future__ import unicode_literals\n from __future__ import absolute_import\n+import locale as pylocale\n import sys\n from faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES\n from faker import Generator\n@@ -13,8 +14,7 @@\n \n         # fix locale to package name\n         locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE\n-        if '_' in locale:\n-            locale = locale[:2] + locale[2:].upper()\n+        locale = pylocale.normalize(locale).split('.')[0]\n         if locale not in AVAILABLE_LOCALES:\n             raise AttributeError('Invalid configuration for faker locale \"{0}\"'.format(locale))\n", "issue": "Default locale to language if no territory given.\nIt would be great that if faker was initialized with only a locale and no territory, that it would use  a sensible default.\n\nFor example I currently have to do the following if using something such as \"en\" instead of \"en_US\".  \n\n``` py\nfrom faker import Factory\nfrom faker import AVAILABLE_LOCALES\n\nlocale = 'en'\nif locale not in AVAILABLE_LOCALES:\n    locale = next(l for l in AVAILABLE_LOCALES if l.startswith(locale))\n\nfactory = Factory.create(locale)\n```\n\nThis happens when using dynamic mock data in local development where django sets the locale to \"en\" because we do not define territories.\n\n", "before_files": [{"content": "from __future__ import unicode_literals\nfrom __future__ import absolute_import\nimport sys\nfrom faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES\nfrom faker import Generator\nfrom faker import providers as providers_mod\n\n\nclass Factory(object):\n\n    @classmethod\n    def create(cls, locale=None, providers=None, generator=None, **config):\n\n        # fix locale to package name\n        locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE\n        if '_' in locale:\n            locale = locale[:2] + locale[2:].upper()\n        if locale not in AVAILABLE_LOCALES:\n            raise AttributeError('Invalid configuration for faker locale \"{0}\"'.format(locale))\n\n        providers = providers or DEFAULT_PROVIDERS\n\n        faker = generator or Generator(**config)\n        faker.add_provider(providers_mod.BaseProvider)\n        for provider_name in providers:\n\n            provider_class, lang_found = cls._get_provider_class(provider_name, locale)\n            provider = provider_class(faker)\n            provider.__provider__ = provider_name\n            provider.__lang__ = lang_found\n            faker.add_provider(provider)\n\n        return faker\n\n    @classmethod\n    def _get_provider_class(cls, provider, locale=''):\n\n        provider_class = cls._find_provider_class(provider, locale)\n\n        if provider_class:\n            return provider_class, locale\n\n        if locale and locale != DEFAULT_LOCALE:\n            # fallback to default locale\n            provider_class = cls._find_provider_class(provider, DEFAULT_LOCALE)\n            if provider_class:\n                return provider_class, DEFAULT_LOCALE\n\n        # fallback to no locale\n        provider_class = cls._find_provider_class(provider)\n        if provider_class:\n            return provider_class, None\n\n        raise ValueError('Unable to find provider \"{0}\" with locale \"{1}\"'.format(provider, locale))\n\n    @classmethod\n    def _find_provider_class(cls, provider, locale=''):\n\n        path = \"{providers}{lang}.{provider}\".format(\n            providers=providers_mod.__package__ or providers_mod.__name__,\n            lang='.' + locale if locale else '',\n            provider=provider\n        )\n\n        try:\n            __import__(path)\n        except ImportError:\n            return None\n\n        return sys.modules[path].Provider\n", "path": "faker/factory.py"}], "after_files": [{"content": "from __future__ import unicode_literals\nfrom __future__ import absolute_import\nimport locale as pylocale\nimport sys\nfrom faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES\nfrom faker import Generator\nfrom faker import providers as providers_mod\n\n\nclass Factory(object):\n\n    @classmethod\n    def create(cls, locale=None, providers=None, generator=None, **config):\n\n        # fix locale to package name\n        locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE\n        locale = pylocale.normalize(locale).split('.')[0]\n        if locale not in AVAILABLE_LOCALES:\n            raise AttributeError('Invalid configuration for faker locale \"{0}\"'.format(locale))\n\n        providers = providers or DEFAULT_PROVIDERS\n\n        faker = generator or Generator(**config)\n        faker.add_provider(providers_mod.BaseProvider)\n        for provider_name in providers:\n\n            provider_class, lang_found = cls._get_provider_class(provider_name, locale)\n            provider = provider_class(faker)\n            provider.__provider__ = provider_name\n            provider.__lang__ = lang_found\n            faker.add_provider(provider)\n\n        return faker\n\n    @classmethod\n    def _get_provider_class(cls, provider, locale=''):\n\n        provider_class = cls._find_provider_class(provider, locale)\n\n        if provider_class:\n            return provider_class, locale\n\n        if locale and locale != DEFAULT_LOCALE:\n            # fallback to default locale\n            provider_class = cls._find_provider_class(provider, DEFAULT_LOCALE)\n            if provider_class:\n                return provider_class, DEFAULT_LOCALE\n\n        # fallback to no locale\n        provider_class = cls._find_provider_class(provider)\n        if provider_class:\n            return provider_class, None\n\n        raise ValueError('Unable to find provider \"{0}\" with locale \"{1}\"'.format(provider, locale))\n\n    @classmethod\n    def _find_provider_class(cls, provider, locale=''):\n\n        path = \"{providers}{lang}.{provider}\".format(\n            providers=providers_mod.__package__ or providers_mod.__name__,\n            lang='.' + locale if locale else '',\n            provider=provider\n        )\n\n        try:\n            __import__(path)\n        except ImportError:\n            return None\n\n        return sys.modules[path].Provider\n", "path": "faker/factory.py"}]}