Jump to content

How to get page title of other languages ?


adrianmak
 Share

Recommended Posts

I have added two more other languages , that's there are total of three languages, default, jpn, kor (default is the English)

And I changed the existing title field type to PageTitleLanguage

Now, I want a page title to display all three languages from template file

The $page->title only output the default language i.e English in my case

How to display other two languages in template file ?

I follow the link below, to how to get a multi-language field value

http://processwire.com/api/multi-language-support/multi-language-fields/
 

This is my segment of code

$categories = $pages->get(1060)->children();

foreach($categories as $category) {
    $page->of(false);
    $eng = $languages->get('eng');
    $content .=  $category->title->getLanguageValue($eng);
}

With this code executed, I got an error

Error: Call to a member function getLanguageValue() on a non-object (line 14 of /var/www/html/site/templates/case-studies-index.php)

Line 14 is this line of code

$content .= $category->title->getLanguageValue($eng);

Link to comment
Share on other sites

I am getting the same error with multi language fields when I use getLanguageValue method.

When I use the other approach it is working. Try this

$categories = $pages->get(1060)->children();

$language = $user->language; // save the user's language
foreach($categories as $category) {
    $user->language = $languages->get('default'); // you need to use default because English is your default language and set the $user language to it
    $content .=  $category->title;
}
$user->language = $language; // restore the user's language

If you want to show page titles in all languages, just loop through your languages inside the foreach:

foreach($categories as $category) {
    foreach ($languages as $lang) {
        $user->language = $lang;
        $content .=  $category->title;
    }
}

  • Like 2
Link to comment
Share on other sites

@adrianmak, is it just a copy&paste mistake that your code accesses $category while you set outputFormatting to false on $page?

@gebeer: Additionally, there are two other methods to get a page field's value in another language (with the first one being my favorite):

// 1. Directly from the page:
$title_en = $category->getLanguageValue($eng, 'title');

// 2. A bit more circumspect:
$titleField = $category->getUnformatted('title');
$title_en = $titleField->getLanguageValue($eng);

  • Like 6
Link to comment
Share on other sites

  • 3 years later...

If you need to output all the languages, but only want to show the title in native spoken, then I came up with this (maybe there is a better way?):
 

foreach($languages as $language){
	if($user->language->id != $language->id) echo '<a href="'.$page->localUrl($language).'" class="languagebtn">'.$language->getLanguageValue($language->name, 'title').'</a>';
}

 

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