elabx Posted August 22, 2015 Share Posted August 22, 2015 I built a menu using Pages to let the translation handle itself through PW multi language page title fields. Though, while trying some click menu and scroll to element with class gimmicky, I decided to try to place a data attribute inside the clicked link so that I can later use it for the javascript class selection. $navigationPages = $pages->get("/navigation/")->children(); $spanish = $user->language->id; foreach ($navigationPages as $navigationItem){ echo "<li><a data-scroll='{$navigationItem->name}'>{$navigationItem->title} </a></li>"; } So while trying this, as you can see I used the name property to echo the data attribute value, I stupidly kept trying to echo the name in the non default language (spanish), until I decided to not break my brain and just use the one given by default (english). Could I have actually output the name in the non default language? For example I tried: $navigationItem->name->getLanguageValue($spanish) But didn't work, and I'm just wondering why. And I am also wondering, is this and this the only documentation available for using the language support through the API? I was just a bit surprised I didn't quite find any reference of this on the cheatsheet, is this because $languages or $user->language have to do with the language modules? Should I start reading source code to find out about this? 1 Link to comment Share on other sites More sharing options...
Soma Posted August 22, 2015 Share Posted August 22, 2015 Turn off output formatting first. As name is a string and not an object when output formatting is on. Or try $page->getUnformatted(fieldname)->getLanguageValue(lang) Or just this as it's a page native field.. $page->get("name$langId") Link to comment Share on other sites More sharing options...
elabx Posted August 23, 2015 Author Share Posted August 23, 2015 Turn off output formatting first. As name is a string and not an object when output formatting is on. Or try $page->getUnformatted(fieldname)->getLanguageValue(lang) Or just this as it's a page native field.. $page->get("name$langId") This worked nice! $page->get("name$langId") Though if I set it like this, I have kind of save first the ID of the language I will use to output the data. It calls to my attention that when set on the default language, it seems that "name$langID" doesn't do anything. I will try the other lines of code later more carefully, 'cause I kind of quickly tested your suggestions. Thanks a lot for your answer! Link to comment Share on other sites More sharing options...
Adam Kiss Posted August 23, 2015 Share Posted August 23, 2015 It's because default language does have an id, but the field for the default language doesn't use it. So you have: - lang: default, id: 1017, field: name - lang: other, id: 1019, field: name1019 - lang: third, id: 1020, field: name1020 So, if you just tried get("name$langId") for default language, you'd try to access "name1017", which doesn't exist. 1 Link to comment Share on other sites More sharing options...
bfncs Posted January 6, 2016 Share Posted January 6, 2016 This thread is already quite old, but I just stumbled upon the same problem and wanted to add my solution to this thread. When you install the LanguageSupportPageNames module, you also get the Page::localName($language) method to easily retrieve the page name in different languages. This has been added in 2.3.0. So, to get the page name in a the language (or any other), you could do something around these lines and be done: // Get default language $defaultLanguage = $languages->getDefault(); // Get default page name $defaultName = $page->localName($defaultLanguage); 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now