IfLanguageName 与 iflanguage

IfLanguageName 与 iflanguage

该包建议使用该包的babel命令来测试语言。据解释,该命令 太粗略,因为它仅基于连字模式。IfLanguageNameiflangiflanguage

但是,例如,如果我们想测试当前语言是否是英语、美国英语、美国英语或美国英语这四种语言之一,我们是否必须对每一种语言进行四次测试?

是否有针对语系的测试,例如针对包括英式、美式和其他变体的英语语系的测试?

答案1

Packagebabel懂得家庭英语的四种以上语言:

\DeclareOption{american}{\bbl@load@language{english}}
\DeclareOption{australian}{\bbl@load@language{english}}
\DeclareOption{british}{\bbl@load@language{english}}
\DeclareOption{canadian}{\bbl@load@language{english}}
\DeclareOption{newzealand}{\bbl@load@language{english}}
\DeclareOption{UKenglish}{\bbl@load@language{english}}
\DeclareOption{USenglish}{\bbl@load@language{english}}

并且babel知道很多连字模式(宏\l@<language>),从english.ldf

\ifx\l@english\@undefined
  \ifx\l@UKenglish\@undefined
    \ifx\l@british\@undefined
      \ifx\l@american\@undefined
        \ifx\l@USenglish\@undefined
          \ifx\l@canadian\@undefined
            \ifx\l@australian\@undefined
              \ifx\l@newzealand\@undefined

检测英语必须测试八种语言名称或八种连字模式。

不可扩展\IfLangEnglish

下面的示例定义了\IfLangEnglish使用 LaTeX\in@检查是否可以在列表中找到参数:

\documentclass{article}
\usepackage[USenglish,british,canadian,australian,ngerman]{babel}

\makeatletter
\newcommand*{\LanguageEnglishList}{%
  ,american,australian,british,canadian,english,newzealand,UKenglish,USenglish,%
}
\@onelevel@sanitize\LanguageEnglishList
\DeclareRobustCommand*{\IfLangEnglish}{%
  \let\@LanguageName@\languagename
  \@onelevel@sanitize\@LanguageName@
  \edef\@next{%
    \noexpand\in@{,\@LanguageName@,}{\LanguageEnglishList}%
  }\@next
  \ifin@
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\begin{document}
  \def\test#1/#2 {%
    \par \textbf{\IfLangEnglish{yes}{no}} (#1/\textbf{#2})\par
  }%
  \test start document/ngerman
  \foreignlanguage{USenglish}{%
    \test foreignlanguage/USenglish
  }
  \begin{otherlanguage}{british}
    \test otherlanguage/british
  \end{otherlanguage}
  \begin{otherlanguage*}{canadian}
    \test otherlanguage*/canadian
  \end{otherlanguage*}
  \selectlanguage{australian}
  \test selectlanguage/australian
  \selectlanguage{ngerman}
  \test selectlanguage/ngerman
\end{document}

里面字母的类别\languagename可以是 11(字母)或 12(其他),甚至可以混合使用(例如,第一个字符有 12,其他字符有 11)。因此,\@onelevel@sanitize将字符规范化为类别 12 以进行比较。

结果:

结果

可扩展\IfLangEnglish

前者\IfLangEnglish由于分配和定义而不能完全扩展。

\IfLangEnglish因此我将根据\IfLanguageName包的可扩展性使用以下定义iflang

\documentclass{article}
\usepackage[USenglish,british,canadian,australian,ngerman]{babel}

\usepackage{iflang}

\makeatletter
\newcommand*{\IfLangEnglish}{%
  % The order can be optimized by putting more often used names first.
  \IfLanguageName{american}\@firstoftwo{%
  \IfLanguageName{australian}\@firstoftwo{%
  \IfLanguageName{british}\@firstoftwo{%
  \IfLanguageName{canadian}\@firstoftwo{%
  \IfLanguageName{english}\@firstoftwo{%
  \IfLanguageName{newzealand}\@firstoftwo{%
  \IfLanguageName{UKenglish}\@firstoftwo{%
  \IfLanguageName{USenglish}\@firstoftwo\@secondoftwo}}}}}}}%
}
\makeatother

\begin{document}
  \def\test#1/#2 {%
    \par
    \csname if\IfLangEnglish{true}{false}\endcsname
      \textbf{yes}
    \else
      \textbf{no}
    \fi
    (#1/\textbf{#2})\par
  }%
  \test start document/ngerman
  \foreignlanguage{USenglish}{%
    \test foreignlanguage/USenglish
  }
  \begin{otherlanguage}{british}
    \test otherlanguage/british 
  \end{otherlanguage}
  \begin{otherlanguage*}{canadian}
    \test otherlanguage*/canadian 
  \end{otherlanguage*}
  \selectlanguage{australian}
  \test selectlanguage/australian
  \selectlanguage{ngerman}
  \test selectlanguage/ngerman
\end{document}

结果

软件包iflang2014/04/29 v1.7

新版本iflang现已推出这里直到它到达 CTAN。(该.dtx文件作为 PDF 附件嵌入,通过纯 TeX 运行它以获取解压后的包文件。)

新版本提供了针对语系的测试命令:

\IfLanguageFamilyEnglish{<yes>}{<no>}

该实现基于可扩展\IfLangEnglish宏。

请参阅受支持的语言系列及其所包含的语言的文档。

答案2

translations套餐提供(可扩展)

\ifcurrentbaselanguage{<lang>}{<true>}{<false>}

测试其中<lang>是调用基础语言的名称translations。»英语« 就是其中之一。其他语言是方言或者别名就而言translations。 只要当前语言是别名()或方言(,...)或方言的别名(,...),\ifcurrentbaselanguage{english}{<true>}{<false>}就将分支放在输入流中<true>babelenglishEnglishbritishUKenglish

以下示例中的所有测试都会打印yes,除了第一个当前语言为的测试之外ngerman

\documentclass{article}
\usepackage[USenglish,british,canadian,australian,newzealand,UKenglish,american,ngerman]{babel}

\usepackage{translations}

\begin{document}

% ngerman
\ifcurrentbaselanguage{English}{yes}{no}

\begin{otherlanguage}{british}
  \ifcurrentbaselanguage{English}{yes}{no}
\end{otherlanguage}

\begin{otherlanguage*}{canadian}
  \ifcurrentbaselanguage{English}{yes}{no}
\end{otherlanguage*}

\selectlanguage{australian}
\ifcurrentbaselanguage{English}{yes}{no}

\selectlanguage{american}
\ifcurrentbaselanguage{English}{yes}{no}

\selectlanguage{newzealand}
\ifcurrentbaselanguage{English}{yes}{no}

\selectlanguage{USenglish}
\ifcurrentbaselanguage{English}{yes}{no}

\selectlanguage{UKenglish}
\ifcurrentbaselanguage{English}{yes}{no}

% this shows that the test is expandable:
\edef\foo{\ifcurrentbaselanguage{English}{yes}{no}}
\show\foo
% > \foo=macro:
% ->yes.
% l.32 \show\foo

\end{document}

在此处输入图片描述

相关内容