vmo Posted May 6, 2016 Posted May 6, 2016 Hi, I have a template "Project" and in this template accepts url segments. The site is a multi-language site, it as PT and EN languages. The possible segments are configured in a Class, at the "_func.php", that has a method that returns all the segments info already translated width "_x('string','context')", and with that info a Project menu is created. The translation was made in the admin translating the "_func.php" for EN. For example _x("apresentacoes",'Segments') when in PT returns "apresentacoes" and _x("apresentacoes",'Segments') when in EN returns "presentations". The site has a language switcher and I want to concatenate the current segment to each language switcher when creating the language switcher: PT => projecto/nome-do-projeto/apresentacoes EN => project/name-of-project/presentations And I don't know how to get the translation from a current segment like "apresentacoes" when in PT to add it to the EN switcher or the translation for "presentations" when in EN to add it to PT switcher. How can I get a translation from string that isn't associated to a page or other PW object? Thank you very much
tpr Posted May 6, 2016 Posted May 6, 2016 I think it should just work fine if you append the segment using _x() to the langswitcher url. What is the code you use for the langswitcher now?
vmo Posted May 6, 2016 Author Posted May 6, 2016 Hi, this is the code I am using, is the standard code of the Languages Profile. <?php foreach ( $languages as $language ) { $_cst = ''; if (! $page->viewable ( $language )) continue; // is page viewable in this language? if ($language->id == $user->language->id) { echo '<li class="current">'; $_cst = $current_segment; // [1] Segment in the current language } else { echo '<li>'; $_cst = _x($current_segment,'Segment'); // [2] Translated segment } $url = $page->localUrl ( $language ); $_url = $url; if($current_segment) $_url = $_url.$_cst; $hreflang = $homepage->getLanguageValue ( $language, 'name' ); echo '<a class="link-language" hreflang="'.$hreflang.'" href="'.$_url.'">'.$language->title.'</a>'; echo "</li>"; } ?> The question is when in PT I need to translate the segment "apresentacoes" to EN and only using the _x() it not return the translationlue va, I think I need to load in some way the EN language and get the "apresentacoes" translation value. Thank you.
tpr Posted May 6, 2016 Posted May 6, 2016 I see. What if you set $user->language = 'en' just before getting the translation, and set it back to original after that?
tpr Posted May 6, 2016 Posted May 6, 2016 This actually works pretty well here. I was sceptic because the if you are on a non-default language then the string you pass to the translate function will be different from the one you originally used. So if the string is "Search", and you browse the site in German, then it wil look like this: _x('Suchen', 'Segments') And in this case I thought it won't find the English counterpart because I've never set 'Suchen', only 'Search'. But it works! Just tried it on a 4-language site and it works in all languages, back and forth. Magic. <?php // save current language to variable to restore later $originalLanguage = $user->language; foreach ( $languages as $language ) { $_cst = ''; if (! $page->viewable ( $language )) continue; // is page viewable in this language? if ($language->id == $user->language->id) { echo '<li class="current">'; $_cst = $current_segment; // [1] Segment in the current language } else { echo '<li>'; // need to change user language temporary to find translation $user->language = $language; $_cst = _x($current_segment,'Segment'); // [2] Translated segment // restore user language $user->language = $orginalLanguage; } $url = $page->localUrl ( $language ); $_url = $url; if($current_segment) $_url = $_url.$_cst; $hreflang = $homepage->getLanguageValue ( $language, 'name' ); echo '<a class="link-language" hreflang="'.$hreflang.'" href="'.$_url.'">'.$language->title.'</a>'; echo "</li>"; } ?> The question is that are all your segments translated? If not, it won't find anything. Another issue is that do you need to check the availability of the segment in the other language before appending to the href? If yes, you'll need to use a query first to see if the given segment exists.
vmo Posted May 9, 2016 Author Posted May 9, 2016 Hi tpr, there are only two segments translated the other two possible segments are the same in PT and EN. What is happening is if there is no translation it returns the same string. I implemented the code and I don't get the translation when changing the current user language it returns the same string, it changes the current language but does not find the translation. The translation occurs in the _main.php and the translated string are translated in the _func.php maybe this is a issue, maybe when using the translation function _x() or __() it only evaluates the translation strings associate with that file and not all the translated strings in all translated files. I found this post https://processwire.com/talk/topic/10447-using-translatable-strings-across-template-files/ about this. Thank you
tpr Posted May 9, 2016 Posted May 9, 2016 I'm using this method for translation: https://processwire.com/talk/topic/10447-using-translatable-strings-across-template-files/ In my setup there is only 1 file '_strings.php' so PW will find translations even if it's in another file. You don't even need to include() that file, only select it in the Language Translator (admin). 1
vmo Posted May 9, 2016 Author Posted May 9, 2016 Hi, I created the file _strings.php and translated it and noting happened. I used the _x() and tried the __() but with the same result, I think I am doing something wrong, I need todo more tests. Thank you
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