Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Switching the pages per language?


Reply


8 Posts   3676 Views

Avatar
ChuckGyver

Community Member, 5 Posts

26 June 2009 at 3:17am

Edited: 26/06/2009 3:18am

Hello,

2.3.2 brought back multilingualcontent. I have created some testpages in different languages. Im glad that many functions which i wanted, r running, but i still have one major problem.

My Question: Is it Somehow possible to switch between the Pages per language.
for Example: if im navigating on a page called mysite.url/sommer and if i click on a link(like the british national flag,img, in the template), and then get linked to the correct english page for sommer, in this case then mysite.url/sommer-$number.

actually i have in the page.ss template an include file called languagebar

<a href="$get_homepage_urlsegment_by_locale(de_DE)"> <img ... /></a>

i need the same, but for every page, not only for homepage.

....i have tried getTranslation,get_one_by_locale etc., which are causing problems, like "Object->__call(): the method 'fortemplate' does not exist on 'HeadnavigationPage'" , though this pagetype is inheriting from page.php....

what would be the right function therefor?

Avatar
ChuckGyver

Community Member, 5 Posts

26 June 2009 at 10:05pm

Edited: 26/06/2009 11:45pm

now i solved it basically, with

<% control Translations %>,<% if Locale == "locale %> and <% if hasTranslation(locale) %>

my original planning about the "languagebar" drifts a little bit from the result

Step1. "Getting the corect $Link per locale". 'Cause i havent found a function for me, which fits, i made it by using

<% control Translations %>

with that it gets the "correct" $link for each locale unsorted,. To get it "sorted", i made

<% if Locale == "en_GB" %>
<a href="$Link">
<img src... /></a>


and then further the other languages, so it is possible to fix the order of the locales,

<% end_control %>

But, what is, when there is no Locale Link for this page?
Step2. Pages, without a translation for a locale, it wouldn appear

to check, if a page, has a tranlation for that locale, for example

<% if hasTranslation(de_DE) %>

, if not, link with the "startpage" for this locale

<a href="$get_homepage_urlsegment_by_locale(de_DE)">
<img src"..."/></a>

Now it shows, first the links for the pages, with a translation for its locale, and then the links for the pages, without translation for its locale, at which is a link for the "startpage" for this locale.

But, the actually locale is not shown now in the bar.
Step3. displaying of the actual locale
to get this, i inserted before <% control Translations %> has begun, another

<% if Locale == "locale" %>
<a href="$Link">
<img src="...."/></a>

and at least, there is still a little problem, with hastranslation --> actual locale would be postet twice, therefor i made

<% if Locale != "en_GB" %>
<% if hasTranslation(en_GB) %>
<% else %>
<a href="......</a>

Now, in the language bar, the first locale, which is displayed, is the actual locale. Followed by the Pages, with a Translation for this page, and at least, the pages, without a Translation for this page, and this through the if requests sorted.
As posted it drifts a little bit from my planning. 'Cause i wanted the order of the locales completly fixed (for example, first always locale en_GB then de_DE and so on)

For Sure not the best solution, but a solution,

has somebody an idea for a better solution?

edit(forgotten): in page.php page_Controller

if($this->dataRecord->hasExtension('Translatable')) {
         i18n::set_locale($this->dataRecord->Locale);
            }


with that, every page "knows" its locale

Avatar
kudesign

Community Member, 64 Posts

6 August 2009 at 9:43pm

Hi thank you very much for your tip! Sorry I am not much of a code reader, just trying your tip out at the moment.

To implement this on my test template, I modified this file: themes/blackcandy/templates/Layout/page.ss

And placed this code you mentioned in order in

<% if Locale == "locale" %>
<a href="$Link">
<img src="...."/></a>

<% control Translations %>
<% if Locale == "en_GB" %>
<a href="$Link">
<img src="http://nisglass.co.nz/img/en.gif" src... /></a>
<% end_control %>

<% if hasTranslation(zh_TW) %>
<a href="$get_homepage_urlsegment_by_locale(zh_TW)">
<img src="http://nisglass.co.nz/img/tw.gif" src... /></a>

<% if Locale != "en_GB" %>
<% if hasTranslation(en_GB) %>
<% else %>
<a href="......</a>

After applying this, it didn't seem to work, and got an error...

Also I can't quite get how to implement this:

edit(forgotten): in page.php page_Controller

if($this->dataRecord->hasExtension('Translatable')) {
i18n::set_locale($this->dataRecord->Locale);
}

Sorry to be a pain, you would really save me if you could assist me with this...!!!!!!

Avatar
ChuckGyver

Community Member, 5 Posts

7 August 2009 at 12:57am

Edited: 07/08/2009 1:13am

<% if Locale == "locale" %>

the "locale" is a placeholder for de_DE, en_US and so

<a href="$Link">
<img src="...."/></a>


<img src="...."/> is a placeholder for the link-text/img/div

if($this->dataRecord->hasExtension('Translatable')) {
i18n::set_locale($this->dataRecord->Locale);
}

has to be placed in /mysite/page.php below
so that it looks like

class Page_Controller extends ContentController {
   
