Merge pull request #193 from chillu/pulls/consistent-locale-casing

Consistent locale casing, set locale on POST
This commit is contained in:
Damian Mooyman 2015-08-20 17:14:46 +12:00
commit 295e11c0e0
4 changed files with 14 additions and 21 deletions

View File

@ -16,9 +16,6 @@ class TranslatableCMSMainExtension extends Extension {
// as an intermediary rather than the endpoint controller // as an intermediary rather than the endpoint controller
if(!$this->owner->stat('tree_class')) return; if(!$this->owner->stat('tree_class')) return;
// Leave form submissions alone
if($req->httpMethod() != 'GET') return;
// Locale" attribute is either explicitly added by LeftAndMain Javascript logic, // Locale" attribute is either explicitly added by LeftAndMain Javascript logic,
// or implied on a translated record (see {@link Translatable->updateCMSFields()}). // or implied on a translated record (see {@link Translatable->updateCMSFields()}).
// $Lang serves as a "context" which can be inspected by Translatable - hence it // $Lang serves as a "context" which can be inspected by Translatable - hence it
@ -26,8 +23,6 @@ class TranslatableCMSMainExtension extends Extension {
$id = $req->param('ID'); $id = $req->param('ID');
if($req->requestVar("Locale")) { if($req->requestVar("Locale")) {
$this->owner->Locale = $req->requestVar("Locale"); $this->owner->Locale = $req->requestVar("Locale");
} elseif($req->requestVar("locale")) {
$this->owner->Locale = $req->requestVar("locale");
} else if($id && is_numeric($id)) { } else if($id && is_numeric($id)) {
$record = DataObject::get_by_id($this->owner->stat('tree_class'), $id); $record = DataObject::get_by_id($this->owner->stat('tree_class'), $id);
if($record && $record->Locale) $this->owner->Locale = $record->Locale; if($record && $record->Locale) $this->owner->Locale = $record->Locale;
@ -50,16 +45,14 @@ 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
if($req->requestVar("Locale")) { $requestLocale = $req->requestVar("Locale");
$requestLocale = $req->requestVar("Locale");
} else {
$requestLocale = $req->requestVar("locale");
}
$page = $this->owner->currentPage(); $page = $this->owner->currentPage();
if( if(
$requestLocale && $page && $page->hasExtension('Translatable') $req->httpMethod() == 'GET' // leave form submissions alone
&& $requestLocale
&& $page
&& $page->hasExtension('Translatable')
&& $page->Locale != $requestLocale && $page->Locale != $requestLocale
&& $req->latestParam('Action') != 'EditorToolbar' && $req->latestParam('Action') != 'EditorToolbar'
) { ) {
@ -75,7 +68,7 @@ class TranslatableCMSMainExtension extends Extension {
// If the record is not translated, redirect to pages overview // If the record is not translated, redirect to pages overview
return $this->owner->redirect(Controller::join_links( return $this->owner->redirect(Controller::join_links(
singleton('CMSPagesController')->Link(), singleton('CMSPagesController')->Link(),
'?locale=' . $requestLocale '?Locale=' . $requestLocale
)); ));
} }
} }
@ -140,12 +133,12 @@ class TranslatableCMSMainExtension extends Extension {
function updateLink(&$link) { function updateLink(&$link) {
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); $locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale();
if($locale) $link = Controller::join_links($link, '?locale=' . $locale); if($locale) $link = Controller::join_links($link, '?Locale=' . $locale);
} }
function updateLinkWithSearch(&$link) { function updateLinkWithSearch(&$link) {
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); $locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale();
if($locale) $link = Controller::join_links($link, '?locale=' . $locale); if($locale) $link = Controller::join_links($link, '?Locale=' . $locale);
} }
function updateExtraTreeTools(&$html) { function updateExtraTreeTools(&$html) {
@ -155,7 +148,7 @@ class TranslatableCMSMainExtension extends Extension {
function updateLinkPageAdd(&$link) { function updateLinkPageAdd(&$link) {
$locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); $locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale();
if($locale) $link = Controller::join_links($link, '?locale=' . $locale); if($locale) $link = Controller::join_links($link, '?Locale=' . $locale);
} }
/** /**

View File

@ -1119,7 +1119,7 @@ class Translatable extends DataExtension implements PermissionProvider {
'<li><a href="%s">%s</a></li>', '<li><a href="%s">%s</a></li>',
Controller::join_links( Controller::join_links(
$existingTranslation->CMSEditLink(), $existingTranslation->CMSEditLink(),
'?locale=' . $existingTranslation->Locale '?Locale=' . $existingTranslation->Locale
), ),
i18n::get_locale_name($existingTranslation->Locale) i18n::get_locale_name($existingTranslation->Locale)
); );

View File

@ -37,10 +37,10 @@
}, },
onchange: function(e) { onchange: function(e) {
// Get new locale code // Get new locale code
locale = {locale: $(e.target).val()}; var locale = {Locale: $(e.target).val()};
// Check existing url // Check existing url
search = /locale=[^&]*/; search = /Locale=[^&]*/;
url = document.location.href; url = document.location.href;
if(url.match(search)) { if(url.match(search)) {
// Replace locale code // Replace locale code

View File

@ -31,7 +31,7 @@
onchange: function(e) { onchange: function(e) {
// reload tree with selected locale // reload tree with selected locale
var treeDropdown = $(this).parents('form').find('#internal .treedropdown'); var treeDropdown = $(this).parents('form').find('#internal .treedropdown');
treeDropdown.data('urlTree', $.path.addSearchParams(treeDropdown.data('urlTree').replace(/locale=[^&]*/, ''), 'locale='+$(this).val())); treeDropdown.data('urlTree', $.path.addSearchParams(treeDropdown.data('urlTree').replace(/Locale=[^&]*/, ''), 'Locale='+$(this).val()));
treeDropdown.loadTree(); treeDropdown.loadTree();
} }
}); });