mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUGFIX Check for valid locale in i18n::set_locale()/set_default_locale()/include_locale_file()/include_by_locale() (as defined in i18n::$allowed_locales). Implicitly sanitizes the data for usage in controllers.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@114469 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
640b504ebe
commit
17ac4753fd
@ -1715,6 +1715,26 @@ class i18n extends Object {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a "long" locale format (e.g. "en_US")
|
||||
* by checking it against {@link $all_locales}.
|
||||
*
|
||||
* To add a locale to {@link $all_locales}, use the following example
|
||||
* in your mysite/_config.php:
|
||||
* <code>
|
||||
* i18n::$allowed_locales['xx_XX'] = '<Language name>';
|
||||
* </code>
|
||||
*
|
||||
* Note: Does not check for {@link $allowed_locales}.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
static function validate_locale($locale) {
|
||||
// Convert en-US to en_US
|
||||
$locale = str_replace('-', '_', $locale);
|
||||
return (array_key_exists($locale, self::$all_locales));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current locale
|
||||
* See http://unicode.org/cldr/data/diff/supplemental/languages_and_territories.html for a list of possible locales
|
||||
@ -1722,6 +1742,8 @@ class i18n extends Object {
|
||||
* @param string $locale Locale to be set
|
||||
*/
|
||||
static function set_locale($locale) {
|
||||
if(!self::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||
|
||||
if ($locale) self::$current_locale = $locale;
|
||||
}
|
||||
|
||||
@ -1764,6 +1786,8 @@ class i18n extends Object {
|
||||
* @param String $locale
|
||||
*/
|
||||
static function set_default_locale($locale) {
|
||||
if(!self::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||
|
||||
self::$default_locale = $locale;
|
||||
}
|
||||
|
||||
@ -1792,6 +1816,8 @@ class i18n extends Object {
|
||||
* @param string $locale Locale to be loaded
|
||||
*/
|
||||
static function include_locale_file($module, $locale) {
|
||||
if(!self::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||
|
||||
if (file_exists($file = Director::getAbsFile("$module/lang/$locale.php"))) include_once($file);
|
||||
}
|
||||
|
||||
@ -1806,6 +1832,8 @@ class i18n extends Object {
|
||||
* and may need to reload them.
|
||||
*/
|
||||
static function include_by_locale($locale, $load_plugins = true, $force_load = false) {
|
||||
if(!self::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||
|
||||
global $lang;
|
||||
|
||||
$base = Director::baseFolder();
|
||||
|
@ -261,6 +261,14 @@ class i18nTest extends SapphireTest {
|
||||
$lang = $oldLang;
|
||||
}
|
||||
|
||||
function testValidateLocale() {
|
||||
$this->assertTrue(i18n::validate_locale('en_US'), 'Known locale in underscore format is valid');
|
||||
$this->assertTrue(i18n::validate_locale('en-US'), 'Known locale in dash format is valid');
|
||||
$this->assertFalse(i18n::validate_locale('en'), 'Short lang format is not valid');
|
||||
$this->assertFalse(i18n::validate_locale('xx_XX'), 'Unknown locale in correct format is not valid');
|
||||
$this->assertFalse(i18n::validate_locale(''), 'Empty string is not valid');
|
||||
}
|
||||
|
||||
static function translationTestPlugin($locale) {
|
||||
$result = array();
|
||||
$result["en_US"]["i18nTestProvider"]["foo"] = "baz_en";
|
||||
|
Loading…
x
Reference in New Issue
Block a user