return (bool)preg_match( '/^[a-z0-9-]+$/i', $code );
}
+ /**
+ * Returns true if a language code is an IETF tag known to MediaWiki.
+ *
+ * @param $code string
+ *
+ * @since 1.21
+ * @return bool
+ */
+ public static function isKnownLanguageTag( $tag ) {
+ static $coreLanguageNames;
+
+ if ( $coreLanguageNames === null ) {
+ include( MWInit::compiledPath( 'languages/Names.php' ) );
+ }
+
+ if ( isset( $coreLanguageNames[$tag] )
+ || self::fetchLanguageName( $tag, $tag ) !== ''
+ ) {
+ return true;
+ }
+
+ return false;
+ }
+
/**
* @param $code
* @return String Name of the language class
);
}
+ /**
+ * Test Language::isKnownLanguageTag()
+ * @dataProvider provideKnownLanguageTags
+ */
+ function testKnownLanguageTag( $code, $message = '' ) {
+ $this->assertTrue(
+ (bool) Language::isKnownLanguageTag( $code ),
+ "validating code $code - $message"
+ );
+ }
+
+ function provideKnownLanguageTags() {
+ return array(
+ array( 'fr', 'simple code' ),
+ array( 'bat-smg', 'an MW legacy tag' ),
+ array( 'sgs', 'an internal standard MW name, for which a legacy tag is used externally' ),
+ );
+ }
+
+ /**
+ * Test Language::isKnownLanguageTag()
+ */
+ function testKnownCldrLanguageTag() {
+ if ( !class_exists( 'LanguageNames' ) ) {
+ $this->markTestSkipped( 'reason' );
+ }
+
+ $this->assertTrue(
+ (bool) Language::isKnownLanguageTag( 'pal' ),
+ 'validating code "pal" an ancient language, which probably will not appear in Names.php, but appears in CLDR in English'
+ );
+ }
+
+ /**
+ * Negative tests for Language::isKnownLanguageTag()
+ * @dataProvider provideUnKnownLanguageTags
+ */
+ function testUnknownLanguageTag( $code, $message = '' ) {
+ $this->assertFalse(
+ (bool) Language::isKnownLanguageTag( $code ),
+ "checking that code $code is invalid - $message"
+ );
+ }
+
+ function provideUnknownLanguageTags() {
+ return array(
+ array( 'mw', 'non-existent two-letter code' ),
+ );
+ }
+
/**
* @dataProvider provideSprintfDateSamples
*/