1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
Description: Don't use the shipped static lists for the country-codes
Debian already provides the necessary data as part of its ico-codes package.
This patch changes the upstream behaviour to gather the data at runtime by
parsing the XML files shipped with the iso-codes package.
Translations for the countries names are also taken from iso-codes by using
the symlinked .mo files with Django's lazy translation.
Sorting function by Jakub Wilk.
Author: Fladischer Michael <FladischerMichael@fladi.at>
Forwarded: not-needed
Last-Update: 2011-11-17
--- a/django_countries/settings.py
+++ b/django_countries/settings.py
@@ -1,3 +1,4 @@
+import os
from django.conf import settings
@@ -11,5 +12,11 @@
prefix = '%s/' % prefix
return '%s%s' % (prefix, url)
+def _build_iso_xml():
+ if hasattr(settings, 'COUNTRIES_ISO_XML'):
+ return settings.COUNTRIES_ISO_XML
+ else:
+ return '/usr/share/xml/iso-codes/iso_3166.xml'
FLAG_URL = _build_flag_url()
+ISO_XML = _build_iso_xml()
--- a/django_countries/countries.py
+++ b/django_countries/countries.py
@@ -1,5 +1,35 @@
from django.utils.translation import ugettext_lazy as _
+import unicodedata
+from xml.dom.minidom import parse
+
+from django_countries import settings
+
+
+# Nicely titled (and translated) country names.
+COUNTRIES = []
+COUNTRIES_PLUS = []
+OFFICIAL_COUNTRIES = {}
+
+def _sort_key(s):
+ return unicodedata.normalize('NFD', s).encode('ASCII', 'ignore')
+
+for node in sorted(parse(settings.ISO_XML).getElementsByTagName('iso_3166_entry'),
+ key=lambda node: _sort_key(node.getAttribute('name'))):
+ _code = str(node.getAttribute('alpha_2_code'))
+ _name = node.getAttribute('name')
+ COUNTRIES.append((_code, _(_name)))
+ COUNTRIES_PLUS.append((_code, _name))
+ if _name.find(', ') >= 0:
+ COUNTRIES_PLUS.append((_code, ' '.join(reversed(_name.split(', ')))))
+ OFFICIAL_COUNTRIES[_code.upper()] = _name.upper()
+
+COUNTRIES_PLUS = [(_code, _(_name)) for (_code, _name) in sorted(COUNTRIES_PLUS, key=lambda node: _sort_key(node[1]))]
+
+'''
+# Upstream shipped static lists of countries.
+from django.utils.translation import ugettext_lazy as _
+
# Nicely titled (and translatable) country names.
COUNTRIES = (
('AF', _(u'Afghanistan')),
@@ -775,3 +805,5 @@
'ZM': u'ZAMBIA',
'ZW': u'ZIMBABWE',
}
+'''
+
--- a/setup.py
+++ b/setup.py
@@ -28,7 +28,6 @@
packages=find_packages(),
zip_safe=False,
package_data={'django_countries': [
- 'locale/*/LC_MESSAGES/*',
]},
# titlecase PYPI is broken, copied the module directly for now (in /bin)
# requires=['titlecase'],
|