Currently the determination of which variant is used by a Language object is done by LanguageConverter inspecting global state, including $wgUser and $wgRequest. There is no straightforward method for a caller to directly specify which variant should be used for a particular Language object.
Inspection of global state should instead be done before the Language object is constructed, for example in RequestContext::getLanguage() where it already inspects state (including request parameters and user preferences) to determine the language itself. The determined variant should then be passed to the Language object, which will use the passed value without further reference to global state.
While Language::factory() itself could do that global state inspection in the absence of the caller specifying a variant, and that would most straightforwardly maintain backwards compatibility, I (@Anomie) think that would be a poor interface to maintain long-term.
How to specify the variant on a Language object is open for discussion. Possibilities, not necessarily mutually exclusive, include:
- Determining it from the $code passed to Language::factory(), e.g. Language::factory( 'en-x-piglatin' ) would select language 'en' using variant 'x-piglatin'.
- Passing it as a separate parameter to Language::factory(), e.g. Language::factory( 'en', 'x-piglatin' ).
- Having a setter on Language and/or LanguageConverter to change the variant after creation.
Original report, reformatted from bugzilla to Phabricator syntax:
See T34906#381134 and following comments. I (@liangent) made some attempts in gerrit 33969.
See Also: T59562: Stop using $wgTitle in LanguageConverter::autoConvert() subclasses