Jump to content

Outputting content in different languages


Recommended Posts

Hi there, 

I feel like I have another rather stupid question, but I am stucked in a problem. 

I wrote a small module, which makes use of the multi-language option.

The inputfield code looks like this:

$donationText = $this->modules->InputfieldCKEditor;
$donationText->useLanguages = true;
$donationText->name = 'donationtext';
$donationText->label = 'Donation CTA Text';
$donationText->value = $this->donationtext;
$donationText->required = true;
$wrapper->add($donationText);

My simplified render method in the module looks like this:

public function renderDonationCTA(){
    echo $this->donationtext;
}

However, when I call this method in my template code only the default language gets outputted.

What did I miss or what can I do, in order to output the inserted text in the "right" language? 

Link to comment
Share on other sites

Hi @Zeka,

thank you for this link, didn't find that one. 

However, that did not worked out well for me. After applying the concept of Adrian, I always got the not default language outputted. 

I have a hardcoded version now. It's not good practice, but it does its job. 

Link to comment
Share on other sites

The value of the Inputfield is set from an user trough the module config page ?

If yes (I dont know how your module work) , then imagine the following, in the getDefaultData() you have :

protected static function getDefaultData() {
	return array(
		'ml_myfield_data' => ''
	);
}

 

In your getModuleConfigInputfields() :

public static function getModuleConfigInputfields(array $data) {
	$data = array_merge(self::getDefaultData(), $data);
	// [...]
    	$f = $modules->get("InputfieldCKEditor");
    	$f->attr('name', 'ml_myfield_data');
    	$f->value = $data['ml_myfield_data'];
    	$f->useLanguages = true;
	// [...]
}

 

Then in module ready() (for testing purpose) you can return the right language :

if($this->wire('languages')) {
	$userLanguage = $this->wire('user')->language;
	$lang = $userLanguage->isDefault() ? '' : "__$userLanguage->id";
}
else {
	$lang = '';
}
bd($this->data['ml_myfield_data'.$lang]); // debug with tracy

After you change your language in your profile, it should return the right language.

  • Like 2
Link to comment
Share on other sites

Hi @flydev

thank you for your answer. It helped me finding my mistake (although I am not understanding what my mistake actually is). 


 

//this does not work (no output)
$field = 'donationtext'.$lang;
$test = $this->field;

//this does work however
$testArr = $this->data['donationtext'.$lang]; 

 

Link to comment
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
 Share

×
×
  • Create New...