Bug Report #949

Multi-Lang Module.

Added by Kiall Mac Innes about 3 years ago. Updated almost 3 years ago.

Status:Closed Start date:
Priority:Normal Due date:
Assignee:Redmine Admin % Done:

0%

Category:Modules
Target version:2.4
Resolution:wontfix Points:

Description

I've taken the multi-lang tutorial and forum thread code and merged it into a working, easy to use, module.

I've removed the dependencies on other code I use, so this should work as advertised.

One thing to note that's important, the config option fallback_language is pretty much useless without changes to system/core/Kohana.php - I haven't made these changes yet myself, so obviously they aren't in the attachment! BUT it is used in the hook.

As far as I can tell, this will be incompatible with the current set of changes to the locale.php config file that are suggested in the tutorial and forum thread,It may be worth going back to the standard system/config/locale.php. - But I haven't tested this.

Anyway, overall as i said in the forum post - its pretty much identical to the tutorial/forum thread version, except that its much easier to install and enable/disable. Also - I don't use the url_lang helper, I've just extended the original url helper to generate the correct url using the standard functions.

This is a feature which I use on almost every project - and setting it up every time is a PITA, integrating it into the core, or including it as a core module would be great!

Also - My/Managed I.T.'s Copyright notices etc can be stripped, I can't speak for the other notices but as far as I can tell, the code is already open source.

Kiall

multi_lang.zip (13.9 kB) Kiall -, 12/03/2008 04:21 pm

multi_lang.2.zip - Updated multi lang module - no hooks or routes.php changes required... (10.4 kB) Kiall -, 12/22/2008 03:41 am

History

Updated by Kiall Mac Innes about 3 years ago

Some changes are needed prevent the need to edit routes depending on if multi lang is on or off..

I'll look into this during the week - I have a feeling it would be cleaner and easier to add this directly to core, any of dev's have an issue with me adding this to the system folder and submitting a patch? - Basically, is this a feature that would be accepted into the core?

Updated by Redmine Admin about 3 years ago

I think it would be good to have this as an official module. Please submit a patch which includes your proposed changes to system. Keeping as much as possible in the module.

Kohana should support multi-language site setup out of the box, IMO. I haven't spoken
to other devs, but don't see why not. (Anyway, probably won't make it for 2.3)

Updated by Redmine Admin about 3 years ago

Just to correct my earlier post. If you want to submit a patch that puts the whole multi-lang into core, feel free. But I do think it would be better as a module, with a minimum of required changes to core stuff.

Updated by Kiall Mac Innes about 3 years ago

Sure, over the next week or so i'll clean things up with the suggestions from the forum posting and do my best to keep it as a module.

One thing that I think will need to be added to core is some mods to the url helper - really, we dont want every link pointing to /controller/action and then having the user redirected to /en/controller/action... the URL helper will need to support this.

My options are:

extend the helper - I'd prefer to avoid this as it can conflict with other peoples code.

make some minor changes to the core, keeping the bulk of the functionality in the module - I would possibly add support for a user defined prefix to the urls outputted by the url helper, and then use this in the module.

add the whole thing to the core - This would probably be the cleanest method, but I understand the devs reluctancy to add featues like this into the core.

Any suggestions or comments?

Updated by Kiall Mac Innes about 3 years ago

I've made a few changes and have everything working with no core changes - although, a core lib and a core helper have been extended - not ideal.

I've removed the hook, and switched to extending the router class instead...
And the config/routes.php changes to incorporate the language are no longer required.

So anyway - We're at 3 files total:
modules/multi_lang/helpers/MY_url.php
modules/multi_lang/libraries/MY_Router.php
modules/multi_lang/config/multi_lang.php

Can people take a look and let me know if it works for them?

@zombor - 2.4? No chance of 2.3.1?

Updated by Jeremy Bush about 3 years ago

Replying to [comment:8 kiall]:

@zombor - 2.4? No chance of 2.3.1?

2.3.1 is purely bug fixes. 2.4 is for feature changes/enhancements, which this is.

Updated by Jeremy Bush about 3 years ago

This might belong better as a module add-on on the kohanausers.org module repository that is in the works...

Updated by Kiall Mac Innes about 3 years ago

@zombor 2.3.1... ah...

I haven't seen anything on kohanausers.org? Has the project been started yet?

Sounds like the right place for this though...

Updated by Rodrigo - about 3 years ago

I'm discovered this now, and works fine! Amazing job...a simple module package!

But a suggestion: For more compatiblity, and to work fine under 'sublanguages' scenario, example:
'pt-pt' Portuguese from Portugal
'pt-br' Portuguese from Brazil
'en-gb', 'en-us'.

