Jump to content
Fuzzy

Define fallback for every language

Recommended Posts

Hi,

Currently we have set up a multilanguage site using multilang URLs and multilang fields. English is used as default/fallback. Now we have to create two spanish language versions (Spain-Spanish + LATAM-Spanish). Is it possible to define for LATAM-Spanish as primary fallback Spain-Spanish. If content is not available in Spain-Spanish publish in default language?

Thanks for pointing me in the right direction...

Share this post


Link to post
Share on other sites

Thanks @7Studio. But the default language is still English then. What I'm looking for is:

Currently in Processwire:

  1. Check if content is available in a specific language
  2. If not, content will be shown in the default language.

What I want:

  1. Check if content is availbale in a specific language
  2. If not, check if language is available in primary fallback language for this specific language
  3. If not, content will be shown in the default language.

Share this post


Link to post
Share on other sites

Right, this could help if your default language will be in one of spanish languages. Sorry, I was a bit to fast with my reply. I guess that in this case you will have to provide a complete translations for both spanish languages even if most of strings would be same/similar. Maybe someone else will suggest solution here 😉

Share this post


Link to post
Share on other sites

Example with 'body' field. Define a language fallback order in ready.php and render first match in template. (not tested ... )
 

// in ready.php
$languageFallback = [1046,1042,1033]; // array of Language-IDs in fallback order

$body = '';
$pageActiveLanguageIds = $page->getLanguages()->each('id');
$languageFallback = array_intersect($languageFallback, $pageActiveLanguageIds);
foreach($languageFallback as $languageID) {
    $body = $page->getLanguageValue($languageID, 'body');
    if ($body) break;
}
$page->body = $body;


// in your template file
echo $page->body; // render field body

 

  • Like 3

Share this post


Link to post
Share on other sites

@kixe, you are the best! But I think, you already knew that!

Is it possible to do this for every field of a page? - The code below isn't working...

if ($user->language->id == "20025") {
	$languageFallback = [15151,1010]; // array of Language-IDs in fallback order for LATAM
} else {
	$languageFallback = [1010]; // array of Language-IDs in fallback order for all other languages
}

$pageActiveLanguageIds = $page->getLanguages()->each('id');
$languageFallback = array_intersect($languageFallback, $pageActiveLanguageIds);

foreach ($page->fields as $field) {
  $field = '';
	foreach($languageFallback as $languageID) {
		$field = $page->getLanguageValue($languageID, $field);
		if ($field) break;
	}
  $page->$field = $field;
}

 

Share this post


Link to post
Share on other sites

Do not overwrite the field object (loop item) with the field value. Try:

foreach ($page->fields as $field) {
  $fieldValue = '';
	foreach($languageFallback as $languageID) {
		$fieldValue = $page->getLanguageValue($languageID, $field);
		if ($fieldValue) break;
	}
  $page->$field = $fieldValue;
}
  • Like 2

Share this post


Link to post
Share on other sites

Thanks, @kixe! The code is working, BUT: all data displayed which are not stored in a field on this page still fallback to the default language (e.g. navigation and footer --> template include; translated strings --> _("xyz") . No surprise, but I did not consider this.

Is there a way to overwrite the language fallback behaviour of Processwire globally? - I searched the forum, but could not find any information about that...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...