Jump to content

field label language problem


valan
 Share

Recommended Posts

I've spent few hours at this problem, but still can't solve it -

// this code works not as expected - outputs only in default language, even if $user->language is different.
echo $user->fields->get("some_text_field")->label;

// while this code in the same php file works as expected - output follows $user->language
echo $page->any_field; 

Note: some_text_field field label is a multi-language field, all translations available.

If I try to do something like this:

echo $user->fields->get("some_text_field")->label->getLanguageValue($user->language);

it returns:

Error: Call to a member function getLanguageValue() on a non-object

Please, help - I want to output field label in current language! )) Thanks!

Link to comment
Share on other sites

Meanwhile, discovered why

echo $user->fields->get("some_text_field")->label->getLanguageValue($user->language)  

doesn't work. It is because $user->fields->get("some_text_field")->label returns string. Yes, I'm newbie )))

Still trying to find access to another translation in my field's label field... w/o success after another portion of experiments... 

Link to comment
Share on other sites

Hi valan,

I've found a solution. The translations of the labels (not default) are stored in the 'data' field in the 'fields' table, as JSON.

Example: {"label1012":"Weight"} where 1012 is the ID of the language.

You can try this code:

$label = 'label';
if ($user->language->name != 'default') {
  $label = "label{$user->language}";
}

// Output label in correct language
echo $user->fields->get('field')->$label;
  • Like 5
Link to comment
Share on other sites

WOW!!! It works!

Wanze, thank you for help! - I've started to think that problem doesn't have elegant solutions in PW. But you proved that PW has such solution! 

P.S. hope to see this kind of hints documented sometime by core PW team!

  • Like 2
Link to comment
Share on other sites

$label = 'label';
if ($user->language->title != 'no') {
$label = "label{$user->language}";
}

echo $page->fields->get('phone')->$label;

The above code works great on a regular php page, but I can´t get it to work inside a function - yes, yes - newbie :-)

function renderAuthorData($page) {

	$na = "<span class='na'>n/a</span>";
	$download = $page->files->first()->name;
	
	//Check if author is an employee
	$vista = $page->vista;
	if($vista == 1){
	
	$label = 'label';
	if ($user->language->title != 'no') {
	$label = "label{$user->language}";
	}
	
	$out =	"\n<table class='publication_data'>" .
	"\n\t<tbody>";
		
		//Check if author has a specific function
		if($page->function){
		$out .= "\n\t<tr><th>". ($page->fields->get('function')->$label) ."</th><td>". ($page->function ? $page->function : $na) . "</td></tr>";
		}
		
		$out .= "\n\t<tr><th>". ($page->fields->get('author_title')->$label) ."</th><td>" . ($page->author_title ? : $na) . "</td></tr>" .
				"\n\t<tr><th>". ($page->fields->get('phone')->$label) ."</th><td>" . ($page->phone ? : $na) . "</td></tr>" .
				"\n\t<tr><th>". ($page->fields->get('author_email')->$label) ."</th><td>" . ($page->author_email ? : $na) . "</td></tr>" .
				"\n\t<tr><th>". ($page->fields->get('address')->$label) ."</th><td>" . ($page->address ? : $na) . "</td></tr>" .
				"\n\t<tr><th>CV</th><td><a href='{$page->files->first()->url}'>" . $download . "</a> ({$page->files->first()->filesizeStr})</td></tr>" .
				"\n\t</tbody>" .
				"\n</table>";
		
	}else if($vista == 0){
	
	$out =	"\n\t<div style='clear:both;'><p>{$page->title} er ikke ansatt i Vista Analyse AS, men er medforfatter på følgende publikasjon(er):</p></div>";
	
	}	
	
	return $out;

	}

It would be great to check for $user->language from inside a function. Can anyone help me with this? Probably very easy for someone with more PHP knowledge than me....

(The slightly modified code is from Ryan´s Skyscraper Profile - I have spent a few hours "dissecting" that profile to make a site for client - will share next week in the forum)

Link to comment
Share on other sites

Hi Peter,

That's because all the API variables like $users, $page, $pages... are not known to the function. PHP - scope problem.

The solution is to use the global wire() function from Pw to get the API variables:

$vista = wire('page')->vista;
// ...
if (wire('user')->language->title != 'no')) {
// ...
  • Like 3
Link to comment
Share on other sites

Thanks Wanze,

I got the if statement to work, but not the rest...

$label = 'label';
if (wire('user')->language->title != 'no'){ //Works
$label = "label{wire('user')->language}"; //Doesn't work
}

Probably just a matter of correcting my syntax :-)

-Peter

Link to comment
Share on other sites

  • 2 months later...

I have problems rendering the correct field label for anything other than the default langauge.  (this in the template / frontend, not admin)

