From: Georg Baum Date: Wed, 4 Feb 2009 20:06:50 +0000 (+0000) Subject: Partial fix of bug 1476: X-Git-Tag: 2.0.0~7308 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=cd27d171d5b94eb3345d4be435271030e015bdb6;p=features.git Partial fix of bug 1476: Implement infrastructure to add translations to layout files and translate one .inc file. Some others are missing. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@28356 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/lib/doc/Customization.lyx b/lib/doc/Customization.lyx index a64345aa3c..e6e3a476d7 100644 --- a/lib/doc/Customization.lyx +++ b/lib/doc/Customization.lyx @@ -8888,6 +8888,261 @@ FreeSpacing \begin_inset Flex CharStyle:Code status collapsed +\begin_layout Plain Layout +I18NPreamble +\end_layout + +\end_inset + + Language dependent information to be included in the LaTeX preamble when + this style is used. + Note that this will completely override any prior +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout +I18NPreamble +\end_layout + +\end_inset + + declarations. + Must end with +\begin_inset Quotes eld +\end_inset + + +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout +EndI18NPreamble +\end_layout + +\end_inset + + +\begin_inset Quotes erd +\end_inset + +. + The contents of this tag will occur once in the LaTeX preamble for each + language used by the document. + Each time, the following substitutions are made: +\end_layout + +\begin_deeper +\begin_layout Itemize +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout +$$lang +\end_layout + +\end_inset + + is replaced by the language name (as used by the babel package). +\end_layout + +\begin_layout Itemize +The +\begin_inset Quotes eld +\end_inset + +Function +\begin_inset Quotes erd +\end_inset + + +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout +_() +\end_layout + +\end_inset + + replaces its argument by the translation of the argument to the current + language. +\end_layout + +\begin_layout Standard +If the style defines text that appears in the typeset document, it may use + +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout +I18NPreamble +\end_layout + +\end_inset + + to support non-english and even multilanguage documents correctly. + The following excerpt (from the +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout +theorems-ams.inc +\end_layout + +\end_inset + + file) shows how this works: +\end_layout + +\begin_layout LyX-Code +Preamble +\end_layout + +\begin_layout LyX-Code + +\backslash +providecommand{ +\backslash +claimname}{Claim} +\end_layout + +\begin_layout LyX-Code + +\backslash +theoremstyle{remark} +\end_layout + +\begin_layout LyX-Code + +\backslash +newtheorem{claim}[thm]{ +\backslash +protect +\backslash +claimname} +\end_layout + +\begin_layout LyX-Code +EndPreamble +\end_layout + +\begin_layout LyX-Code +I18nPreamble +\end_layout + +\begin_layout LyX-Code + +\backslash +AtBeginDocument{ +\backslash +ifx +\backslash +LdfInit +\backslash +undefined +\backslash +relax +\backslash +else% +\end_layout + +\begin_layout LyX-Code + +\backslash +addto +\backslash +captions$$lang{ +\backslash +renewcommand{ +\backslash +claimname}{_(Claim)}}% +\end_layout + +\begin_layout LyX-Code + +\backslash +fi} +\end_layout + +\begin_layout LyX-Code +EndI18nPreamble +\end_layout + +\begin_layout Standard +The key to correct translation of the user visible text is the definition + of the command +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout + +\backslash +claimname +\end_layout + +\end_inset + + in the standard preamble. + This command holds the name of the theorem that will appear in the output. + The +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout +I18NPreamble +\end_layout + +\end_inset + + then uses the commands offered by the babel package to redefine +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout + +\backslash +claimname +\end_layout + +\end_inset + + for each used language. + The +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout + +\backslash +ifx +\backslash +LdfInit +\end_layout + +\end_inset + + test ensures that no LaTeX error occurs in documents that do not use babel. + Since LyX loads babel after the style-specific preamble, the whole command + is deferred to the actual start of the document with +\begin_inset Flex CharStyle:Code +status collapsed + +\begin_layout Plain Layout + +\backslash +AtBeginDocument +\end_layout + +\end_inset + +. +\end_layout + +\end_deeper +\begin_layout Description +\begin_inset Flex CharStyle:Code +status collapsed + \begin_layout Plain Layout InnerTag \end_layout diff --git a/lib/layouts/theorems-ams.inc b/lib/layouts/theorems-ams.inc index 79327ef66d..e3fadb8294 100644 --- a/lib/layouts/theorems-ams.inc +++ b/lib/layouts/theorems-ams.inc @@ -19,7 +19,7 @@ # - Proof # - Case (regular only -- defined as an enumeration) -Format 11 +Format 12 Style Theorem Category Theorem @@ -49,9 +49,13 @@ Style Theorem Series Bold EndFont Preamble + \providecommand{\theoremname}{Theorem} \theoremstyle{plain} - \newtheorem{thm}{Theorem} + \newtheorem{thm}{\protect\theoremname} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\theoremname}{_(Theorem)}}\fi} + EndI18nPreamble Requires amsthm End @@ -62,9 +66,13 @@ Style Corollary LatexName cor LabelString "Corollary \thetheorem." Preamble + \providecommand{\corollaryname}{Corollary} \theoremstyle{plain} - \newtheorem{cor}[thm]{Corollary} + \newtheorem{cor}[thm]{\protect\corollaryname} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\corollaryname}{_(Corollary)}}\fi} + EndI18nPreamble End @@ -74,9 +82,13 @@ Style Lemma LatexName lem LabelString "Lemma \thetheorem." Preamble + \providecommand{\lemmaname}{Lemma} \theoremstyle{plain} - \newtheorem{lem}[thm]{Lemma} + \newtheorem{lem}[thm]{\protect\lemmaname} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\lemmaname}{_(Lemma)}}\fi} + EndI18nPreamble End @@ -86,9 +98,13 @@ Style Proposition LatexName prop LabelString "Proposition \thetheorem." Preamble + \providecommand{\propositionname}{Proposition} \theoremstyle{plain} - \newtheorem{prop}[thm]{Proposition} + \newtheorem{prop}[thm]{\protect\propositionname} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\propositionname}{_(Proposition)}}\fi} + EndI18nPreamble End @@ -98,9 +114,13 @@ Style Conjecture LatexName conjecture LabelString "Conjecture \thetheorem." Preamble + \providecommand{\conjecturename}{Conjecture} \theoremstyle{plain} - \newtheorem{conjecture}[thm]{Conjecture} + \newtheorem{conjecture}[thm]{\protect\conjecturename} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\conjecturename}{_(Conjecture)}}\fi} + EndI18nPreamble End @@ -110,9 +130,13 @@ Style Fact LatexName fact LabelString "Fact \thetheorem." Preamble + \providecommand{\factname}{Fact} \theoremstyle{plain} - \newtheorem{fact}[thm]{Fact} + \newtheorem{fact}[thm]{\protect\factname} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\factname}{_(Fact)}}\fi} + EndI18nPreamble End @@ -129,9 +153,13 @@ Style Definition Series Bold EndFont Preamble + \providecommand{\definitionname}{Definition} \theoremstyle{definition} - \newtheorem{defn}[thm]{Definition} + \newtheorem{defn}[thm]{\protect\definitionname} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\definitionname}{_(Definition)}}\fi} + EndI18nPreamble End @@ -140,10 +168,13 @@ Style Example LatexName example LabelString "Example \thetheorem." Preamble - \theoremstyle{definition} - \newtheorem{example}[thm]{Example} + \providecommand{\examplename}{Example} + \theoremstyle{definition} + \newtheorem{example}[thm]{\protect\examplename} EndPreamble - Requires amsthm + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\examplename}{_(Example)}}\fi} + EndI18nPreamble End @@ -152,10 +183,13 @@ Style Problem LatexName problem LabelString "Problem \thetheorem." Preamble + \providecommand{\problemname}{Problem} \theoremstyle{definition} - \newtheorem{problem}[thm]{Problem} + \newtheorem{problem}[thm]{\protect\problemname} EndPreamble - Requires amsthm + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\problemname}{_(Problem)}}\fi} + EndI18nPreamble End @@ -164,10 +198,13 @@ Style Exercise LatexName xca LabelString "Exercise \thetheorem." Preamble + \providecommand{\exercisename}{Exercise} \theoremstyle{definition} - \newtheorem{xca}[thm]{Exercise} + \newtheorem{xca}[thm]{\protect\exercisename} EndPreamble - Requires amsthm + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\exercisename}{_(Exercise)}}\fi} + EndI18nPreamble End @@ -185,9 +222,13 @@ Style Remark Shape Italic EndFont Preamble + \providecommand{\remarkname}{Remark} \theoremstyle{remark} - \newtheorem{rem}[thm]{Remark} + \newtheorem{rem}[thm]{\protect\remarkname} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\remarkname}{_(Remark)}}\fi} + EndI18nPreamble End @@ -196,10 +237,13 @@ Style Claim LatexName claim LabelString "Claim \thetheorem." Preamble + \providecommand{\claimname}{Claim} \theoremstyle{remark} - \newtheorem{claim}[thm]{Claim} + \newtheorem{claim}[thm]{\protect\claimname} EndPreamble - Requires amsthm + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\claimname}{_(Claim)}}\fi} + EndI18nPreamble End @@ -226,9 +270,10 @@ Style Case Shape Italic EndFont Preamble + \providecommand{\casename}{Case} \newcounter{casectr} \newenvironment{caseenv} - {\begin{list}{{\itshape\ Case} \arabic{casectr}.}{% + {\begin{list}{{\itshape\ \protect\casename} \arabic{casectr}.}{% \setlength{\leftmargin}{\labelwidth} \addtolength{\leftmargin}{\parskip} \setlength{\itemindent}{\listparindent} @@ -238,4 +283,7 @@ Style Case \usecounter{casectr}} {\end{list}} EndPreamble + I18nPreamble + \AtBeginDocument{\ifx\LdfInit\undefined\relax\else\addto\captions$$lang{\renewcommand{\casename}{_(Case)}}\fi} + EndI18nPreamble End diff --git a/lib/scripts/layout2layout.py b/lib/scripts/layout2layout.py index af5ab1d44f..d99c14615c 100644 --- a/lib/scripts/layout2layout.py +++ b/lib/scripts/layout2layout.py @@ -9,7 +9,7 @@ # Full author contact details are available in file CREDITS -# This script will update a .layout file to format 6 +# This script will update a .layout file to current format import os, re, string, sys @@ -39,7 +39,10 @@ import os, re, string, sys # Incremented to format 11, 14 October 2008 by rgh # Add ProvidesModule, ExcludesModule tags -currentFormat = 11 +# Incremented to format 12, 10 January 2009 by gb +# Add I18NPreamble tag + +currentFormat = 12 def usage(prog_name): @@ -120,6 +123,7 @@ def convert(lines): re_AMSMaths = re.compile(r'^\s*Input amsmaths.inc\s*') re_AMSMathsPlain = re.compile(r'^\s*Input amsmaths-plain.inc\s*') re_AMSMathsSeq = re.compile(r'^\s*Input amsmaths-seq.inc\s*') + re_TocLevel = re.compile(r'^(\s*)(TocLevel)(\s+)(\S+)', re.IGNORECASE) # counters for sectioning styles (hardcoded in 1.3) counters = {"part" : "\\Roman{part}", @@ -150,6 +154,7 @@ def convert(lines): i = 0 only_comment = 1 counter = "" + toclevel = "" label = "" labelstring = "" labelstringappendix = "" @@ -197,6 +202,11 @@ def convert(lines): i += 1 continue + # This just involved new features, not any changes to old ones + if format == 11: + i += 1 + continue + if format == 10: match = re_UseMod.match(lines[i]) if match: @@ -400,11 +410,17 @@ def convert(lines): latextype = string.lower(match.group(4)) latextype_line = i + # Remember the TocLevel line + match = re_TocLevel.match(lines[i]) + if match: + toclevel = string.lower(match.group(4)) + # Reset variables at the beginning of a style definition match = re_Style.match(lines[i]) if match: style = string.lower(match.group(4)) counter = "" + toclevel = "" label = "" space1 = "" labelstring = "" @@ -472,7 +488,7 @@ def convert(lines): i += 1 # Add the TocLevel setting for sectioning styles - if toclevels.has_key(style) and maxcounter <= toclevels[style]: + if toclevel == "" and toclevels.has_key(style) and maxcounter <= toclevels[style]: lines.insert(i, '%sTocLevel %d' % (space1, toclevels[style])) i += 1 diff --git a/src/LaTeXFeatures.cpp b/src/LaTeXFeatures.cpp index a6c08888ef..577df7473d 100644 --- a/src/LaTeXFeatures.cpp +++ b/src/LaTeXFeatures.cpp @@ -16,6 +16,7 @@ #include "LaTeXFeatures.h" +#include "Buffer.h" #include "Color.h" #include "BufferParams.h" #include "Encoding.h" @@ -869,10 +870,16 @@ docstring const LaTeXFeatures::getTClassPreamble() const tcpreamble << tclass.preamble(); + typedef LanguageList::const_iterator lang_it; + lang_it const lbeg = UsedLanguages_.begin(); + lang_it const lend = UsedLanguages_.end(); list::const_iterator cit = usedLayouts_.begin(); list::const_iterator end = usedLayouts_.end(); for (; cit != end; ++cit) { tcpreamble << tclass[*cit].preamble(); + tcpreamble << tclass[*cit].i18npreamble(buffer().language()); + for (lang_it lit = lbeg; lit != lend; ++lit) + tcpreamble << tclass[*cit].i18npreamble(*lit); } return tcpreamble.str(); diff --git a/src/Layout.cpp b/src/Layout.cpp index cbc1ab1899..e59374a992 100644 --- a/src/Layout.cpp +++ b/src/Layout.cpp @@ -13,13 +13,18 @@ #include #include "Layout.h" +#include "Language.h" #include "TextClass.h" #include "Lexer.h" #include "Font.h" +#include "support/Messages.h" #include "support/debug.h" +#include "support/lassert.h" #include "support/lstrings.h" +#include + using namespace std; using namespace lyx::support; @@ -51,6 +56,7 @@ enum LayoutTags { LT_FREE_SPACING, LT_PASS_THRU, //LT_HEADINGS, + LT_I18NPREAMBLE, LT_ITEMSEP, LT_KEEPEMPTY, LT_LABEL_BOTTOMSEP, @@ -144,6 +150,7 @@ bool Layout::read(Lexer & lex, TextClass const & tclass) { "fill_top", LT_FILL_TOP }, { "font", LT_FONT }, { "freespacing", LT_FREE_SPACING }, + { "i18npreamble", LT_I18NPREAMBLE }, { "innertag", LT_INNERTAG }, { "intitle", LT_INTITLE }, { "itemsep", LT_ITEMSEP }, @@ -329,6 +336,10 @@ bool Layout::read(Lexer & lex, TextClass const & tclass) preamble_ = from_utf8(lex.getLongString("EndPreamble")); break; + case LT_I18NPREAMBLE: + i18npreamble_ = from_utf8(lex.getLongString("EndI18NPreamble")); + break; + case LT_LABELTYPE: readLabelType(lex); break; @@ -404,7 +415,7 @@ bool Layout::read(Lexer & lex, TextClass const & tclass) case LT_LABELSTRING: // label string definition // FIXME: this means LT_ENDLABELSTRING may only // occur after LT_LABELSTRING - lex >> labelstring_; + lex >> labelstring_; labelstring_ = trim(labelstring_); labelstring_appendix_ = labelstring_; break; @@ -763,6 +774,38 @@ docstring const & Layout::depends_on() const } +docstring const Layout::i18npreamble(Language const * lang) const +{ + if (i18npreamble_.empty()) + return i18npreamble_; + string preamble = subst(to_utf8(i18npreamble_), "$$lang", + lang->babel()); +#ifdef TEX2LYX + // tex2lyx does not have getMessages() + LASSERT(false, /**/); +#else + // FIXME UNICODE + // boost::regex is not unicode-safe. + // Should use QRegExp or (boost::u32regex, but that requires ICU) + static boost::regex const reg("_\\(([^\\)]+)\\)"); + boost::smatch sub; + while (boost::regex_search(preamble, sub, reg)) { + string const key = sub.str(1); + string translated; + if (isAscii(key)) + translated = to_utf8(getMessages(lang->code()).get(key)); + else { + lyxerr << "Warning: not translating `" << key + << "' because it is not pure ASCII." << endl; + translated = key; + } + preamble = subst(preamble, sub.str(), translated); + } +#endif + return from_utf8(preamble); +} + + bool Layout::operator==(Layout const & rhs) const { // This is enough for the applications we actually make, diff --git a/src/Layout.h b/src/Layout.h index e4aab34c5d..eab06f53ef 100644 --- a/src/Layout.h +++ b/src/Layout.h @@ -24,6 +24,7 @@ namespace lyx { +class Language; class Lexer; class TextClass; @@ -89,6 +90,9 @@ public: docstring const & category() const { return category_; } /// docstring const & preamble() const { return preamble_; } + /// Get language dependent macro definitions needed for this layout + /// for language \p lang + docstring const i18npreamble(Language const * lang) const; /// std::set const & requires() const { return requires_; } /// @@ -266,6 +270,8 @@ private: docstring category_; /// Macro definitions needed for this layout docstring preamble_; + /// Language dependent macro definitions needed for this layout + docstring i18npreamble_; /// Packages needed for this layout std::set requires_; }; diff --git a/src/TextClass.cpp b/src/TextClass.cpp index 9088046239..2b5f52f5e4 100644 --- a/src/TextClass.cpp +++ b/src/TextClass.cpp @@ -60,7 +60,7 @@ private: docstring name_; }; -int const FORMAT = 11; +int const FORMAT = 12; bool layout2layout(FileName const & filename, FileName const & tempfile)