
建置包時有時會出現的一個問題是啟用翻譯。
目前我維護兩個軟體包foo-nl.sty
(荷蘭語版本)和foo-en.sty
(英文版本)。當然,這種方法的一個問題是需要同步兩個包,並且沒有機制來檢查是否是這種情況。
還有哪些其他選項可以提供翻譯。如果能夠根據\usepackage[dutch]{babel}
主文檔中的命令自動設定語言,那就太好了。
答案1
該translations
軟體包正是為此目的而開發的。包中為每種語言的關鍵字提供了翻譯
\DeclareTranslation{<language>}{<keyword>}{<translation>}
也許還應該添加
\DeclareTranslationFallback{<keyword>}{<translation>}
babel
如果選擇的語言沒有提供翻譯,則使用該語言。
以這種方式聲明的翻譯可以與
\GetTranslation{<keyword>}
該命令是可擴展的,因此它也適用於用作hyperref
書籤的部分標題。
這是一個簡短的範例包:
\documentclass{article}
\usepackage[english,dutch]{babel}
\usepackage{filecontents}
\begin{filecontents}{mypackage.sty}
\ProvidesPackage{mypackage}
\RequirePackage{translations}
% a command that's translated according to the provided translations:
\newcommand*\mypackagetitle{\section*{\GetTranslation{mypackage-title}}}
% the translations:
% the fallback is used for languages where no specific translation is provided
\DeclareTranslationFallback {mypackage-title}{My title}
\DeclareTranslation{English}{mypackage-title}{My title}
\DeclareTranslation{Dutch} {mypackage-title}{Mijn titel}
\end{filecontents}
\usepackage{mypackage}
\begin{document}
\mypackagetitle
\edef\foo{\GetTranslation{mypackage-title}}
\show\foo
% > \foo=macro:
% ->Mijn titel.
% l.27 \show\foo
\selectlanguage{english}
\mypackagetitle
\edef\foo{\GetTranslation{mypackage-title}}
\show\foo
% > \foo=macro:
% ->My title.
% l.37 \show\foo
\end{document}