I want to write an international Webapp.
The problem I am facing is not just a matter of translation of content and messages but also the layout will have small differences in some languages.
What would be the most natural approach? Different themes for the different locales or perhaps a switch in the rendering?
thanks for your ideas!
That's easy. Just create a different layout for different language.
Thanks, that was one of my ideas too.
Do I have to trigger the appropriate layout in the rendering method of the controler myself or is there some locale dependant automatic? I mean like the apache who distiguishes the pages by the locale ending for example.
Please read the Guide about how to do this. It's very easy.
Sorry, I think it was simply too late when I read this part. I’m sure I read the File Translation part three times and still didn’t get it.
Thanks for pointing me back to it!
yeah, that paragraph may not explain very clearly.
Basically, for layout or views (either controller view or widget view), you can just create a subdirectory named as the language ID, and put the localized view versions under that directory.
When you use one of the render methods to render the view, it will automatically pick up the view which is corresponding to the current application language.
I'm also a little confused - Allow me to quote the documentation:
File translation is mainly used when rendering a view. When calling one of the render methods in a controller or widget, the view files will be translated. For example, if the target language is zh_cn while the source language is en_us, rendering a view named edit would resulting in searching for the view file protected/views/ControllerID/zh_cn/edit.php. If the file is found, this translated version will be used for rendering; otherwise, the file protected/views/ControllerID/edit.php will be rendered instead.
I understand the above like this: If a file exists in a LocalId folder then the file(view) is used
Then I would expect the da version to be used for Danish users etc. , but… that does not work.
I'm pretty sure that it's due to the fact that I need to set something up or do I need to call CApplication::findLocalizedFile() on each call to a render method ?
Some clarification would be nice
I have found the problem - I minor bug in CApplication::findLocalizedFile()
and one have to add:
'language' => 'da' in config/main.php
Thanks for finding the bug. Fixed.
Yes, when using I18N feature, you have to first set language of the application to be different from its sourceLanguage.
And a last question
Until now we discussed how to set the locale explicit as I understood. Where can I react on the CHttpRequest::preferredLanguage setting of the user request? Or is this done automatically?
What has the priority when the users preference settings differ from the language he chose on the website or is it switchable to choose the language either from the users browser settings or a fixed language the user choose so that I can give the user the possibility to override the preferences set in his browser by clicking on the 'en' switch for example?
There is no automatic mechanism in Yii to set language based on user's preference. The application code has to do this explicitly using whatever policy it likes.
So if you want your application to use a language preferred by user, you may set the language property of application before you use any i18n feature. For example, you can do so in controller constructors, or in onbeginRequest event handler to the application.
Just started with Yii, and see a great framework here. i18n is there too, cool. To get some grip on things, I'm planning to port a small webapp to Yii (now CI), but for SEO reasons I would like to add a language ID to the url, except for the default language.
To give an example: default: English =>http://domain/ but for French http://domain/fr/, for Spanish http::/domain/es/ and so on. The language needs to stick at the first position, so http://domain/es/controllerid/action.
The language the user will see the first time can be determined by the preferred language. To avoid messing with controllers for each language, it probably needs to be done before any routing is done (extending CUrlManager), but perhaps there are other possibilities in Yii.
Any suggestions appreciated!
Using a rewrite rule is the easiest in your case, like the following:
RewriteRule ^(es|en|zh)/?(.*) index.php/$2?lang=$1 [QSA]
With this rule, the language id would be a GET parameter.
Then you can define a base controller class, which checks in its constructor to see if 'lang' GET parameter is specified. If not, use Yii::app()->request->preferredLanguage to guess the user's preferred language. If yes, check if the language is supported, and if so, set the application's language property.
All your controllers should extend this base controller class. And you will get the needed i18n support automatically.
Thanks! Works like a charm. Have put the new BaseController class in extensions.
Perhaps it could be done as a preFilter too, but then you have to apply it for every action. Or is there a way to call a preFilter by default?
Yes, a filter applies to all actions by default. The drawback is that you have to configure the filters in every controller class.
Having a base controller class has extra benefit that you can place commonly used logic inside it to share among controllers.
Porting a small CI app to Yii, and I'm having a strange issue here concerning DB and UTF-8.
Adding a text like 'Jesús' to a view displays as it should, but getting the same value from a db record gives 'Jes�s'.
The MySQL db is using utf8_general_ci, and phpMyAdmin (and the old app) shows it ok. Any ideas?
What is your page's charset and language setting?
Add 'charset'=>'utf8' to connection string.
ah great, thanks! Was thinking it was taken from the DB, as per doc:
Defaults to null, meaning using default charset as specified by the database.
Would be nice…