Jump to content

[SOLVED] How to use other than "default" language when field is empty


DV-JF
 Share

Recommended Posts

Hey all, I'm setting up a site with 3 languages:

  1. default = German
  2. en = English
  3. zh = Chinese simplified

Is it possible to set all Chinese fields which are not translated yet to English instead of the default language German? In the settings of the fields I can only choose the default one?

I was trying to edit / mess around the file wire/modules/LanguageSupport/LanguagesPageFieldValue.php but without any luck.

Is there maybe any hook I could use to accomplish my purpose?

Many greets, Jens alias DV-JF

Edited by DV-JF
Solved - Thanks to Zeka!
Link to comment
Share on other sites

Hi.

Not tested but should work

$this->wire()->addHookAfter('LanguagesPageFieldValue::getStringValue', function ($event) {
	$value = $event->return;
	$languagesPageFieldValue = $event->object;
	$languages = $this->wire()->languages;
	$userLanguageID = $this->wire()->user->language->id;
	$chineseLanguageID = $languages->get('chinese')->id;
	$newFallbackLanguageID = $languages->get('english')->id;

	if($userLanguageID === $chineseLanguageID && !$languagesPageFieldValue->getLanguageValue($chineseLanguageID)) {
		$value = $languagesPageFieldValue->getLanguageValue($newFallbackLanguageID);

		if(!strlen($value)) {
			$value = $languagesPageFieldValue->getDefaultValue();
		}
	};
	
	$event->return = $value;
});

 

  • Like 3
  • Thanks 4
Link to comment
Share on other sites

  • DV-JF changed the title to [SOLVED] How to use other than "default" language when field is empty
  • 8 months later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...