Jump to content

Translate Select Options Fieldtype when getting all available options


Recommended Posts

Posted

I have a select options field, which needs to be translated.

The translation works fine as long as you echo the available options page-specific.

Now, according to the documentation, when you need to display all possible options (independently of any page), you would do something like this:

<?php
  // Get all category options
  $field = $fields->get('project_category');
  $all_options = $field->type->getOptions($field);

  foreach($all_options as $option) {
      echo "<option value='.{$option->title}'>{$option->title}</option>"."\n";
  } ?>

 

The problem is that this does not pass the translated language values for each option.

It will display always in the default language.

 

Any idea how to get the translations in this situation?

 

Posted
  foreach($all_options as $option) {
      $title = $language->isDefault() ? $option->title : $option->get("title$language");
      echo "<option value='$title'>$title</option>"."\n";
  } ?>

 

Posted

Did you try it?

The translated contents of an option are in its properties title$languageid and value$languageid.

Posted

I had to change the default language check to get it working:

 

foreach($all_options as $option) {

       $title = $user->language->name == 'default' ? $option->title : $option->get("title$language");
       echo "<li><a href='#' class='filter-category' data-filter='.$title'>$title</a></li>"."\n";

} ?>

 

Don't know why $language->isDefault() always fails...

  • 4 years later...
Posted

Hi everybody.

I refresh this old thread because I had also the same issue.

For the solution I have found (don't remember where*):

echo $option->getTitle();

For me it works fine, hope to be helpful.

*found here, thanks @schwarzdesign :

 

 

  • Like 2

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...