Skip to content

Commit

Permalink
API CHANGE Using i18n::validate_locale() in various Translatable meth…
Browse files Browse the repository at this point in the history
…ods to ensure the locale exists (as defined through i18n::$allowed_locales) (from r114470)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@114474 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
chillu authored and Sam Minnee committed Feb 2, 2011
1 parent 924f0fe commit 1670dab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions core/model/Translatable.php
Expand Up @@ -268,6 +268,8 @@ static function default_locale() {
* @param $locale String
*/
static function set_default_locale($locale) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

$localeList = i18n::get_locale_list();
if(isset($localeList[$locale])) {
self::$default_locale = $locale;
Expand Down Expand Up @@ -295,6 +297,8 @@ static function get_current_locale() {
* @param string $lang New reading language.
*/
static function set_current_locale($locale) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

self::$current_locale = $locale;
}

Expand All @@ -308,6 +312,8 @@ static function set_current_locale($locale) {
* @return DataObject
*/
static function get_one_by_locale($class, $locale, $filter = '', $cache = false, $orderby = "") {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

$orig = Translatable::get_current_locale();
Translatable::set_current_locale($locale);
$do = DataObject::get_one($class, $filter, $cache, $orderby);
Expand All @@ -329,6 +335,8 @@ static function get_one_by_locale($class, $locale, $filter = '', $cache = false,
* @return mixed The objects matching the conditions.
*/
static function get_by_locale($class, $locale, $filter = '', $sort = '', $join = "", $limit = "", $containerClass = "DataObjectSet", $having = "") {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

$oldLang = self::get_current_locale();
self::set_current_locale($locale);
$result = DataObject::get($class, $filter, $sort, $join, $limit, $containerClass, $having);
Expand Down Expand Up @@ -1053,6 +1061,8 @@ function extendWithSuffix($table) {
* @return DataObjectSet
*/
function getTranslations($locale = null, $stage = null) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

if($this->owner->exists()) {
// HACK need to disable language filtering in augmentSQL(),
// as we purposely want to get different language
Expand Down Expand Up @@ -1103,6 +1113,8 @@ function getTranslations($locale = null, $stage = null) {
* @return DataObject Translated object
*/
function getTranslation($locale, $stage = null) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

$translations = $this->getTranslations($locale, $stage);
return ($translations) ? $translations->First() : null;
}
Expand All @@ -1120,6 +1132,8 @@ function getTranslation($locale, $stage = null) {
* @return DataObject The translated object
*/
function createTranslation($locale) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

if(!$this->owner->exists()) {
user_error('Translatable::createTranslation(): Please save your record before creating a translation', E_USER_ERROR);
}
Expand Down Expand Up @@ -1179,6 +1193,8 @@ function createTranslation($locale) {
* @return boolean
*/
function canTranslate($member = null, $locale) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();

// check for locale
Expand Down Expand Up @@ -1218,6 +1234,8 @@ function canEdit($member) {
* @return boolean
*/
function hasTranslation($locale) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));

return (
$this->owner->Locale == $locale
|| array_search($locale, $this->getTranslatedLocales()) !== false
Expand Down
2 changes: 1 addition & 1 deletion tests/model/TranslatableTest.php
Expand Up @@ -57,7 +57,7 @@ function testLocaleGetParamRedirectsToTranslation() {
$this->assertEquals(301, $response->getStatusCode(), 'Locale GET param causes redirect if it exists');
$this->assertContains($translatedPage->URLSegment, $response->getHeader('Location'));

$response = $this->get(Controller::join_links($origPage->URLSegment, '?locale=xx_XX'));
$response = $this->get(Controller::join_links($origPage->URLSegment, '?locale=fr_FR'));
$this->assertEquals(200, $response->getStatusCode(), 'Locale GET param without existing translation shows original page');
}

Expand Down

0 comments on commit 1670dab

Please sign in to comment.