Welcome Guest, Not a member yet? Register   Sign In
Confused about language files
#1

[eluser]peredurabefrog[/eluser]
Hi,

I'm writing a bilingual site in CI version 2. Following the instructions in the user guide I've created language files at application/language/english/english_lang.php and application/language/welsh/welsh_lang.php. In autoload.php I've added 'english' and 'welsh' to the $autoload['language'] array:

Code:
$autoload['language'] = array('english', 'welsh');

What I want to do in my application is to test for the language the user wants and then load the correct strings for things like titles, so in english_lang.php I have, for example:

Code:
$lang['en_title_home'] = "Wales Legislation Online: Home";

And in welsh_lang.php I have:

Code:
$lang['cy_title_home'] = "Deddfwriaeth Cymru Arlein: Hafan";

The language the user wants is passed into every controller function as a parameter, for example:

Code:
function index($language = 'en') { ... }

So I'm hoping that in each function I can do something like:

Code:
if($language == 'en')
  $data['title'] = $this->lang->line('en_title_home');
else
  $data['title'] = $this->lang->line('cy_title_home');

However, when I try to run the application it fails with the following error:

Code:
Unable to load the requested language file: language/english/welsh_lang.php

From what I can tell, it's the autoloading that's failing, although I could be mistaken. Obviously I'm misunderstanding the user guide so if anyone could take the time to put me straight, I'd be very grateful.

Thanks in advance,


Peredur
#2

[eluser]smilie[/eluser]
As far as I know, you can not autoload two languages.

What I have done in a multilanguage site is;
Same directory structure as yours (two languages, with same file names).
I however store user language in the session and then when I need language files, I do (in controller / view, where ever you need language files):

Code:
$this->lang->load('filename', $this->session->userdata('language');
# Whereby in session I have: english, dutch or whatever (same name as directory name)

Also - very important - my language defines in the language file itself have _same_ names; for example:

english/something.php:
Code:
$lang['title_home'] = "Wales Legislation Online: Home";

And ie. welsh/something.php:
Code:
$lang['title_home'] = "Deddfwriaeth Cymru Arlein: Hafan";

This way, when I need to include that line, I only call appropriate language and as define (key) is same in both languages, it can always get it out Smile

Code:
$this->lang->line('title_home');

Hope this helps!

Cheers,
Smilie
#3

[eluser]peredurabefrog[/eluser]
[quote author="smilie" date="1295904982"]As far as I know, you can not autoload two languages.

What I have done in a multilanguage site is;
Same directory structure as yours (two languages, with same file names).
I however store user language in the session and then when I need language files, I do (in controller / view, where ever you need language files):

...

Hope this helps!

Cheers,
Smilie[/quote]

Thanks, smilie. I'll play around with that.

I should point out though that the user guide does say you can autoload more than one language:

Quote:Auto-loading Languages

If you find that you need a particular language globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the language(s) to the autoload array.

Note the, "... adding the language(s) to the autoload array". From this I take it that you can autoload more than one language.

Thanks again,


Peredur
#4

[eluser]WanWizard[/eluser]
This is indeed something that could be improved.

When talking about languages where, they mean language FILES, not languages.

Only one language can be active at any given time, if you do want to have your original files (en_title and cy_title), do not create a separate 'welsh' language folder, but store the 'welsh_lang.php' file in the english folder. This way you can load them both. But I would advice against it, as it is confusing and not very scalable. @smilie's suggestion is the way to go.
#5

[eluser]peredurabefrog[/eluser]
[quote author="WanWizard" date="1295912913"]This is indeed something that could be improved.

When talking about languages where, they mean language FILES, not languages.

Only one language can be active at any given time, if you do want to have your original files (en_title and cy_title), do not create a separate 'welsh' language folder, but store the 'welsh_lang.php' file in the english folder. This way you can load them both. But I would advice against it, as it is confusing and not very scalable. @smilie's suggestion is the way to go.[/quote]

That's very helpful. Thanks.

I've followed @smilie's advice and I now have it working.

Thanks to all.

Cheers


Peredur




Theme © iAndrew 2016 - Forum software by © MyBB