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. (from r114469)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@114473 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-12-03 00:47:57 +00:00 committed by Sam Minnee
parent 7864d8f250
commit 924f0feb5d
2 changed files with 36 additions and 0 deletions

View File

@ -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, used as the default for * Set the current locale, used as the default for
* any localized classes, such as {@link FormField} or {@link DBField} * any localized classes, such as {@link FormField} or {@link DBField}
@ -1727,6 +1747,8 @@ class i18n extends Object {
* @param string $locale Locale to be set. See http://unicode.org/cldr/data/diff/supplemental/languages_and_territories.html for a list of possible locales. * @param string $locale Locale to be set. See http://unicode.org/cldr/data/diff/supplemental/languages_and_territories.html for a list of possible locales.
*/ */
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;
} }
@ -1779,6 +1801,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;
} }
@ -1807,6 +1831,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);
} }
@ -1821,6 +1847,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

@ -261,6 +261,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();
$result["en_US"]["i18nTestProvider"]["foo"] = "baz_en"; $result["en_US"]["i18nTestProvider"]["foo"] = "baz_en";