Page MenuHomePhabricator

Add parser function to verify that a string is a valid language code
Closed, DeclinedPublic

Description

It would be nice to avoid huge switches like on [[:m:Template:Other languages]] or [[:wm2012:Template:AddLanguageCategory]], and in general to maintain language codes centrally and ensure proper input of other parser functions such as #language.

Note that the example template can't be replaced by the tools in the Translate extension where the source language is not English.
Still, it can be argued that this feature is not worth a function and we should find a better way to do the things it would be used in. Compare bug 28596.


Version: unspecified
Severity: enhancement

Details

Reference
bz35752

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 22 2014, 12:13 AM
bzimport set Reference to bz35752.
bzimport added a subscriber: Unknown Object (MLST).

Could be a use case for Lua, but not as a parser function. Wikitext is not PHP.

Of course, actually implementing this is rather easy... Something like:

/**

  • Reports whether language code is valid.
  • @param $parser Parser
  • @param $inStr string
  • @return bool
	 */

public static function runValidLanguagecode ( $parser, $inStr = '' ) {

		wfProfileIn( __METHOD__ );

		$inStr = self::killMarkers( $parser, (string)$inStr );
		$valid = Language::isValidCode( $inStr );

		wfProfileOut( __METHOD__ );
		return $valid;

}

It could go in string functions and it could also go into core... That's the hard part

(In reply to comment #2)

Of course, actually implementing this is rather easy...

I'm not sure it was, but it should be now that bug 41477 was fixed.

(In reply to Siebrand Mazeland from comment #1)

Could be a use case for Lua, but not as a parser function. Wikitext is not
PHP.

Indeed. In fact, Lua already has mw.language.isValidCode (and a few variants) designed to do just this, so I see no reason we should add a parser function to do it.

(In reply to Jackmcbarn from comment #4)

I see no reason we should add a
parser function to do it.

Well, the reason is explained in comment 0. :-) However the use case was covered in Translate with bug 68385 now and templates based on this implementation would be hacky anyway, so thanks for closing this.