Fuzzy Posted August 11, 2020 Share Posted August 11, 2020 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... Link to comment Share on other sites More sharing options...
7Studio Posted August 11, 2020 Share Posted August 11, 2020 Hi Fuzzy, maybe this could help: https://processwire.com/blog/posts/pw-3.0.151/#language-translation-function-improvements Link to comment Share on other sites More sharing options...
Fuzzy Posted August 11, 2020 Author Share Posted August 11, 2020 Thanks @7Studio. But the default language is still English then. What I'm looking for is: Currently in Processwire: Check if content is available in a specific language If not, content will be shown in the default language. What I want: Check if content is availbale in a specific language If not, check if language is available in primary fallback language for this specific language If not, content will be shown in the default language. Link to comment Share on other sites More sharing options...
7Studio Posted August 11, 2020 Share Posted August 11, 2020 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 ? Link to comment Share on other sites More sharing options...
kixe Posted August 11, 2020 Share Posted August 11, 2020 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 3 Link to comment Share on other sites More sharing options...
Fuzzy Posted August 12, 2020 Author Share Posted August 12, 2020 @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; } Link to comment Share on other sites More sharing options...
kixe Posted August 15, 2020 Share Posted August 15, 2020 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; } 2 Link to comment Share on other sites More sharing options...
Fuzzy Posted August 17, 2020 Author Share Posted August 17, 2020 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... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now