[Tex/LaTex] How to declare locale specific unit with siunitx

babellocalesiunitx

I am writing a multi-language document in latex.

I am using siunitx package in conjonction with babel to handle every numerical and units related numeric values.

My goal was to declare a new unit (for instance \fps) which would output differently depending on the locale selected.

I was thus thinking of something like this :

\documentclass[french,english]{book}

\usepackage[utf8]{inputenc}
\usepackage{babel} 
\usepackage{siunitx}

\sisetup{range-units = single}

\addto\extrasfrench{%
\sisetup{locale = FR, group-separator = {\,}, detect-all}%
\DeclareSIUnit[number-unit-product = \text{~}]{\fps}{images\text{~}par\text{~}seconde}}

\addto\extrasenglish{\sisetup{locale = US, group-separator = {,}, detect-all}%
\DeclareSIUnit[number-unit-product = \text{~}]{\fps}{frames\text{~}per\text{~}second}}

\begin{document}

    English: \SIrange{5}{6}{\fps}
    \begin{otherlanguage}{french}

        Français: \SIrange{5}{6}{\fps}

    \end{otherlanguage}

\end{document}

Unfortunately this idea cannot work since the

\DeclareSIUnit[number-unit-product = \text{~}]{\fps}{frames\text{~}per\text{~}second}

macro is then executed within the document and thus resulting in an error ?

Any idea ?

Thank you.

Best Answer

You can use the facilities of the translator package to do this. If the package is in your system, then siunitx loads it automatically. You can then set up the unit with

\DeclareSIUnit{\fps}{ \translate{frames per second} }

and declare translations with

\newtranslation[to = French]{frames per second}{images par seconde}

In your document this gives

Sample output

\documentclass[french,english]{book}

\usepackage[utf8]{inputenc}
\usepackage{babel} 
\usepackage{siunitx}

\sisetup{range-units = single}

\DeclareSIUnit{\fps}{ \translate{frames per second} }

\newtranslation[to = French]{frames per second}{images par seconde}

\addto\extrasfrench{%
\sisetup{locale = FR, group-separator = {\,}, detect-all}}

\addto\extrasenglish{\sisetup{locale = US, group-separator = {,}, detect-all}}

\begin{document}

English: \SIrange{5}{6}{\fps}

\begin{otherlanguage}{french}
  Français: \SIrange{5}{6}{\fps}
\end{otherlanguage}

\end{document}