10

I am unable to make my class strings language dependent using babel. I have the following minimal example:

file test-babel.cls

\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{test-babel}[2012/10/22 v0.1]

\DeclareOption{spanish}{%
  \AtBeginDocument{%
      \renewcommand*\conclusionsname{Conclusiones finales}
     }
  \AtEndOfClass{\main@language{spanish}}
}

\DeclareOption{english}{%
  \AtBeginDocument{%
    \renewcommand*\conclusionsname{Final conclussions}
  }
  \AtEndOfClass{\main@language{english}}
}

\newcommand\conclusionsname{dumyy}


\DeclareOption*{\PassOptionsToClass{\CurrentOption}{report}}
\ProcessOptions\relax

\LoadClass{report}
\newcommand\print{\conclusionsname}

I use this test tex file:

\documentclass{test-babel}
\usepackage[spanish]{babel}
\begin{document}
\print
\end{document}

The result of compilation is the word dummy, the default value, and not the Spanish translation.

Can someone help me to understand how to use babel while writing a class?

1 Answer 1

14

The spanish option is not passed to the class, to begin with.

I don't think you really need to have class options for this, however. Here is zunbeltz.cls:

\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{zunbeltz}[2012/10/22 v0.1]

\providecommand*{\conclusionsname}{Final conclusions}

\AtBeginDocument{
  \@ifpackagewith{babel}{spanish}{%
    \addto\captionsspanish{\renewcommand*\conclusionsname{Conclusiones finales}}%
  }{}
  \@ifpackagewith{babel}{english}{%
    \addto\captionsenglish{\renewcommand*\conclusionsname{Final conclusions}}%
  }{}
}


\DeclareOption*{\PassOptionsToClass{\CurrentOption}{report}}
\ProcessOptions\relax

\LoadClass{report}

and here the test document zunbeltz.tex:

\documentclass{zunbeltz}
\usepackage[english,spanish]{babel}
\begin{document}
\conclusionsname

\selectlanguage{english}
\conclusionsname
\end{document}

You'll get

Conclusiones finales
Final conclusions

2
  • Thanks. Your example work fine. But when only one language is used in zunbeltz.tex, let's english, then there is an error: ! Package babel Error: You haven't loaded the option spanish yet.
    – TeXtnik
    Oct 24, 2012 at 9:56
  • 4
    @zunbeltz Compile twice: when you remove a language from a document, the .aux file still keeps track of the preceding LaTeX run when the language was loaded.
    – egreg
    Oct 24, 2012 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.