Only for documentation:
Example: a list with sorted label of localized countries
# init.py
import gettext
import pycountry
german = gettext.translation(
"iso3166",
pycountry.LOCALES_DIR,
languages=["de"])
german.install()
german_countries = german.gettext
# vocabulary.py
from your.addon import german_countries
from zope.interface import provider
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
import pycountry
def countries_factory(context=None):
countries = [country for country in pycountry.countries]
countries.sort(key=lambda country: german_countries(country.name))
terms = [
SimpleTerm(
value=country.name,
token=country.numeric,
title=german_countries(country.name),
)
for country in countries
]
return SimpleVocabulary(terms)
@provider(IVocabularyFactory)
def countries(context):
return countries_factory(context)
<!-- vocabulary.zcml -->
<configure xmlns="http://namespaces.zope.org/zope"
xmlns:five="http://namespaces.zope.org/five"
xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
i18n_domain="your.addon">
<utility component=".vocabularies.countries"
name="your.addon.vocabulary.countries" />
</configure>
# interfaces.py
class IMyCT(model.Schema):
"""Marker interface and Dexterity Python Schema for CT"""
country = schema.Choice(
title="Land",
required=False,
source="your.addon.vocabulary.countries",
)