I replaced this:

    foreach(Kohana::user_agent('languages') as $language)
    {
        $new_langs[] = substr($language, 0, 2);
    }

With this:

    foreach(Kohana::user_agent('languages') as $language)
    {
        if(strlen($language) > 2)
            $new_langs[] = substr($language, 3, 2);
        $new_langs[] = substr($language, 0, 2);
    }

And i'm using the second part of string in config:

$config['allowed_languages'] = array
(
    'en' => 'en_US',
    'br' => 'pt_BR',
    'pt' => 'pt_PT',
);

Updated by Kiall Mac Innes about 3 years ago

Quick fix to a bug I just noticed...

in multi_lang/helpers/MY_url.php swap the url::current function for this:

public static function current($qs = FALSE)
{
    $lang = _;

    if (Kohana::config('multi_lang.enabled')) {
        $lang = Router::$language.'/';
    }

    if ($qs === TRUE) {
        return $lang.Router::$complete_uri;
    } else {
        return $lang.Router::$current_uri;
    }
}

I'll do up a proper fix sometime next week - also i've heard there is an issue with the module when using kohana in a subdirectory.... I'll check it out next week.

As a side note... it seems to work well on 2.3.1.

Kiall

Updated by dougal2 - about 3 years ago

Some URIs should not be redirected to a language specific URI. I have a site that runs an XMLRPC webservice controller and when using this module the webservice client gets redirected to a different URI and the POST data (the webservice request) is gone.

I've changed 1 line in MY_Router.php which allows certain URIs to be excluded from the multi_lang treatment.
MY_Router.php, line 9:
if (Kohana::config('multi_lang.enabled') and !in_array(Router::$current_uri, Kohana::config('multi_lang.nolang_uri'))) {

and a new entry in config/multi_lang.php:
$config['nolang_uri'] = array(
'webservice',
);

This way the webservice is not interrupted with a redirect.

Updated by dougal2 - about 3 years ago

grr here's the code properly formatted:

if (Kohana::config('multi_lang.enabled') and !in_array(Router::$current_uri, Kohana::config('multi_lang.nolang_uri'))) {

// ------------
$config['nolang_uri'] = array(
    'ixr',
);

Updated by dougal2 - about 3 years ago

Additionally, a smarter way to do this would be handy:

<?php if(Kohana::config('multi_lang.enabled') == true): ?>
     <div class="nav_group">
      <?php
       $cl = Router::$language;
       foreach(Kohana::config('multi_lang.allowed_languages') as $code=>$lang):
       Router::$language = $code;
      ?>
      <div class="nav_item_right right">
       <?php echo html::anchor( Router::$current_uri, media::flag($code)); ?>
      </div>
      <?php
       endforeach;
       Router::$language = $cl;
      ?>
     </div>
     <?php endif; ?>

Updated by Kiall Mac Innes about 3 years ago

Replying to [comment:16 dougal2]:

grr here's the code properly formatted:

....

This is something I've noticed recently, I'll add this to the mod when I get a chance this week.

Replying to [comment:17 dougal2]:

Additionally, a smarter way to do this would be handy:
.....

Sure - I'll be adding few extra methods to the url helper - one is basically url::current_lang('fr') - that name needs some work though, basically it would work the same as url::current, but would switch the language for you.

with the "foreach as $code=>$lang" - I'm sure what I can do with that. I'll have a think.

Updated by Kiall Mac Innes almost 3 years ago

Module moved to: http://projects.kohanaphp.com/projects/show/multilang

I'll move over SVN when i get a chance...

No more comments etc in this ticket please!

Kiall

Updated by sherlock_brett - almost 3 years ago

Replying to [comment:13 nighty0]:

But a suggestion: For more compatiblity, and to work fine under 'sublanguages' scenario, example:
'pt-pt' Portuguese from Portugal
'pt-br' Portuguese from Brazil
'en-gb', 'en-us'.

And i'm using the second part of string in config:

It is a good suggestion, but there could be some problems:

1- In Firefox, I found 2 letter code "br" for "Breton" (I don't know the translation for that language, sorry), and "pt-br" for Portuguese-Brazil. In that case, what language should you choose?

2- In IE, Catalan (language from Spain) is "localized" under "ca-ES", but in Firefox, it's "ca". And the same for Gallego (from Galicia). Your way, it would be selected like catalan in Firefox, but it would choose Spanish in IE.

I think it's a great idea to use this kind of selection without bothering the user, but being aware that each browser writes its own code. Any new idea on this?

Updated by Kiall Mac Innes almost 3 years ago

  • Status changed from New to Closed
  • Resolution set to wontfix

Also available in: Atom PDF