   public function init() {
      parent::init();
      
      if($this->dataRecord->hasExtension('Translatable')) {
         i18n::set_locale($this->dataRecord->Locale);
      }

// Note: you should use SS template require tags inside your templates
      // instead of putting Requirements calls here. However these are
      // included so that our older themes still work

// further code ....

if u have made translations in the cms admin try this in your template(with en_US, de_DE translations in the sitetree for example)

<% control Translations %>

<% if Locale == "en_US" %>
<a href="$Link">English version </a>
<% end_if %>

<% if Locale == "de_DE" %>
<a href="$Link">Deutsche Version</a>
<% end_if %>

<% end_control %>

Avatar
kudesign

Community Member, 64 Posts

7 August 2009 at 1:17am

Wow! it worked! I can finally go to sleep :) Thank you very much, I am more of a graphics person, so if you want a button or banner made, I'll be more than happy to assist.

This is a demo site to see it in action: http://www.nisglass.co.nz/glass/home-3/

I have been reading over and over the doc supplied by SS:

<% if Translations %>
<ul class="translations">
<% control Translations %>
<li class="$Locale.RFC1766">
<a href="$Link" hreflang="$Locale.RFC1766"
title="$Title">
<% sprintf(_t('SHOWINPAGE','Show page in %s'),$Locale.Nice) %>
</a>
</li>
<% end_control %>
</ul>
<% end_if %>

And it mentioned that you need to register the Locale Values in i18n::get_common_locales()

I did not have any idea how to register the Locale values in i18n... you got any idea?

Cheers!!!!!!!!!

Joseph

Avatar
snaip

Community Member, 180 Posts

28 September 2009 at 12:59am

hi

i have a problem with multilanguage site

i want to have site in three languages pl, en, de

in _config.php i add

i18n::enable();

Object::add_extension('SiteTree', 'Translatable');
Director::set_environment_type("dev");

Translatable::set_default_locale("pl_PL");
Translatable::set_default_locale("en_US");
Translatable::set_default_locale("de_DE");

i18n::set_locale('pl_PL');
i18n::set_locale('en_US');
i18n::set_locale('de_DE');

is this correct ?

now in CMS i have three copies of the site for PL,DE and EN

the Home Pages URL now should be ?locale=pl_PL ?locale=en_US ?locale=de_DE but they aren't
when i put in URL one of these language i always get Polish Home Page
Home Pages URL are: PL = home EN = home-2 DE = home-3

my questions are:

1) how to set default language page ?
i tried to change language in browser to EN / DE but i also get Polish Home Page too

2) how to create layout for different language ?
i have same static element which are not modify by CMS (like images for polish, english and deutch version)
some declaration like

<% control Translations %>

<% if Locale == "en_US" %>
<img src="pl_image" />
<% end_if %>

<% if Locale == "de_DE" %>
<img src="de_image" />
<% end_if %>

?

3) i tried to create language switcher like yours ChuckGyver but it doesn't work

could you help me ? i have problem to understand documentation :(

Avatar
biapar

Forum Moderator, 435 Posts

14 January 2010 at 12:36am

Any info?

Avatar
AlknicTeos

Community Member, 5 Posts

19 January 2010 at 10:52pm

hm, Translatable::set_default_lang('pl_PL') is used to give the admin a default language sitetree. so there is no need to call for each languange another set_default_lang.

-> 1) how to set default language page ?
i tried to change language in browser to EN / DE but i also get Polish Home Page too

The page with the URL /home , will be showed first, when entering your site Url. the default lang gets normally the standard URL's like /home ,the translated pages get then /"$original URL" - $Number -> the first translated page of home will be home-2, the second home-3 and so on. (by default)

To make the en_US startpage shown first, when entering your site, you have to change, the url of the current startpage /home to something different like home-$Number and the URL of the en_US startpage to /home. If there is no /home Url , a new Site with /home will be generated , ( at least in older versions)

----

?locale=pl_PL ?locale=en_US ?locale=de_DE,
as far i have seen it, its only used in the admin to switch the language. It cannot be used to switch the language in the frontend. They are adressed with their unique URL.

----

2) how to create layout for different language ?

if u just want to change images for the another languages , u dont have to give them in a control. A simple

<% if Locale == "pl_PL" %>
<img src="pl_image" />
<% end_if %>
<% if Locale == "de_DE" %>
<img src="DE_image" />
<% end_if %>


should reach..

btw, havent testet it with <% _t() %> in the img src

3) i tried to create language switcher like yours ChuckGyver but it doesn't work

Are you sure you have inserted

if($this->dataRecord->hasExtension('Translatable')) {
i18n::set_locale($this->dataRecord->Locale);
}


in the page controller ?

then the template

<div>
// current language, show first
// example for de_De , do it for every language
<% if Locale == "de_DE" %>
<a href="$Link">
<img src="de_DE.jpg" alt="DE" title="DE"/></a>
<% end_if %>
// further languages

// after the current language, show the other languages in fix order

// to not post the current locale a second time
<% if Locale != "de_DE" %>
// is there a translated page ?
<% if hasTranslation(de_DE) %>
<% control Translations %>
<% if Locale == "de_DE" %>
<a href="$Link">
<img src="de_DE.jpg" alt="DE" title="DE"/></a>
<% end_if %>
<% end_control %>
// if there is no translations for this page, link it to the startpage of the locale
<% else %>
<a href="$get_homepage_urlsegment_by_locale(de_DE)">
<img src="de_DE.jpg" alt="DE" title="DE"/></a>
<% end_if %>
<% end_if %>
// same for the other languages.
</div>


or simple, something like

<div>
// will always link to the startpage in another language
<a href= "Url for start page in another language like"'/home-3'"><img src="locale_LOCALE.jpg"></a>
</div>

I hope, i have written no nonsense ...