0

Couldn't find example for my particular case.

I am creating a multilingual website on Kohana 3.3. I need to create URLs like:

http://example.com for English version
http://es.example.com for Spanish
http://fr.example.com for French

and so on. But all examples, that I could find, were like http://example.com/lang/

What do I have to do to accomplish the goal? Do I have to make different applications for every language and then adjust .htaccess? Or there is a way to read the language from URL (in case of lang.example.com).

4

1 回答 1

0

I've never done this so I am merely attempting to describe how I would approach the problem as if I was faced with it.

Firstly, I'd start with setting up the server so all domains point to one, single application (which I assume you already have in place).

Next, in your bootstrap.php, when setting the language, I'd use a helper function which would define it for me, based on the URL the website is accessed at, and my bootstrap would then probably look like this:

/**
 * Set the default language
 */
I18n::lang(MyApp::get_request_language_code());

I would expect my MyApp::get_request_language_code() to return a string that would match the language code used for I18n class. This helper should also redirect the request if the url was invalid (e.g. xxx.example.com).

Then, I'd probably set the base url using $_SERVER['HTTP_HOST'], i.e.:

Kohana::init(array(
    'base_url'      => $_SERVER['HTTP_HOST'],
));

Alternatively, I'd have another helper function which would return the domain prefix which I would concatenate with my actual domain, e.g.:

Kohana::init(array(
    'base_url'      => MyApp::get_request_language_url_prefix().'example.com',
));

By setting the domain name with language prefix in base_url during Kohana::init() you ensure that all urls generated using either Route or URL class have appropriate language included.

And that's how I would try to solve my problem. Again, I'd like to stress that I am only attempting here to come up with a solution to a problem and the above approach has not been tested.

于 2013-03-30T22:45:25.710 回答