[Feature] add language variant fallback support
tanyunshi opened this issue · 2 comments
Some one requires translations in English(United States) also understands British English.
For example, if we don't have translations for en-us
, we shall translate the object in en-gb
if the translation exists. For example,
>> f = Fruit.objects.get(name='苹果', translations=
{'de_de': {'name': 'Apfel'},
'tr_tr': {'name': 'elma'},
'en_gb': {'name': 'apple'},}
)
>> f.language('en_us')
'apple'
>> f.language('en-us')
'apple'
It would be a nice feature. I am trying to think of the ways how we can achieve this.
There are multiple ways to achieve this. But I would prefer to be able to do it with a single query.
We can always search keys starting with languagecode_othercountrycode
but this has some disadvantages.
- It would be painfully slow for large queries
- For instance
zh_cn
andzh_tw
are basically two different writing styles for Chinese. They are not really interchangeable.
We can make another settings like settings.TRANSLATIONS_MAP
which contains a list of strings containing interchangeable language keys:
TRANSLATIONS_INTERCHANGEABLE_KEYS = {
'en_us': ['en_gb', 'en_au'],
'en_gb': ['en_us', 'en_au'],
...
}
This way we can always do a has_any
query and retrieve one of the interchangeable translations. That's the most efficient solution I can think of.
@tatterdemalion would you have time to review the related pull-request?