Browse Source

Merge pull request #136 from patacrep/locale

Locale codes with country names are also accepted
pull/144/merge
Louis 9 years ago
parent
commit
d27a7f3c67
  1. 101
      patacrep/latex/__init__.py
  2. 1
      test/test_compilation/datadir.tex.control
  3. 3
      test/test_compilation/languages.sb
  4. 193
      test/test_compilation/languages.tex.control
  5. 4
      test/test_compilation/languages_datadir/songs/language.sgc
  6. 4
      test/test_compilation/languages_datadir/songs/language_location.sgc
  7. 3
      test/test_compilation/languages_datadir/songs/no_language.sgc
  8. 4
      test/test_compilation/languages_datadir/songs/wrong_language.sgc
  9. 4
      test/test_compilation/languages_datadir/songs/wrong_location.sgc

101
patacrep/latex/__init__.py

@ -13,19 +13,96 @@ from patacrep.latex.syntax import tex2plain, parse_song
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
BABEL_LANGUAGES = OrderedDict(( BABEL_LANGUAGES = OrderedDict((
('fr', 'french'), ('de_de', 'german'),
('en', 'english'), ('de_at', 'austrian'),
('de', 'german'), ('eo_uy', 'esperanto'),
('es', 'spanish'), ('en_us', 'english'), # USenglish, american
('it', 'italian'), ('en_gb', 'british'), # UKenglish
('pt', 'portuguese'), ('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): def lang2babel(lang):
"""Return the language used by babel, corresponding to the language code""" """Return the language used by babel, corresponding to the language code"""
try: # Exact match
return BABEL_LANGUAGES[lang] if lang.lower() in BABEL_LANGUAGES:
except KeyError: return BABEL_LANGUAGES[lang.lower()]
available = ", ".join(BABEL_LANGUAGES.keys()) # Only language code is provided (e.g. 'fr')
LOGGER.error('Unknown lang code: ' + lang + '. Supported: ' + available) for babel in BABEL_LANGUAGES:
return 'english' 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]
# Error: no (exact or approximate) match found
available = ", ".join(BABEL_LANGUAGES.keys())
LOGGER.error(
"Unknown language code '{}' (supported: {}). Using default 'english' instead.".format(
lang,
available
)
)
return 'english'

1
test/test_compilation/datadir.tex.control

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

3
test/test_compilation/languages.sb

@ -0,0 +1,3 @@
{
"datadir": ["languages_datadir"]
}

193
test/test_compilation/languages.tex.control

@ -0,0 +1,193 @@
%% Automatically generated document.
%% You may edit this file but all changes will be overwritten.
%% If you want to change this document, have a look at
%% the templating system.
%%
%% Generated using Songbook <http://www.patacrep.com>
\makeatletter
\def\input@path{ %
{@TEST_FOLDER@/languages_datadir/latex/} %
{@TEST_FOLDER@/latex/} %
{@DATA_FOLDER@/latex/} %
}
\makeatother
\documentclass[
]{article}
\usepackage[
chorded,
diagram,
pictures,
guitar,
]{patacrep}
\usepackage{lmodern}
\PassOptionsToPackage{english}{babel}
\PassOptionsToPackage{spanish}{babel}
\PassOptionsToPackage{french}{babel}
\PassOptionsToPackage{english}{babel}
\usepackage[english]{babel}
\lang{english}
\usepackage{graphicx}
\graphicspath{ %
{@TEST_FOLDER@/languages_datadir/} %
{@TEST_FOLDER@/} %
{@DATA_FOLDER@/} %
}
\makeatletter
\@ifpackageloaded{hyperref}{}{
\usepackage{url}
\newcommand{\phantomsection}{}
\newcommand{\hyperlink}[2]{#2}
\newcommand{\href}[2]{\expandafter\url\expandafter{#1}}
}
\makeatother
\usepackage{chords}
\title{Guitar songbook}
\author{The Patacrep Team}
\newindex{titleidx}{languages_title}
\newauthorindex{authidx}{languages_auth}
\notenamesout{A}{B}{C}{D}{E}{F}{G}
\begin{document}
\maketitle
\showindex{\songindexname}{titleidx}
\showindex{\authorindexname}{authidx}
% list of chords
\ifchorded
\ifdiagram
\phantomsection
\addcontentsline{toc}{section}{\chordlistname}
\chords
\fi
\fi
\phantomsection
\addcontentsline{toc}{section}{\songlistname}
\begin{songs}{titleidx,authidx}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% songs/./language.sgc
\selectlanguage{english}
\beginsong{Language only}[
by={
},
]
\begin{verse}
Foo
\end{verse}
\endsong
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% songs/./language_location.sgc
\selectlanguage{french}
\beginsong{Full language specification}[
by={
},
]
\begin{verse}
Foo
\end{verse}
\endsong
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% songs/./no_language.sgc
\selectlanguage{english}
\beginsong{No language}[
by={
},
]
\begin{verse}
Foo
\end{verse}
\endsong
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% songs/./wrong_language.sgc
\selectlanguage{english}
\beginsong{Wrong language}[
by={
},
]
\begin{verse}
Foo
\end{verse}
\endsong
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% songs/./wrong_location.sgc
\selectlanguage{spanish}
\beginsong{Wrong location}[
by={
},
]
\begin{verse}
Foo
\end{verse}
\endsong
\end{songs}
\end{document}

4
test/test_compilation/languages_datadir/songs/language.sgc

@ -0,0 +1,4 @@
{title : Language only}
{lang: en}
Foo

4
test/test_compilation/languages_datadir/songs/language_location.sgc

@ -0,0 +1,4 @@
{title : Full language specification}
{lang: fr_FR}
Foo

3
test/test_compilation/languages_datadir/songs/no_language.sgc

@ -0,0 +1,3 @@
{title : No language}
Foo

4
test/test_compilation/languages_datadir/songs/wrong_language.sgc

@ -0,0 +1,4 @@
{title : Wrong language}
{lang: xx_XX}
Foo

4
test/test_compilation/languages_datadir/songs/wrong_location.sgc

@ -0,0 +1,4 @@
{title : Wrong location}
{lang: es_XX}
Foo
Loading…
Cancel
Save