mirror of
https://github.com/silverstripe/silverstripe-translatable
synced 2024-10-22 09:05:59 +00:00
Coding conventions fixes (mostly line length)
This commit is contained in:
parent
4035f50717
commit
51c7d9e2ef
@ -30,7 +30,8 @@ class TranslatableCMSMainExtension extends Extension {
|
|||||||
} else {
|
} else {
|
||||||
$this->owner->Locale = Translatable::default_locale();
|
$this->owner->Locale = Translatable::default_locale();
|
||||||
if ($this->owner->class == 'CMSPagesController') {
|
if ($this->owner->class == 'CMSPagesController') {
|
||||||
// the CMSPagesController always needs to have the locale set, otherwise page editing will cause an extra
|
// the CMSPagesController always needs to have the locale set,
|
||||||
|
// otherwise page editing will cause an extra
|
||||||
// ajax request which looks weird due to multiple "loading"-flashes
|
// ajax request which looks weird due to multiple "loading"-flashes
|
||||||
return $this->owner->redirect($this->owner->Link());
|
return $this->owner->redirect($this->owner->Link());
|
||||||
}
|
}
|
||||||
@ -38,9 +39,17 @@ class TranslatableCMSMainExtension extends Extension {
|
|||||||
Translatable::set_current_locale($this->owner->Locale);
|
Translatable::set_current_locale($this->owner->Locale);
|
||||||
|
|
||||||
// if a locale is set, it needs to match to the current record
|
// if a locale is set, it needs to match to the current record
|
||||||
$requestLocale = $req->requestVar("Locale") ? $req->requestVar("Locale") : $req->requestVar("locale");
|
if($req->requestVar("Locale")) {
|
||||||
|
$requestLocale = $req->requestVar("Locale");
|
||||||
|
} else {
|
||||||
|
$requestLocale = $req->requestVar("locale");
|
||||||
|
}
|
||||||
|
|
||||||
$page = $this->owner->currentPage();
|
$page = $this->owner->currentPage();
|
||||||
if($requestLocale && $page && $page->hasExtension('Translatable') && $page->Locale != $requestLocale) {
|
if(
|
||||||
|
$requestLocale && $page && $page->hasExtension('Translatable')
|
||||||
|
&& $page->Locale != $requestLocale
|
||||||
|
) {
|
||||||
$transPage = $page->getTranslation($requestLocale);
|
$transPage = $page->getTranslation($requestLocale);
|
||||||
if($transPage) {
|
if($transPage) {
|
||||||
Translatable::set_current_locale($transPage->Locale);
|
Translatable::set_current_locale($transPage->Locale);
|
||||||
@ -61,7 +70,10 @@ class TranslatableCMSMainExtension extends Extension {
|
|||||||
// collect languages for TinyMCE spellchecker plugin.
|
// collect languages for TinyMCE spellchecker plugin.
|
||||||
// see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
|
// see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
|
||||||
$langName = i18n::get_locale_name($this->owner->Locale);
|
$langName = i18n::get_locale_name($this->owner->Locale);
|
||||||
HtmlEditorConfig::get('cms')->setOption('spellchecker_languages', "+{$langName}={$this->owner->Locale}");
|
HtmlEditorConfig::get('cms')->setOption(
|
||||||
|
'spellchecker_languages',
|
||||||
|
"+{$langName}={$this->owner->Locale}"
|
||||||
|
);
|
||||||
|
|
||||||
Requirements::javascript('translatable/javascript/CMSMain.Translatable.js');
|
Requirements::javascript('translatable/javascript/CMSMain.Translatable.js');
|
||||||
Requirements::css('translatable/css/CMSMain.Translatable.css');
|
Requirements::css('translatable/css/CMSMain.Translatable.css');
|
||||||
@ -98,7 +110,8 @@ class TranslatableCMSMainExtension extends Extension {
|
|||||||
// to the usual "create page" pattern of storing the record
|
// to the usual "create page" pattern of storing the record
|
||||||
// in-memory until a "save" is performed by the user, mainly
|
// in-memory until a "save" is performed by the user, mainly
|
||||||
// to simplify things a bit.
|
// to simplify things a bit.
|
||||||
// @todo Allow in-memory creation of translations that don't persist in the database before the user requests it
|
// @todo Allow in-memory creation of translations that don't
|
||||||
|
// persist in the database before the user requests it
|
||||||
$translatedRecord = $record->createTranslation($langCode);
|
$translatedRecord = $record->createTranslation($langCode);
|
||||||
|
|
||||||
$url = Controller::join_links(
|
$url = Controller::join_links(
|
||||||
|
@ -12,10 +12,14 @@ class LanguageDropdownField extends GroupedDropdownField {
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $title
|
* @param string $title
|
||||||
* @param array $excludeLocales List of locales that won't be included
|
* @param array $excludeLocales List of locales that won't be included
|
||||||
* @param string $translatingClass Name of the class with translated instances where to look for used languages
|
* @param string $translatingClass Name of the class with translated instances
|
||||||
* @param string $list Indicates the source language list. Can be either Common-English, Common-Native, Locale-English, Locale-Native
|
* where to look for used languages
|
||||||
|
* @param string $list Indicates the source language list.
|
||||||
|
* Can be either Common-English, Common-Native, Locale-English, Locale-Native
|
||||||
*/
|
*/
|
||||||
function __construct($name, $title, $excludeLocales = array(), $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null) {
|
function __construct($name, $title, $excludeLocales = array(),
|
||||||
|
$translatingClass = 'SiteTree', $list = 'Common-English', $instance = null
|
||||||
|
) {
|
||||||
$usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
|
$usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
|
||||||
$usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
|
$usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
|
||||||
|
|
||||||
@ -25,7 +29,9 @@ class LanguageDropdownField extends GroupedDropdownField {
|
|||||||
else if('Locale-Native' == $list) $allLocalesWithTitle = i18n::get_common_locales(true);
|
else if('Locale-Native' == $list) $allLocalesWithTitle = i18n::get_common_locales(true);
|
||||||
else $allLocalesWithTitle = i18n::get_locale_list();
|
else $allLocalesWithTitle = i18n::get_locale_list();
|
||||||
|
|
||||||
if(isset($allLocales[Translatable::default_locale()])) unset($allLocales[Translatable::default_locale()]);
|
if(isset($allLocales[Translatable::default_locale()])) {
|
||||||
|
unset($allLocales[Translatable::default_locale()]);
|
||||||
|
}
|
||||||
|
|
||||||
// Limit to allowed locales if defined
|
// Limit to allowed locales if defined
|
||||||
// Check for canTranslate() if an $instance is given
|
// Check for canTranslate() if an $instance is given
|
||||||
|
@ -41,7 +41,8 @@
|
|||||||
* Translatable::set_default_locale(<locale>); // e.g. 'de_DE' or 'fr_FR'
|
* Translatable::set_default_locale(<locale>); // e.g. 'de_DE' or 'fr_FR'
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* For the Translatable class, a "locale" consists of a language code plus a region code separated by an underscore,
|
* For the Translatable class, a "locale" consists of a language code plus a region
|
||||||
|
* code separated by an underscore,
|
||||||
* for example "de_AT" for German language ("de") in the region Austria ("AT").
|
* for example "de_AT" for German language ("de") in the region Austria ("AT").
|
||||||
* See http://www.w3.org/International/articles/language-tags/ for a detailed description.
|
* See http://www.w3.org/International/articles/language-tags/ for a detailed description.
|
||||||
*
|
*
|
||||||
@ -225,7 +226,8 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
/**
|
/**
|
||||||
* Choose the language the site is currently on.
|
* Choose the language the site is currently on.
|
||||||
*
|
*
|
||||||
* If $_GET['locale'] is currently set, then that locale will be used. Otherwise the member preference (if logged
|
* If $_GET['locale'] is currently set, then that locale will be used.
|
||||||
|
* Otherwise the member preference (if logged
|
||||||
* in) or default locale will be used.
|
* in) or default locale will be used.
|
||||||
*
|
*
|
||||||
* @todo Re-implement cookie and member option
|
* @todo Re-implement cookie and member option
|
||||||
@ -238,7 +240,11 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
return self::$current_locale;
|
return self::$current_locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((isset($_REQUEST['locale']) && !$langsAvailable) || (isset($_REQUEST['locale']) && in_array($_REQUEST['locale'], $langsAvailable))) {
|
if(
|
||||||
|
(isset($_REQUEST['locale']) && !$langsAvailable)
|
||||||
|
|| (isset($_REQUEST['locale'])
|
||||||
|
&& in_array($_REQUEST['locale'], $langsAvailable))
|
||||||
|
) {
|
||||||
// get from request parameter
|
// get from request parameter
|
||||||
self::set_current_locale($_REQUEST['locale']);
|
self::set_current_locale($_REQUEST['locale']);
|
||||||
} else {
|
} else {
|
||||||
@ -267,13 +273,18 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @param $locale String
|
* @param $locale String
|
||||||
*/
|
*/
|
||||||
static function set_default_locale($locale) {
|
static function set_default_locale($locale) {
|
||||||
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
if($locale && !i18n::validate_locale($locale)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||||
|
}
|
||||||
|
|
||||||
$localeList = i18n::get_locale_list();
|
$localeList = i18n::get_locale_list();
|
||||||
if(isset($localeList[$locale])) {
|
if(isset($localeList[$locale])) {
|
||||||
self::$default_locale = $locale;
|
self::$default_locale = $locale;
|
||||||
} else {
|
} else {
|
||||||
user_error("Translatable::set_default_locale(): '$locale' is not a valid locale.", E_USER_WARNING);
|
user_error(
|
||||||
|
"Translatable::set_default_locale(): '$locale' is not a valid locale.",
|
||||||
|
E_USER_WARNING
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,7 +307,9 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @param string $lang New reading language.
|
* @param string $lang New reading language.
|
||||||
*/
|
*/
|
||||||
static function set_current_locale($locale) {
|
static function set_current_locale($locale) {
|
||||||
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
if($locale && !i18n::validate_locale($locale)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||||
|
}
|
||||||
|
|
||||||
self::$current_locale = $locale;
|
self::$current_locale = $locale;
|
||||||
}
|
}
|
||||||
@ -311,7 +324,9 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @return DataObject
|
* @return DataObject
|
||||||
*/
|
*/
|
||||||
static function get_one_by_locale($class, $locale, $filter = '', $cache = false, $orderby = "") {
|
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));
|
if($locale && !i18n::validate_locale($locale)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||||
|
}
|
||||||
|
|
||||||
$orig = Translatable::get_current_locale();
|
$orig = Translatable::get_current_locale();
|
||||||
Translatable::set_current_locale($locale);
|
Translatable::set_current_locale($locale);
|
||||||
@ -327,14 +342,17 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @param string $locale The name of the language
|
* @param string $locale The name of the language
|
||||||
* @param string $filter A filter to be inserted into the WHERE clause.
|
* @param string $filter A filter to be inserted into the WHERE clause.
|
||||||
* @param string $sort A sort expression to be inserted into the ORDER BY clause.
|
* @param string $sort A sort expression to be inserted into the ORDER BY clause.
|
||||||
* @param string $join A single join clause. This can be used for filtering, only 1 instance of each DataObject will be returned.
|
* @param string $join A single join clause. This can be used for filtering, only 1
|
||||||
|
* instance of each DataObject will be returned.
|
||||||
* @param string $limit A limit expression to be inserted into the LIMIT clause.
|
* @param string $limit A limit expression to be inserted into the LIMIT clause.
|
||||||
* @param string $containerClass The container class to return the results in.
|
* @param string $containerClass The container class to return the results in.
|
||||||
* @param string $having A filter to be inserted into the HAVING clause.
|
* @param string $having A filter to be inserted into the HAVING clause.
|
||||||
* @return mixed The objects matching the conditions.
|
* @return mixed The objects matching the conditions.
|
||||||
*/
|
*/
|
||||||
static function get_by_locale($class, $locale, $filter = '', $sort = '', $join = "", $limit = "") {
|
static function get_by_locale($class, $locale, $filter = '', $sort = '', $join = "", $limit = "") {
|
||||||
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
if($locale && !i18n::validate_locale($locale)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||||
|
}
|
||||||
|
|
||||||
$oldLang = self::get_current_locale();
|
$oldLang = self::get_current_locale();
|
||||||
self::set_current_locale($locale);
|
self::set_current_locale($locale);
|
||||||
@ -492,7 +510,8 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
function __construct($translatableFields = null) {
|
function __construct($translatableFields = null) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
// @todo Disabled selection of translatable fields - we're setting all fields as translatable in setOwner()
|
// @todo Disabled selection of translatable fields - we're setting all fields as
|
||||||
|
// translatable in setOwner()
|
||||||
/*
|
/*
|
||||||
if(!is_array($translatableFields)) {
|
if(!is_array($translatableFields)) {
|
||||||
$translatableFields = func_get_args();
|
$translatableFields = func_get_args();
|
||||||
@ -544,7 +563,12 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
// If the record is saved (and not a singleton), and has a locale,
|
// If the record is saved (and not a singleton), and has a locale,
|
||||||
// limit the current call to its locale. This fixes a lot of problems
|
// limit the current call to its locale. This fixes a lot of problems
|
||||||
// with other extensions like Versioned
|
// with other extensions like Versioned
|
||||||
$locale = ($this->owner->ID && !empty($this->owner->Locale)) ? $this->owner->Locale : Translatable::get_current_locale();
|
if($this->owner->ID && !empty($this->owner->Locale)) {
|
||||||
|
$locale = $this->owner->Locale;
|
||||||
|
} else {
|
||||||
|
$locale = Translatable::get_current_locale();
|
||||||
|
}
|
||||||
|
|
||||||
$baseTable = ClassInfo::baseDataClass($this->owner->class);
|
$baseTable = ClassInfo::baseDataClass($this->owner->class);
|
||||||
if(
|
if(
|
||||||
$locale
|
$locale
|
||||||
@ -555,7 +579,8 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
// the query contains this table
|
// the query contains this table
|
||||||
// @todo Isn't this always the case?!
|
// @todo Isn't this always the case?!
|
||||||
&& array_search($baseTable, array_keys($query->getFrom())) !== false
|
&& array_search($baseTable, array_keys($query->getFrom())) !== false
|
||||||
// or we're already filtering by Lang (either from an earlier augmentSQL() call or through custom SQL filters)
|
// or we're already filtering by Lang (either from an earlier augmentSQL()
|
||||||
|
// call or through custom SQL filters)
|
||||||
&& !preg_match('/("|\'|`)Locale("|\'|`)/', implode(' ', $query->getWhere()))
|
&& !preg_match('/("|\'|`)Locale("|\'|`)/', implode(' ', $query->getWhere()))
|
||||||
//&& !$query->filtersOnFK()
|
//&& !$query->filtersOnFK()
|
||||||
) {
|
) {
|
||||||
@ -606,7 +631,11 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
// Auto-setting permissions based on these intransparent criteria is a bit hacky,
|
// Auto-setting permissions based on these intransparent criteria is a bit hacky,
|
||||||
// but unavoidable until we can determine when a certain permission code was made available first
|
// but unavoidable until we can determine when a certain permission code was made available first
|
||||||
// (see http://open.silverstripe.org/ticket/4940)
|
// (see http://open.silverstripe.org/ticket/4940)
|
||||||
$groups = Permission::get_groups_by_permission(array('CMS_ACCESS_CMSMain','CMS_ACCESS_LeftAndMain','ADMIN'));
|
$groups = Permission::get_groups_by_permission(array(
|
||||||
|
'CMS_ACCESS_CMSMain',
|
||||||
|
'CMS_ACCESS_LeftAndMain',
|
||||||
|
'ADMIN'
|
||||||
|
));
|
||||||
if($groups) foreach($groups as $group) {
|
if($groups) foreach($groups as $group) {
|
||||||
$codes = $group->Permissions()->column('Code');
|
$codes = $group->Permissions()->column('Code');
|
||||||
$hasTranslationCode = false;
|
$hasTranslationCode = false;
|
||||||
@ -714,7 +743,11 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
|
|
||||||
$baseDataClass = ClassInfo::baseDataClass($this->owner->class);
|
$baseDataClass = ClassInfo::baseDataClass($this->owner->class);
|
||||||
return DB::query(
|
return DB::query(
|
||||||
sprintf('SELECT "TranslationGroupID" FROM "%s_translationgroups" WHERE "OriginalID" = %d', $baseDataClass, $this->owner->ID)
|
sprintf(
|
||||||
|
'SELECT "TranslationGroupID" FROM "%s_translationgroups" WHERE "OriginalID" = %d',
|
||||||
|
$baseDataClass,
|
||||||
|
$this->owner->ID
|
||||||
|
)
|
||||||
)->value();
|
)->value();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -785,7 +818,11 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
// Caution: This logic is very sensitve to infinite loops when translation status isn't determined properly
|
// Caution: This logic is very sensitve to infinite loops when translation status isn't determined properly
|
||||||
// If a parent for the newly written translation was existing before this
|
// If a parent for the newly written translation was existing before this
|
||||||
// onBeforeWrite() call, it will already have been linked correctly through createTranslation()
|
// onBeforeWrite() call, it will already have been linked correctly through createTranslation()
|
||||||
if(class_exists('SiteTree') && $this->owner->hasField('ParentID') && $this->owner instanceof SiteTree) {
|
if(
|
||||||
|
class_exists('SiteTree')
|
||||||
|
&& $this->owner->hasField('ParentID')
|
||||||
|
&& $this->owner instanceof SiteTree
|
||||||
|
) {
|
||||||
if(
|
if(
|
||||||
!$this->owner->ID
|
!$this->owner->ID
|
||||||
&& $this->owner->ParentID
|
&& $this->owner->ParentID
|
||||||
@ -828,7 +865,11 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
// created through createTranslation(), meaning they don't
|
// created through createTranslation(), meaning they don't
|
||||||
// have the translation group automatically set.
|
// have the translation group automatically set.
|
||||||
$translationGroupID = $this->getTranslationGroup();
|
$translationGroupID = $this->getTranslationGroup();
|
||||||
if(!$translationGroupID) $this->addTranslationGroup($this->owner->_TranslationGroupID ? $this->owner->_TranslationGroupID : $this->owner->ID);
|
if(!$translationGroupID) {
|
||||||
|
$this->addTranslationGroup(
|
||||||
|
$this->owner->_TranslationGroupID ? $this->owner->_TranslationGroupID : $this->owner->ID
|
||||||
|
);
|
||||||
|
}
|
||||||
unset($this->owner->_TranslatableIsNewRecord);
|
unset($this->owner->_TranslatableIsNewRecord);
|
||||||
unset($this->owner->_TranslationGroupID);
|
unset($this->owner->_TranslationGroupID);
|
||||||
}
|
}
|
||||||
@ -859,7 +900,8 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @return SiteTree
|
* @return SiteTree
|
||||||
*/
|
*/
|
||||||
public function alternateGetByLink($URLSegment, $parentID) {
|
public function alternateGetByLink($URLSegment, $parentID) {
|
||||||
// If the parentID value has come from a translated page, then we need to find the corresponding parentID value
|
// If the parentID value has come from a translated page,
|
||||||
|
// then we need to find the corresponding parentID value
|
||||||
// in the default Locale.
|
// in the default Locale.
|
||||||
if (
|
if (
|
||||||
is_int($parentID)
|
is_int($parentID)
|
||||||
@ -921,7 +963,10 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
'Locale-English',
|
'Locale-English',
|
||||||
$this->owner
|
$this->owner
|
||||||
),
|
),
|
||||||
$createButton = new InlineFormAction('createtranslation',_t('Translatable.CREATEBUTTON', 'Create'))
|
$createButton = new InlineFormAction(
|
||||||
|
'createtranslation',
|
||||||
|
_t('Translatable.CREATEBUTTON', 'Create')
|
||||||
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$createButton->includeDefaultJS(false);
|
$createButton->includeDefaultJS(false);
|
||||||
@ -1076,7 +1121,9 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @return DataObjectSet
|
* @return DataObjectSet
|
||||||
*/
|
*/
|
||||||
function getTranslations($locale = null, $stage = null) {
|
function getTranslations($locale = null, $stage = null) {
|
||||||
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
if($locale && !i18n::validate_locale($locale)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||||
|
}
|
||||||
|
|
||||||
if($this->owner->exists()) {
|
if($this->owner->exists()) {
|
||||||
// HACK need to disable language filtering in augmentSQL(),
|
// HACK need to disable language filtering in augmentSQL(),
|
||||||
@ -1126,7 +1173,9 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @return DataObject Translated object
|
* @return DataObject Translated object
|
||||||
*/
|
*/
|
||||||
function getTranslation($locale, $stage = null) {
|
function getTranslation($locale, $stage = null) {
|
||||||
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
if($locale && !i18n::validate_locale($locale)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||||
|
}
|
||||||
|
|
||||||
$translations = $this->getTranslations($locale, $stage);
|
$translations = $this->getTranslations($locale, $stage);
|
||||||
return ($translations) ? $translations->First() : null;
|
return ($translations) ? $translations->First() : null;
|
||||||
@ -1151,7 +1200,9 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
|
|
||||||
// Find the best base translation for SiteConfig
|
// Find the best base translation for SiteConfig
|
||||||
Translatable::disable_locale_filter();
|
Translatable::disable_locale_filter();
|
||||||
$existingConfig = SiteConfig::get()->filter(array('Locale' => Translatable::default_locale()))->first();
|
$existingConfig = SiteConfig::get()->filter(array(
|
||||||
|
'Locale' => Translatable::default_locale()
|
||||||
|
))->first();
|
||||||
if(!$existingConfig) $existingConfig = SiteConfig::get()->first();
|
if(!$existingConfig) $existingConfig = SiteConfig::get()->first();
|
||||||
Translatable::enable_locale_filter();
|
Translatable::enable_locale_filter();
|
||||||
|
|
||||||
@ -1188,8 +1239,11 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* Hooks into the DataObject::populateDefaults() method
|
* Hooks into the DataObject::populateDefaults() method
|
||||||
*/
|
*/
|
||||||
public function populateDefaults() {
|
public function populateDefaults() {
|
||||||
if (empty($this->owner->ID) && ($this->owner instanceof SiteConfig) && self::$enable_siteconfig_generation)
|
if (
|
||||||
{
|
empty($this->owner->ID)
|
||||||
|
&& ($this->owner instanceof SiteConfig)
|
||||||
|
&& self::$enable_siteconfig_generation
|
||||||
|
) {
|
||||||
// Use enable_siteconfig_generation to prevent infinite loop during object creation
|
// Use enable_siteconfig_generation to prevent infinite loop during object creation
|
||||||
self::$enable_siteconfig_generation = false;
|
self::$enable_siteconfig_generation = false;
|
||||||
$this->populateSiteConfigDefaults();
|
$this->populateSiteConfigDefaults();
|
||||||
@ -1212,10 +1266,15 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @return DataObject The translated object
|
* @return DataObject The translated object
|
||||||
*/
|
*/
|
||||||
function createTranslation($locale, $saveTranslation = true) {
|
function createTranslation($locale, $saveTranslation = true) {
|
||||||
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
if($locale && !i18n::validate_locale($locale)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||||
|
}
|
||||||
|
|
||||||
if(!$this->owner->exists()) {
|
if(!$this->owner->exists()) {
|
||||||
user_error('Translatable::createTranslation(): Please save your record before creating a translation', E_USER_ERROR);
|
user_error(
|
||||||
|
'Translatable::createTranslation(): Please save your record before creating a translation',
|
||||||
|
E_USER_ERROR
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// permission check
|
// permission check
|
||||||
@ -1273,7 +1332,9 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
function canTranslate($member = null, $locale) {
|
function canTranslate($member = null, $locale) {
|
||||||
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $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();
|
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
|
||||||
|
|
||||||
@ -1314,7 +1375,9 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
function hasTranslation($locale) {
|
function hasTranslation($locale) {
|
||||||
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
if($locale && !i18n::validate_locale($locale)) {
|
||||||
|
throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
$this->owner->Locale == $locale
|
$this->owner->Locale == $locale
|
||||||
@ -1397,7 +1460,11 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
$allCodes = array_merge(i18n::$all_locales, i18n::$common_locales);
|
$allCodes = array_merge(i18n::$all_locales, i18n::$common_locales);
|
||||||
foreach ($langlist as $langCode) {
|
foreach ($langlist as $langCode) {
|
||||||
if($langCode && isset($allCodes[$langCode])) {
|
if($langCode && isset($allCodes[$langCode])) {
|
||||||
$returnMap[$langCode] = (is_array($allCodes[$langCode])) ? $allCodes[$langCode][0] : $allCodes[$langCode];
|
if(is_array($allCodes[$langCode])) {
|
||||||
|
$returnMap[$langCode] = $allCodes[$langCode][0];
|
||||||
|
} else {
|
||||||
|
$returnMap[$langCode] = $allCodes[$langCode];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $returnMap;
|
return $returnMap;
|
||||||
@ -1515,8 +1582,13 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
/**
|
/**
|
||||||
* @deprecated 2.4 Use get_by_locale()
|
* @deprecated 2.4 Use get_by_locale()
|
||||||
*/
|
*/
|
||||||
static function get_by_lang($class, $lang, $filter = '', $sort = '', $join = "", $limit = "", $containerClass = "DataObjectSet", $having = "") {
|
static function get_by_lang($class, $lang, $filter = '', $sort = '',
|
||||||
return self::get_by_locale($class, i18n::get_locale_from_lang($lang), $filter, $sort, $join, $limit, $containerClass, $having);
|
$join = "", $limit = "", $containerClass = "DataObjectSet", $having = ""
|
||||||
|
) {
|
||||||
|
return self::get_by_locale(
|
||||||
|
$class, i18n::get_locale_from_lang($lang), $filter,
|
||||||
|
$sort, $join, $limit, $containerClass, $having
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1599,7 +1671,8 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* consisting of the original formfield plus a readonly-version
|
* consisting of the original formfield plus a readonly-version
|
||||||
* of the original value, wrapped in a CompositeField.
|
* of the original value, wrapped in a CompositeField.
|
||||||
*
|
*
|
||||||
* @param DataObject $original Needs the original record as we populate the readonly formfield with the original value
|
* @param DataObject $original Needs the original record as we populate
|
||||||
|
* the readonly formfield with the original value
|
||||||
*
|
*
|
||||||
* @package sapphire
|
* @package sapphire
|
||||||
* @subpackage misc
|
* @subpackage misc
|
||||||
@ -1657,7 +1730,4 @@ class Translatable_Transformation extends FormTransformation {
|
|||||||
return $nonEditableField_holder;
|
return $nonEditableField_holder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
|
||||||
|
@ -111,7 +111,10 @@ class MigrateTranslatableTask extends BuildTask {
|
|||||||
$existingTrans = $original->getTranslation($newLocale, $stage);
|
$existingTrans = $original->getTranslation($newLocale, $stage);
|
||||||
|
|
||||||
if($existingTrans) {
|
if($existingTrans) {
|
||||||
echo sprintf("Found existing new-style translation for #%d. Already merged? Skipping.\n", $oldtrans['OriginalLangID']);
|
echo sprintf(
|
||||||
|
"Found existing new-style translation for #%d. Already merged? Skipping.\n",
|
||||||
|
$oldtrans['OriginalLangID']
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +133,8 @@ class MigrateTranslatableTask extends BuildTask {
|
|||||||
foreach(ClassInfo::ancestry($oldtrans['ClassName']) as $classname) {
|
foreach(ClassInfo::ancestry($oldtrans['ClassName']) as $classname) {
|
||||||
$oldtransitem = false;
|
$oldtransitem = false;
|
||||||
|
|
||||||
// If the class is SiteTree, we already have the DB record, else check for the table and get the record
|
// If the class is SiteTree, we already have the DB record,
|
||||||
|
// else check for the table and get the record
|
||||||
if($classname == 'SiteTree') {
|
if($classname == 'SiteTree') {
|
||||||
$oldtransitem = $oldtrans;
|
$oldtransitem = $oldtrans;
|
||||||
} elseif(in_array(strtolower($classname) . '_lang', DB::tableList())) {
|
} elseif(in_array(strtolower($classname) . '_lang', DB::tableList())) {
|
||||||
|
@ -41,7 +41,11 @@ class TranslatableSiteConfigTest extends SapphireTest {
|
|||||||
$this->assertInstanceOf('SiteConfig', $configFr);
|
$this->assertInstanceOf('SiteConfig', $configFr);
|
||||||
$this->assertEquals($configFr->Locale, 'fr_FR');
|
$this->assertEquals($configFr->Locale, 'fr_FR');
|
||||||
$this->assertEquals($configFr->Title, $configEn->Title, 'Copies title from existing config');
|
$this->assertEquals($configFr->Title, $configEn->Title, 'Copies title from existing config');
|
||||||
$this->assertEquals($configFr->getTranslationGroup(), $configEn->getTranslationGroup(), 'Created in the same translation group');
|
$this->assertEquals(
|
||||||
|
$configFr->getTranslationGroup(),
|
||||||
|
$configEn->getTranslationGroup(),
|
||||||
|
'Created in the same translation group'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testCanEditTranslatedRootPages() {
|
function testCanEditTranslatedRootPages() {
|
||||||
|
@ -60,7 +60,9 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
$this->assertContains($translatedPage->URLSegment, $response->getHeader('Location'));
|
$this->assertContains($translatedPage->URLSegment, $response->getHeader('Location'));
|
||||||
|
|
||||||
$response = $this->get(Controller::join_links($origPage->URLSegment, '?locale=fr_FR'));
|
$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');
|
$this->assertEquals(200, $response->getStatusCode(),
|
||||||
|
'Locale GET param without existing translation shows original page'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testTranslationGroups() {
|
function testTranslationGroups() {
|
||||||
@ -657,9 +659,11 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
$translatedParentPage->AllChildrenIncludingDeleted()->column('ID'),
|
$translatedParentPage->AllChildrenIncludingDeleted()->column('ID'),
|
||||||
array(
|
array(
|
||||||
$child2PageTranslatedID,
|
$child2PageTranslatedID,
|
||||||
$child1PageTranslatedID // $child1PageTranslated was deleted from stage, so the original record doesn't have the ID set
|
// $child1PageTranslated was deleted from stage, so the original record doesn't have the ID set
|
||||||
|
$child1PageTranslatedID
|
||||||
),
|
),
|
||||||
"Showing AllChildrenIncludingDeleted() in translation mode with parent page in translated language shows children in translated language"
|
"Showing AllChildrenIncludingDeleted() in translation mode with parent page in " .
|
||||||
|
"translated language shows children in translated language"
|
||||||
);
|
);
|
||||||
|
|
||||||
Translatable::set_current_locale('de_DE');
|
Translatable::set_current_locale('de_DE');
|
||||||
@ -668,7 +672,8 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$parentPage->AllChildrenIncludingDeleted()->column('ID'),
|
$parentPage->AllChildrenIncludingDeleted()->column('ID'),
|
||||||
array(),
|
array(),
|
||||||
"Showing AllChildrenIncludingDeleted() in translation mode with parent page in translated language shows children in default language"
|
"Showing AllChildrenIncludingDeleted() in translation mode with parent page in " .
|
||||||
|
"translated language shows children in default language"
|
||||||
);
|
);
|
||||||
|
|
||||||
// reset language
|
// reset language
|
||||||
@ -790,11 +795,13 @@ class TranslatableTest extends FunctionalTest {
|
|||||||
Translatable::set_allowed_locales(array('ja_JP'));
|
Translatable::set_allowed_locales(array('ja_JP'));
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
$testPage->canTranslate($cmseditor, 'ja_JP'),
|
$testPage->canTranslate($cmseditor, 'ja_JP'),
|
||||||
"Users with canEdit() and TRANSLATE_ALL permission can create a new translation if locale is in Translatable::get_allowed_locales()"
|
"Users with canEdit() and TRANSLATE_ALL permission can create a new translation " .
|
||||||
|
"if locale is in Translatable::get_allowed_locales()"
|
||||||
);
|
);
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
$testPage->canTranslate($cmseditor, 'de_DE'),
|
$testPage->canTranslate($cmseditor, 'de_DE'),
|
||||||
"Users with canEdit() and TRANSLATE_ALL permission can't create a new translation if locale is not in Translatable::get_allowed_locales()"
|
"Users with canEdit() and TRANSLATE_ALL permission can't create a new translation if " .
|
||||||
|
"locale is not in Translatable::get_allowed_locales()"
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user