Skip to content

Commit

Permalink
Merge 6f82ae2 into 47c5ee2
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Dec 6, 2019
2 parents 47c5ee2 + 6f82ae2 commit 8e124c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Intuition.php
Expand Up @@ -356,11 +356,14 @@ protected function normalizeDomain( string $domain ) : string {
}

/**
* Normalize a language string (which may be user-supplied and unsafe) to be an all-lowercase
* hyphen-separated string that approximately conforms to IETF's 'Tags for Identifying Languages'.
* @link https://www.ietf.org/rfc/bcp/bcp47.txt
* @param string $lang
* @return string
*/
protected function normalizeLang( $lang ) {
$lang = strtolower( str_replace( '_', '-', $lang ) );
$lang = trim( preg_replace( '/[^a-z0-9-]+/', '-', strtolower( $lang ) ), '-' );
if ( isset( $this->deprecatedLangCodes[$lang] ) ) {
return $this->deprecatedLangCodes[$lang];
}
Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/IntuitionTest.php
Expand Up @@ -73,7 +73,13 @@ public function testSetLang() {
$this->i18n->msg( 'test-value', 'test-domain' ),
'Change default lang'
);
// Language must be a non-empty string.
$this->assertFalse( $this->i18n->setLang( 42 ), 'Bad value' );
// Language is normalized to lowercase and hyphen-separated.
$this->i18n->setLang( 'en_AU' );
$this->assertEquals( 'en-au', $this->i18n->getLang() );
$this->i18n->setLang( '"bad" lang <string>' );
$this->assertEquals( 'bad-lang-string', $this->i18n->getLang() );
}

/**
Expand Down

0 comments on commit 8e124c8

Please sign in to comment.