On my website, at a certain moment, the question of organizing multilingual issues became very acute, and it was not about 2-3 languages, but about how to translate a website into another, absolutely any language could be done in a matter of minutes. In addition, each language should have a unique address, that is, the domain / ru /, the domain / en /, etc. There is not much information on this topic in the network (maybe I was looking bad?) And I had to think for myself. Although my solution is quite simple and it may seem obvious to many, I will bring it anyway, I hope it will be useful to someone. Immediately I warn you that you need a little idea about the kohana 3 framework.
So, what could be the options for organizing multilingualism on the site? The simplest and most obvious option is to create a folder for each language “ru”, “en”, etc. But that would mean copying all the files, and if they changed something, we change all 30 folders ... Oh, horror, only a masochist can develop this theme, so naturally, I began to look for another way that would allow only the language files to be changed with translation .
Obviously, it was necessary to enter in the routing parameter lang, which would go at the beginning of each rule:
Route::set('auth', '<lang>/<action>(/<param>)', array('action' => 'login|logout|registration|forgot|start')) ->defaults(array( 'directory' => 'index', 'controller' => 'auth', )); Route::set('static', '<lang>/<action>(/<uri>)', array('action' => 'page|blog|news|forms|polls')) ->defaults(array( 'directory' => 'index', 'controller' => 'static', 'action' => 'page', )); Route::set('default', '(<lang>(/<controller>(/<action>(/<param>))))', array('param' => '.+')) ->defaults(array( 'directory' => 'index', 'controller' => 'static', 'action' => 'index', 'lang' => 'ru' ));
')
Please note that I installed lang by default on the last route so that you can open a domain without specifying the
domain language, if I didn’t do this, then I would have to always add
the / ru language, well, or redirect each time.
In the main controller in the before method (can this be better done in bootstrap?) I defined the LANG constant:
if ( !defined('LANG') ) { define('LANG', $this->lang()); }
In the lang method, I define the user language by cookie or user ip. The translation file lies in the application / i18n / LANG / controller_name.php folder. I split the translation into controllers, you can do it by action or even put everything in one file, that's how you like your soul and needs of the project.
It remains only to override the method helper'a HTML anchor. To do this, I created in the classes folder an html file with the contents:
<?php defined('SYSPATH') or die('No direct script access.'); class HTML extends Kohana_HTML { public static function anchor($uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = TRUE) { $uri = LANG . '/'. $uri; return parent::anchor($uri, $title, array $attributes, $protocol, $index); } }
I ALWAYS file an address with no domain and no forward slash in this method, if you don’t always do that, you need to process the incoming $ uri in the right way!
Well that's all. Now, in any place, any phrase that needs translation needs to be processed with the __ () function. But note that you need to choose a basic language: if the necessary translation is not present, then the phrase will be displayed in that language. In addition, you do not have to do the translation files for this language. I chose Russian as such language, but I think to switch to English.
I admit that in some places this implementation may not be Feng Shui, but it works and works very flexibly. Thank you all for your attention.