mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Added DBLocale class for Translatable extension
API CHANGE Deprecated Translatable::get_langs_by_id() - use getTranslations() MINOR Removed Translatable::is_default_locale() - no deprecation necessary, was recently added MINOR Removed commented out methods in Translatable: augmentNumChildrenCountQuery(), augmentAllChildrenIncludingDeleted() and augmentStageChildren() MINOR Removed fieldsInExtraTables() and extendWithSuffix() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@75742 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
f2efb12bdd
commit
c1c920cca0
@ -247,14 +247,6 @@ class Translatable extends DataObjectDecorator {
|
|||||||
self::$default_locale = $locale;
|
self::$default_locale = $locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether the default and current reading language are the same.
|
|
||||||
* @return boolean Return true if both default and reading language are the same.
|
|
||||||
*/
|
|
||||||
static function is_default_locale() {
|
|
||||||
return (!self::current_locale() || self::$default_locale == self::current_locale());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current reading language.
|
* Get the current reading language.
|
||||||
* @return string
|
* @return string
|
||||||
@ -361,7 +353,9 @@ class Translatable extends DataObjectDecorator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of languages in which a given element has been translated
|
* Get a list of languages in which a given element has been translated.
|
||||||
|
*
|
||||||
|
* @deprecated 2.4 Use {@link getTranslations()}
|
||||||
*
|
*
|
||||||
* @param string $class Name of the class of the element
|
* @param string $class Name of the class of the element
|
||||||
* @param int $id ID of the element
|
* @param int $id ID of the element
|
||||||
@ -436,7 +430,7 @@ class Translatable extends DataObjectDecorator {
|
|||||||
if(get_class($this->owner) == ClassInfo::baseDataClass(get_class($this->owner))) {
|
if(get_class($this->owner) == ClassInfo::baseDataClass(get_class($this->owner))) {
|
||||||
return array(
|
return array(
|
||||||
"db" => array(
|
"db" => array(
|
||||||
"Locale" => "Varchar(12)",
|
"Locale" => "DBLocale",
|
||||||
//"TranslationMasterID" => "Int" // optional relation to a "translation master"
|
//"TranslationMasterID" => "Int" // optional relation to a "translation master"
|
||||||
),
|
),
|
||||||
"defaults" => array(
|
"defaults" => array(
|
||||||
@ -474,7 +468,7 @@ class Translatable extends DataObjectDecorator {
|
|||||||
//&& !$query->filtersOnFK()
|
//&& !$query->filtersOnFK()
|
||||||
) {
|
) {
|
||||||
$qry = "\"Locale\" = '$lang'";
|
$qry = "\"Locale\" = '$lang'";
|
||||||
if(Translatable::is_default_locale()) {
|
if(self::$default_locale == self::current_locale()) {
|
||||||
$qry .= " OR \"Locale\" = '' ";
|
$qry .= " OR \"Locale\" = '' ";
|
||||||
$qry .= " OR \"Locale\" IS NULL ";
|
$qry .= " OR \"Locale\" IS NULL ";
|
||||||
}
|
}
|
||||||
@ -611,14 +605,6 @@ class Translatable extends DataObjectDecorator {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
function augmentNumChildrenCountQuery(SQLQuery $query) {
|
|
||||||
if($this->isTranslation()) {
|
|
||||||
$query->where[0] = '"ParentID" = '.$this->getOriginalPage()->ID;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if a table needs Versioned support
|
* Determine if a table needs Versioned support
|
||||||
* This is called at db/build time
|
* This is called at db/build time
|
||||||
@ -879,17 +865,7 @@ class Translatable extends DataObjectDecorator {
|
|||||||
$langDropdown->addExtraClass('languageDropdown');
|
$langDropdown->addExtraClass('languageDropdown');
|
||||||
$createButton->addExtraClass('createTranslationButton');
|
$createButton->addExtraClass('createTranslationButton');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of fields from the tables created by this extension
|
|
||||||
*
|
|
||||||
* @param string $table Name of the table
|
|
||||||
* @return array Map where the keys are db, indexes and the values are the table fields
|
|
||||||
*/
|
|
||||||
function fieldsInExtraTables($table){
|
|
||||||
return array('db'=>null,'indexes'=>null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the names of all translatable fields on this class
|
* Get the names of all translatable fields on this class
|
||||||
* as a numeric array.
|
* as a numeric array.
|
||||||
@ -910,11 +886,7 @@ class Translatable extends DataObjectDecorator {
|
|||||||
$baseClass = array_shift($tableClasses);
|
$baseClass = array_shift($tableClasses);
|
||||||
return (!$stage || $stage == $this->defaultStage) ? $baseClass : $baseClass . "_$stage";
|
return (!$stage || $stage == $this->defaultStage) ? $baseClass : $baseClass . "_$stage";
|
||||||
}
|
}
|
||||||
|
|
||||||
function extendWithSuffix($table) {
|
|
||||||
return $table;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all related translations for the current object,
|
* Gets all related translations for the current object,
|
||||||
* excluding itself. See {@link getTranslation()} to retrieve
|
* excluding itself. See {@link getTranslation()} to retrieve
|
||||||
@ -1035,14 +1007,6 @@ class Translatable extends DataObjectDecorator {
|
|||||||
function hasTranslation($locale) {
|
function hasTranslation($locale) {
|
||||||
return (array_search($locale, $this->getTranslatedLangs()) !== false);
|
return (array_search($locale, $this->getTranslatedLangs()) !== false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
function augmentStageChildren(DataObjectSet $children, $showall = false) {
|
|
||||||
if($this->isTranslation()) {
|
|
||||||
$children->merge($this->getOriginalPage()->stageChildren($showall));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function AllChildrenIncludingDeleted($context = null) {
|
function AllChildrenIncludingDeleted($context = null) {
|
||||||
$children = $this->owner->doAllChildrenIncludingDeleted($context);
|
$children = $this->owner->doAllChildrenIncludingDeleted($context);
|
||||||
@ -1072,43 +1036,6 @@ class Translatable extends DataObjectDecorator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* If called with default language, doesn't affect the results.
|
|
||||||
* Otherwise (called in translation mode) the method tries to find translations
|
|
||||||
* for each page in its original language and replace the original.
|
|
||||||
* The result will contain a mixture of translated and untranslated pages.
|
|
||||||
*
|
|
||||||
* Caution: We also create a method AllChildrenIncludingDeleted() dynamically in the class constructor.
|
|
||||||
*
|
|
||||||
* @param DataObjectSet $untranslatedChildren
|
|
||||||
* @param Object $context
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
function augmentAllChildrenIncludingDeleted(DataObjectSet $children, $context) {
|
|
||||||
$find = array();
|
|
||||||
$replace = array();
|
|
||||||
|
|
||||||
if($context && $context->Locale && $context->Locale != Translatable::default_locale()) {
|
|
||||||
|
|
||||||
if($children) {
|
|
||||||
foreach($children as $child) {
|
|
||||||
if($child->hasTranslation($context->Locale)) {
|
|
||||||
$trans = $child->getTranslation($context->Locale);
|
|
||||||
if($trans) {
|
|
||||||
$find[] = $child;
|
|
||||||
$replace[] = $trans;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach($find as $i => $found) {
|
|
||||||
$children->replace($found, $replace[$i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a list of languages with at least one element translated in (including the default language)
|
* Get a list of languages with at least one element translated in (including the default language)
|
||||||
*
|
*
|
||||||
@ -1123,8 +1050,9 @@ class Translatable extends DataObjectDecorator {
|
|||||||
$returnMap = array();
|
$returnMap = array();
|
||||||
$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)
|
if($langCode && isset($allCodes[$langCode])) {
|
||||||
$returnMap[$langCode] = (is_array($allCodes[$langCode]) ? $allCodes[$langCode][0] : $allCodes[$langCode]);
|
$returnMap[$langCode] = (is_array($allCodes[$langCode])) ? $allCodes[$langCode][0] : $allCodes[$langCode];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $returnMap;
|
return $returnMap;
|
||||||
}
|
}
|
||||||
@ -1162,10 +1090,10 @@ class Translatable extends DataObjectDecorator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated 2.4 Use is_default_locale()
|
* @deprecated 2.4 Use custom check: self::$default_locale == self::current_locale()
|
||||||
*/
|
*/
|
||||||
static function is_default_lang() {
|
static function is_default_lang() {
|
||||||
return self::is_default_locale();
|
return (self::$default_locale == self::current_locale());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
33
core/model/fieldtypes/DBLocale.php
Normal file
33
core/model/fieldtypes/DBLocale.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Locale database field, mainly used in {@link Translatable} extension.
|
||||||
|
*
|
||||||
|
* @todo Allowing showing locale values in different languages through Nice()
|
||||||
|
*
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage i18n
|
||||||
|
*/
|
||||||
|
class DBLocale extends Varchar {
|
||||||
|
|
||||||
|
function __construct($name, $size = 16) {
|
||||||
|
parent::__construct($name, $size);
|
||||||
|
}
|
||||||
|
|
||||||
|
function Nice() {
|
||||||
|
return $this->getShortName();
|
||||||
|
}
|
||||||
|
|
||||||
|
function RFC1766() {
|
||||||
|
return i18n::convert_rfc1766($this->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getShortName() {
|
||||||
|
$common_names = i18n::get_common_locales();
|
||||||
|
return (isset($common_names[$this->value])) ? $common_names[$this->value] : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLongName() {
|
||||||
|
return i18n::get_locale_name($this->value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
12
tests/model/DBLocaleTest.php
Normal file
12
tests/model/DBLocaleTest.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
|
class DBLocaleTest extends SapphireTest {
|
||||||
|
function testNice() {
|
||||||
|
$l = DBField::create('DBLocale', 'de_DE');
|
||||||
|
$this->assertEquals($l->Nice(), 'German (Germany)');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
Loading…
x
Reference in New Issue
Block a user