I'm using PW 2.3.2 dev version, and the values all output fine. It's just that the field labels only show the default language label text, nothing else.

Is there a special syntax I have to use? Or do I still have to manually check for $user->language somehow to get this done? I was under the impression it wouldn't be necessary anymore with 2.3.2. With 4 languages and approx. 50 custom fields, I'd really like to streamline things a bit (all field labels are correctly setup in each language). Or is that something that will be ready only with 2.4?

Link to comment
Share on other sites

I have a hard time to understand why one need the field labels (in different languages) in the front-end. After all if the it should render the language the user currently is viewing.

We covered this already in another thread and there's the solution above and I don't think there's something like you mention on the plan but maybe I miss something. But it would be easy to build a function or module to help out but there's barely anything needed to get a translated label.

$body = $fields->get("body");
$lang = $languages->get("de");
echo $body->get("label$lang"); // german label
Link to comment
Share on other sites

Thanks. I'll try that tomorrow morning.

The reason I need it in the frontend is simple: The various fields generate product specifications in the frontend, as lists or tables. Having to hardcode the field-labels for each addtl. language in the template, when it is already defined in the backend, seems unneccessary, tedious duplicate work. Most values are numeric, but the titles (labels) are always strings. I don't think it's that much of a rare / exotic situation.

After all, an english speaking visitor wants to see "weight", not "Gewicht". A French guy expects "poids", etc.

I have a hard time to understand why one need the field labels (in different languages) in the front-end. 

  • Like 3
Link to comment
Share on other sites

I understand and know this, but why do you need to get the translation when it is just already there. It will output in the language the user views the page ($user->language)

This works just fine in the language the user view the page.

echo $fields->get("body")->label;

Edit: ah, hm it does not? :)

Ok it doesn't get translated as with page fields. Since it's meant to be used for backend context there's no language value for fields settings.

So you either use a $lang variable like this:

$lang = $user->language->isDefault() ? "" : $user->language->id; // Default needs no id
echo $fields->get("body")->get("label$lang");

Or a little hook to add method to the $fields template API var. :)

// from template or a autoload module
$fields->addHook("getLabel", null, "getLabelLang");
function getLabelLang($event){
    $field = $event->arguments(0);
    $lang = wire("user")->language->isDefault() ? "" : wire("user")->language->id;
    $event->return = wire("fields")->get($field)->get("label$lang");
}

// now we use it and it will recognize user lang
echo $fields->getLabel("body");
 
  • Like 10
Link to comment
Share on other sites

Hooray! Thank you. I'm using your hook now in an include, and it works wonderfully.

Guess I'll have to get acquainted with hooks sooner or later...

Do you think this is worth suggesting in the Wishlist + Roadmap forum?

Link to comment
Share on other sites

I have a hard time to understand why one need the field labels (in different languages) in the front-end. After all if the it should render the language the user currently is viewing.

For me, it is because I have a hidden "about" section in my page. I have an about_title and about_description field, but to get to open this hidden "about" section, I have a button labeled "about"/"(about in chinese)". I don't want to have to create whole 'nother field just for "about_button". This is why I wanted to just use the label name for my field, which I already have translated.  I searched and found this thread extremely helpful ^^

I am so glad that this thread popped up just as I encountered the same problem. You guys are awesome  :lol: 

Link to comment
Share on other sites

  • 2 years later...

Dear all,

I know this post is old, but in case someone else stumbles across it, here is a modified version of Soma's getLabel() hook which takes an optional second parameter to decide if you want a fallback to the default language if no localized name of that field is available:

$fields->addHook("getLabel", null, "getLabelLang");
function getLabelLang($event){
    $field = $event->arguments(0);
    $fallback = $event->arguments(1) === null ? true : $event->arguments(1);
    $lang = wire("user")->language->isDefault() ? "" : wire("user")->language->id;
    $field = wire("fields")->get($field);
    $caption = $field->get("label$lang");
    $caption = (count($caption) === 0 && $fallback) ?  $field->get("label") : $caption;
    $event->return = $caption;
}

Usage:

$fields->getLabel("labelName");

-> returns the localized label name with fallback to default language.

$fields->getLabel("labelName", false);

-> returns the label name without fallback to default language.

I am not very good with php, any code improvements welcome!  :)

  • Like 3
Link to comment
Share on other sites

  • 9 months later...
  • 2 months later...
On 16. November 2015 at 5:36 AM, lenno said:

I know this post is old, but in case someone else stumbles across it, here is a modified version of Soma's getLabel() hook which takes an optional second parameter to decide if you want a fallback to the default language if no localized name of that field is available:

Thanks a lot! ??

Link to comment
Share on other sites

  • 7 years 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

×
×
  • Create New...