(I know this question is old, and maybe you've found a solution, but it's still lingering on the unanswered list.)
I want to write a class which loads babel and other language aware
packages and pass to all of them the option ngerman as default.
Your assumption here is that all language-aware packages use the same option list as babel, which is unfortunately not the case. It may be that you have a specific list of language-aware packages that your class will load, where you can verify whether this is true, but this doesn't take into account any additional packages the user might want to load after they've loaded your class that may also require language support.
The other problem is, what happens if the user wants to use XeLaTeX? In this case, they'll more likely want polyglossia, and here there's another problem. From the polyglossia manual:
polyglossia no longer supports loading language definition files as
package options!
It also doesn't accept ngerman as a language name. It must instead be german with the option spelling=new.
So even if you do find an easy way to add ngerman to the class option list, given that it doesn't contain any other language names, you still won't achieve your desired effect if a user of your class wants polyglossia instead of babel.
What of other language-aware packages? I've come across at least one class where the language is set using a key=value option in the form language=enGB. Given the increasing number of packages with a key=value option list, there may well be some packages that require the language set in this way as well. (I can't recall any off the top of my head, but I did consider it for one of my packages with a key=value option list, although in the end I decided it wasn't a good idea.)
I think that most packages that provide multilingual support tend to check for babel or polyglossia so that they can integrate with their caption mechanism (especially given the complications it can cause if they don't in multilingual documents). However, for those that don't, there's no guarantee at all that they will recognise ngerman as an option.
Therefore, it seems that the only practical solution to your question is to just concentrate on getting babel or polyglossia to set German as the default language if no language has been supplied by the user in the document class, and then assume that any other packages the user wants to load that has multilingual support will have the sense to check for babel or polyglossia and pick up the language lists from them.
The simplest method that I can think of to do this is:
\ProvidesClass{mytest}
\LoadClass{article}
\RequirePackage{tracklang}
\RequirePackage{afterpackage}
\RequirePackage{ifxetex}
\ifxetex
\newcommand{\@mytest@add@dialect}[1]{%
\AfterPackage{polyglossia}{\setotherlanguage{\TrackedLanguageFromDialect{#1}}}%
}
\else
\newcommand{\@mytest@add@dialect}[1]{%
\PassOptionsToPackage{#1}{babel}%
}
\fi
\AnyTrackedLanguages
{
\ForEachTrackedDialect{\this@dialect}{%
\expandafter\@mytest@add@dialect\expandafter{\this@dialect}}
}
{
\ifxetex
\AfterPackage{polyglossia}{\setmainlanguage[spelling=new]{german}}
\else
\PassOptionsToPackage{ngerman}{babel}
\fi
}
\ifxetex
\RequirePackage{polyglossia}
\else
\RequirePackage{babel}
\fi
\endinput
This way, ngerman is only automatically added if the user hasn't specified any language options in the document class, so it's not added if they don't want it.
\PassOptionsToPackage{ngerman}{babel}? – egreg Nov 27 '13 at 17:06\documentclass[english]{mytest}doesn't gives english but german words as the document options are processed before the package options. – Ulrike Fischer Nov 27 '13 at 17:14\*namemacros and\todayin your class file, ending with\language\l@ngerman, you will have ngerman defaults in exactly the same way thatarticle.clshas usenglish defaults. – Dan Nov 28 '13 at 04:40\@classoptonlistso it seems like you are adding the option to the correct place. I think it is rare enough that the kernel doesn't need a dedicated macro. – StrongBad Feb 07 '14 at 18:20babel, is if the passed class option isenglish,ngerman: in such a case, "Last declared language option is 'ngerman', but the last processed one was 'english'" as ababel's warning says. – Denis Bitouzé Apr 08 '14 at 20:10\PassOptionsToClass? (Before processing options and loading the class.) That would still run afoul of the point @DenisBitouzé made, I think. – cfr Sep 13 '14 at 02:07\PassOptionsToClassin the class so that it is process before loading the class? Beside this such options are not passed to subsequent packages as you can try out:\PassOptionsToClass{ngerman}{article}\documentclass{article} \usepackage{babel}\begin{document}blub\end{document}– Ulrike Fischer Sep 13 '14 at 10:11\PassOptionsToClassin a custom class before loading a standard class to pass it options. However, maybe this is dependent onxkeyval. – cfr Sep 13 '14 at 13:54babelsee the options passed to your class iff you do not pass it any default?) – cfr Sep 28 '14 at 21:57