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:
Ingo Schommer 2010-12-03 00:27:41 +00:00
parent 640b504ebe
commit 17ac4753fd
2 changed files with 36 additions and 0 deletions

View File

@ -1714,6 +1714,26 @@ class i18n extends Object {
return (isset($module)) ? $module[1] : false; return (isset($module)) ? $module[1] : false;
} }
/**
* 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 * Set the current locale
@ -1722,6 +1742,8 @@ class i18n extends Object {
* @param string $locale Locale to be set * @param string $locale Locale to be set
*/ */
static function set_locale($locale) { static function set_locale($locale) {
if(!self::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
if ($locale) self::$current_locale = $locale; if ($locale) self::$current_locale = $locale;
} }
@ -1764,6 +1786,8 @@ class i18n extends Object {
* @param String $locale * @param String $locale
*/ */
static function set_default_locale($locale) { static function set_default_locale($locale) {
if(!self::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
self::$default_locale = $locale; self::$default_locale = $locale;
} }
@ -1792,6 +1816,8 @@ class i18n extends Object {
* @param string $locale Locale to be loaded * @param string $locale Locale to be loaded
*/ */
static function include_locale_file($module, $locale) { 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); 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. * and may need to reload them.
*/ */
static function include_by_locale($locale, $load_plugins = true, $force_load = false) { 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; global $lang;
$base = Director::baseFolder(); $base = Director::baseFolder();

View File

@ -260,6 +260,14 @@ class i18nTest extends SapphireTest {
$lang = $oldLang; $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) { static function translationTestPlugin($locale) {
$result = array(); $result = array();