Jump to content

Mutilanguage name doubt


manlio
 Share

Recommended Posts

Hello, I have a problem and I'm not sure if it is an intended behaviour or not.

I'm building some url with something like

echo $page->url.$item->name

but  the $item->name remains the same in every language although is translated.

In my mind I was thinking it automatically changes based on language, but this doesn't happen.

Is this a normal behaviour or am I missing something?

Thank you

Link to comment
Share on other sites

The name property is not translated, it is a unique identifier for a page together with the parent_id. You should not build urls manually like this, you should use the following method:

$page->localUrl()

See: https://processwire.com/api/ref/page/

Edit: The name is translated as well, but calling $page->name always returns the (unique) name of the default langauge. Use $page->localName() to get the translated name of a language.

  • Like 3
Link to comment
Share on other sites

Ok Wanze thank you! As I suspected is an intended behaviour.

In this case, I don't need to get the full url because I have to rebuild a "fake" URL.

I'm filtering records based on Urlsegments and names.

I think that I can solve the link problem using something like 

//instead of
echo $page->url.$item->name

// I can use
echo $page->url.sanitizer->pageName($item->title)

but now I have a new problem when I arrive in the destination page where I filter records like this 

$pages->find("template=mytemplate,pageref_category.name=$urlSegment1");
//now this doesn't return anything because it is comparing something like "prima-categoria" with "first-category"

My new question is :-) : How can I properly filter records based on every languages?

Is there any alternative still using Urlsegments?

Thank you for your help.

Link to comment
Share on other sites

Thanks to this post 

I discovered that I can access local names in this way

$item->localName($user->language)

Now I just need to understand if it is possible to do the same thing in a selector to filter the results.

 

EDIT:

I have just found that I'm not alone :-)

 

Link to comment
Share on other sites

10 hours ago, manlio said:

Now I just need to understand if it is possible to do the same thing in a selector to filter the results.

To find a page using a multilanguage name you have to append the ID of the language to 'name' in your selector. Each multilanguage page name is stored separately like this.

So suppose you had a page named 'three' in the default language, and added a French name for the page 'trois'.

In a simplified example, if you wanted to get the page by name in the default language you would do...

$p = $pages->get("name=three");

But if you wanted to get the page by name in French you would do...

$p = $pages->get("name1234=trois");

...where 1234 is the ID of the French language.

So if you are working from the user's language you could do...

$p = $pages->get("name{$user->language->id}=trois");

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Thank you Robin S. I had tried before this approach but I receive this error

Unknown subfield: name11658

When I used in a selector like this

template=mysubcategory,pageref_category.name{$user->language->id}=$urlSegment1 //pageref_category is a multiarray page field type)

So I tried a different not elegant route that uses directly title.

Before putting into the selector I convert the dashes into spaces

$urlTitle1 = str_replace("-", " ", $urlSegment1); //convert dashes into spaces
//the selector becomes something like
template=mysubcategory,pageref_category.title=$urlTitle1

Now everything seems to work although maybe I can have new problems if the title has to remain the same in different languages.

 

Anyway now I ahve a new problem (or maybe I'm tired :-) )

I have a multiarray page fieltype and I'm using alternate multilanguage fields.

categorie.thumb.JPG.9a8c7c1a53be7e7d4e3cbde420bdf60a.JPG

The problem is that when I use this

$selector = "template=prodotto,pageref_categoria.title=$urlTitle1";
$items = $pages->find("$selector");

although the user changes language, it continues to filter data based on pageref_categoria default language....

 

Maybe I need a rest :-)

 

Link to comment
Share on other sites

1 hour ago, manlio said:

 I had tried before this approach but I receive this error


Unknown subfield: name11658

Maybe the multilanguage name is not made available as a subfield. You could do this:

$p = $pages->get("name{$user->language->id}=$urlSegment1"); // you should probably include a parent or template in this selector too
$selector = "template=mysubcategory,pageref_category=$p";

 

As for the other problem, it looks like your screenshot shows the name of those pagefields is different for each language. So you need to search the right one for the language: pageref_categoria or pageref_categoria_en or pageref_categoria_fr.

 

  • Like 1
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...