Browse Source

Locale codes with country names are also accepted

E.g. 'en' and 'en_US' define the american english language
pull/136/head
Louis 9 years ago
parent
commit
c8aa617c8e
  1. 84
      patacrep/latex/__init__.py
  2. 1
      test/test_compilation/subdir.tex.control

84
patacrep/latex/__init__.py

@ -13,19 +13,87 @@ from patacrep.latex.syntax import tex2plain, parse_song
LOGGER = logging.getLogger(__name__)
BABEL_LANGUAGES = OrderedDict((
('fr', 'french'),
('en', 'english'),
('de', 'german'),
('es', 'spanish'),
('it', 'italian'),
('pt', 'portuguese'),
('de_de', 'german'),
('de_at', 'austrian'),
('eo_uy', 'esperanto'),
('en_us', 'english'), # USenglish, american
('en_gb', 'british'), # UKenglish
('en_ca', 'canadian'),
('en_au', 'australian'),
('en_nz', 'newzealand'),
('es_es', 'spanish'),
('fr_fr', 'french'), # francais
('fr_ca', 'canadien'),
('it_it', 'italian'),
('la_LA', 'latin'),
('pt_pt', 'portuguese'), # portuges
('pt_br', 'brazilian'), # brazil
## Remaining Babel language codes
# ('??_??', 'afrikaans'),
# ('??_??', 'bahasa'),
# ('??_??', 'indonesian'),
# ('??_??', 'indon'),
# ('??_??', 'bahasai'),
# ('??_??', 'bahasam'),
# ('??_??', 'malay'),
# ('??_??', 'melayu'),
# ('??_??', 'basque'),
# ('??_??', 'breton'),
# ('??_??', 'bulgarian'),
# ('??_??', 'catalan'),
# ('??_??', 'croatian'),
# ('??_??', 'czech'),
# ('??_??', 'danish'),
# ('??_??', 'dutch'),
# ('??_??', 'estonian'),
# ('??_??', 'finnish'),
# ('??_??', 'acadian'),
# ('??_??', 'galician'),
# ('??_??', 'germanb'),
# ('??_??', 'ngerman'),
# ('??_??', 'naustrian'),
# ('??_??', 'greek'),
# ('??_??', 'polutonikogreek'),
# ('??_??', 'hebrew'),
# ('??_??', 'icelandic'),
# ('??_??', 'interlingua'),
# ('??_??', 'irish'),
# ('??_??', 'lowersorbian'),
# ('??_??', 'samin'),
# ('??_??', 'norsk'),
# ('??_??', 'nynorsk'),
# ('??_??', 'polish'),
# ('??_??', 'romanian'),
# ('??_??', 'russian'),
# ('??_??', 'scottish'),
# ('??_??', 'slovak'),
# ('??_??', 'slovene'),
# ('??_??', 'swedish'),
# ('??_??', 'serbian'),
# ('??_??', 'turkish'),
# ('??_??', 'ukrainian'),
# ('??_??', 'uppersorbian'),
# ('??_??', 'welsh'),
))
def lang2babel(lang):
"""Return the language used by babel, corresponding to the language code"""
try:
return BABEL_LANGUAGES[lang]
# Exact match
if lang.lower() in BABEL_LANGUAGES:
return BABEL_LANGUAGES[lang.lower()]
# Only language code is provided (e.g. 'fr')
for babel in BABEL_LANGUAGES:
if babel.startswith(lang.lower()):
return BABEL_LANGUAGES[babel]
# A non existent country code is provided (e.g. 'fr_CD').
language = lang.lower().split("_")[0]
for babel in BABEL_LANGUAGES:
if babel.startswith(language):
LOGGER.error("Unknown country code '{}'. Using default '{}' instead.".format(lang, babel))
return BABEL_LANGUAGES[babel]
except KeyError:
available = ", ".join(BABEL_LANGUAGES.keys())
LOGGER.error('Unknown lang code: ' + lang + '. Supported: ' + available)
LOGGER.error("Unknown language code '{}' (supported: {}). Using default 'english' instead.".format(lang, available))
return 'english'

1
test/test_compilation/subdir.tex.control

@ -34,6 +34,7 @@ guitar,
\usepackage{lmodern}
\PassOptionsToPackage{english}{babel}
\PassOptionsToPackage{english}{babel}
\usepackage[english]{babel}
\lang{english}

Loading…
Cancel
Save