翻訳を含むパッケージを定義するエレガントな方法

翻訳を含むパッケージを定義するエレガントな方法

パッケージの構築時に時々発生する問題は、翻訳を有効にすることです。

現在、私は 2 つのパッケージfoo-nl.sty(オランダ語版) とfoo-en.sty(英語版) を管理しています。このアプローチの問題点は、もちろん 2 つのパッケージを同期する必要があることですが、同期しているかどうかを確認するメカニズムがありません。

\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}

ここに画像の説明を入力してください

関連情報