Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

translatable : fallback on specific language if not translated


Go to End


4 Posts   1743 Views

Avatar
pac

Community Member, 25 Posts

29 November 2011 at 2:03am

HI

in a bilingual site, I have a few pages that will be changed all the time and will never be translated.

is it possible to display the original content if page is not translated ?
or, to put it in different words, how can you access english content from a [translatable] french page ?

same question for a data object...

thanks for your help

Avatar
martimiz

Forum Moderator, 1391 Posts

29 November 2011 at 11:52pm

I don't think that is possible out of the box... Pages for which no translation exists, will not be displayed on the translated site, so you always need to create a translation.

I'm thinking maybe it would be possible to create some functionality in the page controller that would check if the current language is the master language, and if not, get the values for some specific field from the masterpage. You might use a boolean to enable/disable this behaviour on a per-page basis...

This way things like headers, footers and other stuff would still be French, while specific fields (like Content) would be English? The same would work for DataObjects...

Martine

Avatar
pac

Community Member, 25 Posts

30 November 2011 at 10:28pm

yes exactly what I need.
not sure how to make that controller though.
pretty much

if
Content_locale empty
then
content_en

same for specific fields and dataObjects

will try but could use some help

Avatar
martimiz

Forum Moderator, 1391 Posts

1 December 2011 at 1:42am

I just put something together, that might start as a beginning. What this does is create a custom getter for Content in the Page_Controller that in turn calls some general function useMaster($fieldName). This function does the following:

- check if the field is empty
- if so, check if we're using translatable
- if so, check if the current page is in fact a translation
- if all true, get the master page and return its contents

This is all very scetchy, it might lead to something better, probably need a decorator, I don't know... Curious what you make of it...

class Page_Controller extends ContentController {

	...

	public function Content() {
		return $this->UseMaster('Content');
	}


	function useMaster($fieldName) {
		if (empty($this->$fieldName)) {
			if (Object::has_extension($this->ClassName, 'Translatable')) {
            			$GroupID = $this->getTranslationGroup();
            			if ($GroupID && $this->ID != $GroupID) {
					$Master = DataObject::get_by_id('SiteTree', $GroupID);
					if (!empty($Master->ID) && $Master->CanView()) {
						return $Master->$fieldName;
					}
				}
			}
		}
		return $this->$fieldName;
	}

Martine