Jump to content

Language switch and translated language title


maba
 Share

Recommended Posts

Hi,

with this

foreach($languages as $language) {
  if(!$page->viewable($language)) continue; // check if page not published for this language
  $url = wire('page')->localUrl($language);
  echo "<li$class><a href='$url'>$language->title</a></li>";
}

can we have switch menu not "fully" translated?

Not

Italian
English
French 

but

Italiano
English
Français 

for all languages..

 

Thanks,

Marco


 

Sorry for the formatting, but seems forum has some problems with Safari and preview..

  • Like 1
Link to comment
Share on other sites

Do you suggest to translate:

IT
Title Italiano->Italiano
Title English->Italiano
Title French->Italiano

and not

IT
Title Italiano->Italiano
Title English->Italian
Title French->Italien

?

Ok for menu but in this way language are "never" translated.. Maybe..

Thanks Soma :)

Link to comment
Share on other sites

@moreweb

If you need another translation just for the switcher, add a new text field to the language template e.g. 'titleSwitcher':

foreach($languages as $language) {
  if(!$page->viewable($language)) continue; // check if page not published for this language
  $url = wire('page')->localUrl($language);
  echo "<li$class><a href='$url'>$language->titleSwitcher</a></li>";
}
Link to comment
Share on other sites

@wanze

Not needed in my opinion.. 

I need something like:

foreach($languages as $language) {
  if(!$page->viewable($language)) continue; // check if page not published for this language
  $url = wire('page')->localUrl($language);
  $title = wire('page')->localTitle($language);
  echo "<li$class><a href='$url'>$title</a></li>";
}
Link to comment
Share on other sites

Well, the method localTitle doesn't exist.

So you have at least 2 easy solutions:

1) Like I said: Add the textfield to language template and insert the title you want to appear in every language.

2) Code something, e.g.

// Keys are IDs of languages... you could also use name
$localTitles = array(
  1234 => 'Italiano',
  1235 => 'English',
  3147 => 'Francais',
);

foreach($languages as $language) {
  if(!$page->viewable($language)) continue; // check if page not published for this language
  $url = wire('page')->localUrl($language);
  $title = $localTitles[$language->id];
  echo "<li$class><a href='$url'>$title</a></li>";
}
Link to comment
Share on other sites

No need to add another field, you already have PageTItleLanguage on the language template (see my screenshot). If not change the "title" field to language text field. 

There you can translate the language titles, you don't need to change anything in your code or setup.



Yes, this is my previous configuration. But when you see any EN page you see "German" in switch menu..

Ok, but that what you want!? Translated language titles, so in english it will show all language titles in english.

Link to comment
Share on other sites

In frontend, I want this menu for each language.. always the same:

Languages:
- Italiano
- English
- Deutsch

But i don't want edit language page in this way

post-846-0-29147800-1369983647_thumb.png

(probably I needed the translation in other section of the site)

so I want maintain this

post-846-0-05441200-1369983804_thumb.png

but in the menu I want se the language title localized.

Last wanze's code do the job but why use the array when we just have all data?

Link to comment
Share on other sites

@Soma

As far as I understand, he has this setup already but wants to output always the same titles in every language.

Meaning: Leaving the titles translated BUT not use the translated title in the language switcher ;-)

Try this:

foreach($languages as $language) {
  if(!$page->viewable($language)) continue; // check if page not published for this language
  $url = wire('page')->localUrl($language);
  $title = $language->title->getLanguageValue($language);
  echo "<li$class><a href='$url'>$title</a></li>";
}
  • Like 2
Link to comment
Share on other sites

@Soma

As far as I understand, he has this setup already but wants to output always the same titles in every language.

Meaning: Leaving the titles translated BUT not use the translated title in the language switcher ;-)

Try this:

foreach($languages as $language) {
  if(!$page->viewable($language)) continue; // check if page not published for this language
  $url = wire('page')->localUrl($language);
  $title = $language->title->getLanguageValue($language);
  echo "<li$class><a href='$url'>$title</a></li>";
}

@Wanze

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

@Soma

Required by customer.

Link to comment
Share on other sites

  • 3 years later...

I know this topic is old, but I came across the exact same issue in my first ML site. I am surprised that what @maba was looking to do is not more common or talked about. It seems like the only logical option in my opinion.

Anyway, soma's solution did the trick - it's all about that getUnformatted method so that getLanguageValue has an object to work with. I did want to clarify though that it is in fact getLanguageValue rather than getLanguagevalue - note the case of the "V". It doesn't prevent it from working because methods in PHP are not case sensitive. Also can of course just use the $language object instead of the id.

$language->getUnformatted('title')->getLanguageValue($language);

It does seem a little unnecessarily complicated though.

  • Thanks 1
Link to comment
Share on other sites

Thanks @BitPoet - that is much cleaner.

Another question along these lines. Is it OK to re-title the "default" language as "English" ? I assume I should keep the name as "default", but I feel like it would be nice to title it properly and even localize it when the user is viewing the site in Portuguese. Is that something you guys do, or should I stick with doing this for the frontend language switcher:

$langtitle = $language->name == 'default' ? 'English' : $language->getLanguageValue($language, "title");

 

Link to comment
Share on other sites

Thanks guys!

So in case anyone else new to all this ML stuff is interested, this is what I ended up with which shows the title of the current user's language localized to their language and all of the other available languages with the title localized to their language.

For displaying the currently selected language:

$user->language->getLanguageValue($user->language, "title");

Then for showing the other languages in the switcher:

foreach($languages as $language) {
	if(!$page->viewable($language)) continue; // is page viewable in this language?
	if($language !== $user->language) echo '<li><a hreflang="'.$language->iso.'" href="'.$page->localUrl($language).'">'.$language->getLanguageValue($language, "title").'</a></li>';
}

I obviously left out the <ul> container and the <li> class details in this example, but you get the idea.

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

  • Recently Browsing   0 members

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