views:

8610

answers:

7

Hi friends,

I'm researching hours and hours, but I could not find any clear, efficient way to make it :/

I have a codeigniter base website in English and I have to add a Polish language now. What is the best way to make my site in 2 language depending visitor selection?

is there any way to create array files for each language and call them in view files depends on Session from lang selection? I don't wanna use database.

Appreciate helps! I'm running out of deadline :/ thanks!!

+8  A: 

Have you seen CodeIgniter's Language library?

The Language Class provides functions to retrieve language files and lines of text for purposes of internationalization.

In your CodeIgniter system folder you'll find one called language containing sets of language files. You can create your own language files as needed in order to display error and other messages in other languages.

Language files are typically stored in your system/language directory. Alternately you can create a folder called language inside your application folder and store them there. CodeIgniter will look first in your system/application/language directory. If the directory does not exist or the specified language is not located there CI will instead look in your global system/language folder.

In your case...

  • you need to create a polish_lang.php and english_lang.php inside system/application/language/polish
  • then create your keys inside that file (e.g. $lang['hello'] = "Witaj";
  • then load it in your controller like $this->lang->load('polish_lang', 'polish');
  • then fetch the line like $this->lang->line('hello'); Just store the return value of this function in a variable so you can use it in your view.

Repeat the steps for the english language and all other languages you need.

Randell
thanks for quick answer, yes I have seen this. but it is not clear actually :/ about how to make my need, where to create lang files, what format, what kind of structure, how to call, etc... I'm pretty new at codeigniter. thats why I was looking for clear tutorial or sth. :/
artmania
Have you read it thoroughly? It does what you need, really.
Randell
I added a pseudocode for you. I hope it helps.
Randell
heyooo it is great, working well :D thanks a lot for help! lifesaver you are! :) thanks
artmania
You're welcome. Also, off-topic, you might want to increase your accept rate percentage by marking the correct answers to your StackOverflow questions. =)
Randell
AHA :D thanks I didnt know that marking thing. thanks for mentioning
artmania
+1  A: 

Also to add the language to the session, I would define some constants for each language, then make sure you have the session library autoloaded in config/autoload.php, or you load it whenever you need it. Add the users desired language to the session:

$this->session->set_userdata('language', ENGLISH);

Then you can grab it anytime like this:

$language = $this->session->userdata('language');
mrinject
yes this was my another issue. you already helped me out :D great thganks! I was also thinking to use cookie to remember visitor's lang selection.
artmania
where should I define these sessions? I think it is not good to define these at controllers for each page. must be a way that I define at a global file and it effects all site. thanks!!
artmania
Somehow, there's a better way to do this. But that's an entirely new question. But this can be done as well just to get you started.
Randell
A: 

When managing the actual files, things can get out of sync pretty easily unless you're really vigilant. So we've launched a (beta) free service called String which allows you to keep track of your language files easily, and collaborate with translators.

You can either import existing language files (in PHP array, PHP Define, ini, po or .strings formats) or create your own sections from scratch and add content directly through the system.

String is totally free so please check it out and tell us what you think.

It's actually built on Codeigniter too! Check out the beta at http://mygengo.com/string

+1  A: 

is there anywhere, if the langauge didn't translated use the default one?

e.g:

english/hello_lang.php

$lang['hello'] = 'Hello';


polish/hello_lang.php

$lang['hello'] = "Witaj";

it is possible is I forget to add $lang['hello'] in polish, the system will auto call back english version of "Hello"? Currently version the system will output blank. Any idea? I tried Jérôme Jaglale version, it also having the same output.

Shiro
A: 

all the answers posted here are great but using them you lose the cashing option of your pages,since its one controller that will be cached and it will be in the last language you set.is there a solution to this other then using a different controller for every language.

axmed
+1  A: 

Hello, How would you manage to get data from a database with different languages, lets say, English and Spanish. Would you make differet tables for each language, or would you create more fields within the existing tables, Just want to hear an opinion.. thanks

Juan Velandia
A: 

you can make a function like this

function translateTo($language, $word) {
define('defaultLang','english');
if (isset($lang[$language][$word]) == FALSE)
return $lang[$language][$word];
else
return $lang[defaultLang][$word];
}

thanhquanky