Skip to content

Commit

Permalink
Merge ab899e0 into 47c5ee2
Browse files Browse the repository at this point in the history
  • Loading branch information
samwilson committed Dec 6, 2019
2 parents 47c5ee2 + ab899e0 commit b2b5be3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,11 @@
## [Unreleased]

### Changed

* Language codes: more strict normalization is added
to ensure that language codes more closely conform to
[RFC5646](https://tools.ietf.org/html/rfc5646).

## v2.1.0 (2019-11-24)

### Changed
Expand Down
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://tools.ietf.org/html/rfc5646
* @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 b2b5be3

Please sign in to comment.