A program that serves people in many languages has the same question everywhere. A user asks for a language. You have a set of resources: voices, translation models, word lists, keyboard layouts. Which one is the best match, or is there none?
The question looks simple, and a short string check seems to answer it. It does not.
Five ways to get it wrong
The same tag has many spellings. pt_br, PT-br and pt-BR are one tag. So is
por-BR, with the three-letter code.
Some codes changed. iw is the old code for Hebrew, in for Indonesian, and mo
for Moldovan, which is Romanian. Old data still uses them.
A bare tag must mean one country. A user who asks for pt gets a voice from one
place. Which place?
A prefix is not a family. A common test for Arabic is tag.startswith("ar"). It
misses Levantine (apc), Mesopotamian (acm) and Tunisian Arabic (aeb), and it
accepts Mapudungun (arn), Aragonese (arg) and Arabela (arl), which are not Arabic.
A country is not a dialect. pt-PT covers Lisbon, Porto, the Azores and Madeira.
They sound different. The standard has no code for a city.
linguatag solves each of these with data, not string tricks.
uv pip install --prerelease=allow linguatag
All outputs below are from linguatag 0.13.5a1.
One form for every tag
from linguatag import standardize_lang
standardize_lang("pt_br") # 'pt-BR'
standardize_lang("por-BR") # 'pt-BR'
standardize_lang("iw") # 'he'
standardize_lang("mo") # 'ro'
standardize_lang("sh") # 'sr-Latn'
The rules come from the IANA Language Subtag Registry and CLDR, the Unicode locale data. linguatag carries both in the package, so it needs no network and has no dependencies.
A bare tag and the nearest match
A bare tag gets a default region and a likely script:
from linguatag import with_default_region, likely_script
with_default_region("pt") # 'pt-PT'
with_default_region("en") # 'en-GB'
likely_script("zh-TW") # 'Hant'
linguatag gives a bare tag the region where the language started and is still spoken:
pt is Portugal and en is Britain. This is a choice, not a standard.
langcodes, the best-known Python library for
tags, makes the other choice: pt is Brazil and en is the United States. Both are
reasonable. A program must know which one it gets.
To pick a resource, ask for the closest tag in your list:
from linguatag import closest_lang
closest_lang("pt-AO", ["pt-BR", "pt-PT"]) # 'pt-PT'
closest_lang("zh-HK-HiuMaanNeural", ["zh-CN", "zh-HK"]) # 'zh-HK'
closest_lang("de", ["fr", "it"]) # None
Angolan Portuguese follows the European norm, so it gets the pt-PT voice. A voice
name after the tag is ignored. When nothing is near enough, the answer is None, not
a wrong language.
Arabic is a family
Arabic is a macrolanguage: one name for about 30 languages, from Moroccan to Gulf Arabic. ISO 639-3 lists its members, and linguatag reads that list:
from linguatag import is_arabic_variety
[is_arabic_variety(t) for t in ["apc", "acm", "aeb", "arz"]] # [True, True, True, True]
[is_arabic_variety(t) for t in ["arn", "arg", "arl"]] # [False, False, False]
Dialects with private-use tags
The language tag standard, BCP 47, stops at the country. For everything below it, it
gives the private-use subtag -x-: anything after x is yours to define. Most
libraries ignore it.
linguatag gives these subtags a meaning. It carries a tree of 187 dialects (lects), from the language specs of our phonemizer orthography2ipa. Each lect has a parent:
from linguatag import parent, ancestors, family
parent("pt-PT-x-porto") # 'pt-PT-x-minho'
ancestors("pt-PT-x-porto")[:3] # ['pt-PT-x-minho', 'pt-PT', 'pt-PT-x-medieval']
family("pt-PT-x-porto") # 'Indo-European > Italic > Romance > Ibero-Romance'
ancestors("ars-SA-x-qassim")[:2] # ['ars-SA', 'ar-x-peninsul']
Porto speech belongs to the Minho group of northern Portugal, under European
Portuguese, which descends from medieval Portuguese. Qassim speech belongs to Najdi
Arabic of Saudi Arabia (ars-SA), which is a Peninsular Arabic.
The tree makes dialect matching possible. Say that you have five Portuguese voices:
voices = ["pt-PT", "pt-BR", "pt-PT-x-porto", "pt-PT-x-lisbon", "pt-PT-x-acores"]
A user from Braga asks for their own speech. There is no Braga voice. Which voice is closest? It depends on what the user wants, so linguatag asks:
closest_lang("pt-PT-x-braga", voices) # 'pt-PT'
closest_lang("pt-PT-x-braga", voices, lects="dialect") # 'pt-PT-x-porto'
closest_lang("pt-PT-x-braga", voices, lects="standard") # 'pt-PT'
With lects="dialect", the nearest dialect wins: Braga and Porto are both in the
Minho group, two steps apart in the tree. With lects="standard", the standard
language wins. Without the argument, the private-use part has no weight, as the
standard says.
A tag can also carry a part that the tree does not know, such as the name of a speaker. linguatag then drops one subtag at a time from the end, as the lookup method of RFC 4647 does:
from linguatag import lookup_tags
lookup_tags("pt-PT-x-porto-miro")
# ['pt-PT-x-porto-miro', 'pt-PT-x-porto', 'pt-PT', 'pt']
closest_lang("pt-PT-x-porto-miro", voices, lects="standard") # 'pt-PT-x-porto'
So you can name a resource as precisely as you want: the language, the country, the dialect, the city, the speaker. A request still finds the nearest resource that exists.
The same tags work across our libraries. orthography2ipa has pronunciation rules for
pt-PT-x-porto, and a voice can be trained on them. A text tool can ask for Lisbon
spelling conventions. All of them read the tag in the same way.
linguatag and langcodes
langcodes is good, and on the common cases the two libraries agree. We tested both on cases with a source (the IANA registry, CLDR and the ISO 639-3 macrolanguage table):
| Task | linguatag 0.13.1a1 | langcodes 3.5.1 | startswith("ar") |
|---|---|---|---|
Standardize pt_br, iw, in, mo, sh | 5 of 5 | 5 of 5 | — |
zh-TW matches zh-Hant | yes | yes | — |
| Is it Arabic? 7 cases | 7 of 7 | 6 of 7 | 1 of 7 |
langcodes puts Mesopotamian Arabic (acm) at distance 64 from Arabic, over its own
match limit of 25. Thus it does not match an Iraqi user to an Arabic resource.
The difference is in the dialects. langcodes, like the standard, gives no meaning to
-x- subtags. linguatag does.
What is not done
- Most of the tree is thin. Many lects have only a parent and no finer group. The
Azores islands, for example, are not yet under one Azores node, so
pt-PT-x-sao-migueldoes not matchpt-PT-x-acoresfirst. - The lect names are ours. Private-use subtags are not a registry. Another
program can use
-x-portofor something else. - The default region is a choice. If your users expect
ptto mean Brazil, give the region explicitly.