Skip to content

Commit

Permalink
Merge pull request #193 from chillu/pulls/consistent-locale-casing
Browse files Browse the repository at this point in the history
Consistent locale casing, set locale on POST
  • Loading branch information
Damian Mooyman committed Aug 20, 2015
2 parents 82c3251 + da30096 commit 295e11c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
27 changes: 10 additions & 17 deletions code/controller/TranslatableCMSMainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ function init() {
// as an intermediary rather than the endpoint controller
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,
// or implied on a translated record (see {@link Translatable->updateCMSFields()}).
// $Lang serves as a "context" which can be inspected by Translatable - hence it
// has the same name as the database property on Translatable.
$id = $req->param('ID');
if($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)) {
$record = DataObject::get_by_id($this->owner->stat('tree_class'), $id);
if($record && $record->Locale) $this->owner->Locale = $record->Locale;
Expand All @@ -50,16 +45,14 @@ function init() {
}
Translatable::set_current_locale($this->owner->Locale);

// if a locale is set, it needs to match to the current record
if($req->requestVar("Locale")) {
$requestLocale = $req->requestVar("Locale");
} else {
$requestLocale = $req->requestVar("locale");
}

// If a locale is set, it needs to match to the current record
$requestLocale = $req->requestVar("Locale");
$page = $this->owner->currentPage();
if(
$requestLocale && $page && $page->hasExtension('Translatable')
$req->httpMethod() == 'GET' // leave form submissions alone
&& $requestLocale
&& $page
&& $page->hasExtension('Translatable')
&& $page->Locale != $requestLocale
&& $req->latestParam('Action') != 'EditorToolbar'
) {
Expand All @@ -75,7 +68,7 @@ function init() {
// If the record is not translated, redirect to pages overview
return $this->owner->redirect(Controller::join_links(
singleton('CMSPagesController')->Link(),
'?locale=' . $requestLocale
'?Locale=' . $requestLocale
));
}
}
Expand Down Expand Up @@ -140,12 +133,12 @@ function createtranslation($data, $form) {

function updateLink(&$link) {
$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) {
$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) {
Expand All @@ -155,7 +148,7 @@ function updateExtraTreeTools(&$html) {

function updateLinkPageAdd(&$link) {
$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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion code/model/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ function updateCMSFields(FieldList $fields) {
'<li><a href="%s">%s</a></li>',
Controller::join_links(
$existingTranslation->CMSEditLink(),
'?locale=' . $existingTranslation->Locale
'?Locale=' . $existingTranslation->Locale
),
i18n::get_locale_name($existingTranslation->Locale)
);
Expand Down
4 changes: 2 additions & 2 deletions javascript/CMSMain.Translatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
},
onchange: function(e) {
// Get new locale code
locale = {locale: $(e.target).val()};
var locale = {Locale: $(e.target).val()};

// Check existing url
search = /locale=[^&]*/;
search = /Locale=[^&]*/;
url = document.location.href;
if(url.match(search)) {
// Replace locale code
Expand Down
2 changes: 1 addition & 1 deletion javascript/HtmlEditorField.Translatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
onchange: function(e) {
// reload tree with selected locale
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();
}
});
Expand Down

0 comments on commit 295e11c

Please sign in to comment.