Change language on the fly codeigniter - php

I have a welcome controller, which allows the user to choose a language (en or fr) which then points to it's respective controller (en or fr) so the url looks like this www.xxx.com/en/func/func. I would like so that there can be a link that can change the language, and I would like it to switch language but stay at the same page. Simply grab the corresponding language lines from the proper language files.
Which is the best way to do this?

You should use routing for this, in your routes.php you should add this:
$route['([a-z]{2})/(:any)'] = 'yourdefaulthomecontroller/$2/lang/$1'; // rearrange as you like
Then in your default controller you could find the rsegment and use it against whatever you need.
print_r($this->uri->rsegment_array()); // This will print out the routes
In practical:
if($this->uri->rsegment(3) == 'lang' && $this->uri->rsegment(4))
{
// Do something
}

the easy way is to add to your change language links variables you need to be saved after page reload
so look in your code for all navigation variables and simply add them to your language links

How do you currently achieve the I18N?
Whenever I deal with multiple languages on a site I use parser class as it allows to add multiple languages very easily.
By using that it is quite easy to switch languages by determining language requested in the url.
If by "on the fly" you mean changing the language without realoading the page then that's a whole other thing.

Related

Best way to include different languages into a webpage

I would like to include a language menu on the top of my webpage where you can select between the languages English (en), Spanish (es) and German (de) (More languages might be added in the future). My website consists of the three pages nav.php, index.php and faq.php.
However, I am not sure about the best way to implement the languages on my website. So far I have come up with the following options:
(1) Create every page of the website in a different language and put it in the same folder:
website/pages/nav-en.php
/nav-es.php
/nav-de.php
/index-en.php
/index-es.php
...
(2) Create every page of the website in a different language and create an own folder for each language:
website/en/nav-en.php
/index-en.php
/faq-en.php
website/es/nav-es.php
/index-es.php
/faq-es.php
....
(3) With the options (1) and (2) described above I see the issue that code changes have to be done for each of the pages manually. Therefore, my solution would be to divide the functional code from the text content and loading the text file depending on the language that is selected in the menu:
website/pages/nav.php
/index.php
/faq.php
website/text-content/nav-text-en.php
/nav-text-es.php
/nav-text-de.php
/index-text-en.php
/index-text-es.php
....
Since I am a newbie in webdeveloping I wanted to ask you:
(a) Are the options described above the only once or is there another (better) way to include different languages on a webpage?
(b) Based on your experiences which option would you recommend?
(c) Is there anything else I need to consider?
Thanks for any advice.
You can use gettext for translation. This way you can have one file with structure and text in the same place for all languages, and translate your strings with an editor (like poedit), which could also be done by a translator. The translator would then only see the strings in one language and have to fill in the translated version, without having to deal with the HTML around it.
There is a shortcut to the gettext() function, _(), so you can write something like
<?=_("Linktext in different languages")?>
It's a bit more work to setup once, but after that, adding additional languages will be a lot easier than having different files with all the structure included, which will quickly become a maintenance nightmare.
(a) Are the options described above the only once or is there another (better) way to include different languages on a webpage?
I think using a $_GET param for both language and content is better way. For example,
index.php?page=faq&lang=en
(b) Based on your experiences which option would you recommend?
Using the power of php language is my advice. You may forget about static HTML website architecture.
(c) Is there anything else I need to consider?
If you're using apache, I suggest to use mod_rewrite. This way each page URL looks better:
index.php/en/faq
instead of
index.php?page=faq&lang=en
Well if you google at localization for PHP you will come up with multiple idea's. Personally I would use gettext. This way you can also format the numbers in the correct format.
My experience so far:
It's a nightmare to improve or even maintain a webpage where different views of the same content are based on different files, e.g. your start-page in english, german and spanish.
Therefore, it is a good approach to decouple the HTML-frame (+ CSS) from the language-dependent text. You can do that by implementing your own ideas ... or you are looking for a php framework which provides such functionality. I'm mainly a Java-guy so I don't know such framework. I guess Laravel and Zend Framework 2 could provide localization. Like others mentioned in the thread gettext also looks like a good way to realize localization in your project.
Hope that helps.
To build a multilangage website with easy maintenance, you can do this way :
1. You create ONE file by langage
en.php // english content
es.php // spanish content
...
2. Each file is an array with the same key, that content ALL the content you need to translate :
<?php
// en.php
$translate = array(
"menu_content" => array(
"home" => "Home page",
"nav" => "Nav page",
...
),
"nav_page_content" => array(
"title" => "Welcome to the nav page",
"content" => "In this page you will find...",
...
),
// same for ALL the content
);
3. Now, I see 2 ways you can add the content of your translation file in your website :
3.1 : You use $_SESSION['lang'] variable or some $_GET['lang'] variable if you have the current langage in your URL (eg. http://www.my-page?lang=en) and you load the php file you need at the begin of your PHP script. Then in your HTML you get the content according to your php array (the same key in ALL langage). For example using my example array for the menu :
<nav>
<ul>
<li id="home"> <?= $translate["menu_content"]["home"]; ?> </li>
<li id="nav"> <?= $translate["menu_content"]["nav"]; ?> </li>
...
</ul>
</nav>
And to change langage, you create a list of your langage and on click you change the $_SESSION['lang'] or $_GET['lang'] value and reload your page with the new php langage content.
3.2 : You can do almost the same without reload the page using $.ajax. The idea is almost the same : you have a list of all the langage of your website with a langage attribute for example :
<ul>
<li data-lg='es'>ES</li>
<li data-lg='en'>EN</li>
...
</ul>
On click, you you get this data-lg value and send it to your Ajax function, calling the php file of the choosen langage :
function changeLangue(lg) {
$.ajax({
type: "POST",
url: lg +".php", // eg. es.php, en.php, etc.
dataType: "json", // you will need to add "echo json_encode($translate);" at the end of your php file to send back JSON content
success: function(response){
// Here your change the content of your website without reloading the page, eg with my nav before:
$("#home").html(response.menu['home']);
$("#nav").html(response.menu['nav']);
...
},
error: function(x,e,t){
manageError(x,e);
}
});
With this method, you will need to change your current langage and rebuild your langage selection too but it's not that hard in JS/jQuery.
I don't know if it's the best way to do it to be honnest but it should work (I did it with the second method to test) and it's easy to add a new langage, you will only have to :
Create a new php file yournewlangage.php with the same array than before and translate the content
Add the yournewlangage to your langage list selection
And that's all if you did it right !
An other idea could be to use a Framework for you website. For example, I'm using Silex right now (micro-framework php), it's like Symfony but lighter and you can use the translation component to help you : https://silex.symfony.com/doc/2.0/providers/translation.html
Framework can help you to win some time if you can use them, take some time to use the best one for what you want to do !
Hope it's clear enought and it helps a little

language link to pick correct language file

I have a two language setup, structured like this:
includes/languages/lang-it [has the defines]
includes/languages/lang-en [has the defines]
includes/languages/languages [has the languages array]
en/files
en/subs/files
it/files
it/subs/files
I have en/sub-1/index.php and en/sub-1/file-1.php and need to be able to change language when in file-1.php page
This is the code I have, and of course it does'nt work
require("../../includes/languages/lang-en.php");
link to file-1 English
link to file-1 Italian
Where LANG_ENG and LANG_IT are the directories (en and it)
So I need it to pick the includes/languages/lang-en.php for the the English, and includes/languages/lang-it.php for the Italian.
I guess I need to "require" the correct language file before "LINK-1", but don't know how.
I solved it like this:
Added $thispage, added variables for the file-name, and put that in the link.
Perhaps there are better ways, but that's what I could think of, and is working.
<?php require("../../includes/languages/lang-en.php");?>
<?php // variables for pages to use for language links
$file_1_en="file-1-en.php";
$file_1_it="file-1-it.php";
?>
<?php if ($thisPage=="page name")
echo'
'?>

How to make clean links if I do no know the url

I try to design a web in different languages.
How can make the link that gives a clean url? Some pages are dynamic so, I do not know the url, I am searching for a way to make that links work in any page.
I know I can do this because it works in any page:
<a href='?lang=en'>en</a>
<a href='?lang=fr'>fr</a>
But, how to do that and be able to get a clean url like this?:
myDomain/someFolder/file.php // the default in English
myDomain/fr/someFolder/file.php // fr for French
(Beware that it is not just change fr to en. In English there is no /en/)
I have found that I have tu use a rewrite rule in the htaccess to change from myDomain/someFolder/file.php to myDomain/Folder/file.php?lang=en But this is not the point, my question is different, I ask: how to make clean links in the html if I do not know the url?
What I would do, which is not anywhere as fancy as an .htaccess rewrite tool...
First, I would do something to get the current page name. This can be as simple as:
$pagename = "contactme.html";
Now, I have an array of available languages:
$languages = array('en'=>'English','fr'=>'French','es'=>'Spanish');
With that, I can slap this on the top of every page:
Select your language: <?php
foreach($languages as $ln=>$language)
print " <a href='mysite.com/$ln/$pagename'>$ln</a> ";
That will spit out a list of abbreviations. Can they be little flags (a lot of people like those). Sure. Change $ln in the anchor to <img src='$ln.png' border='0'> and make sure you have all the images. Can they be in a form with a select list? Sure. Make a select list. Add an 'onchange' trigger to the form to reload to the correct URL. Can they be is a super-fancy rotating flash animation? Yes - if you really want to do something that silly.

Dynamically switch content in website based on user language selection

I am designing a small, simple website that will need two languages. I want to keep things simple. I would like to use placeholders/variables in the code that will have user-visible text. Then, allow the user to the select the language of his choice (using a simple link), and make the entire site's text switch over to that language. I would prefer something similar to a language file, in which the values in the code are simply switched when the user selects his language. Below is a small example to illustrate what I am referring to.
<div class='title'>
[$siteTitleGoesHere]
</div>
I hope that makes sense. I am familiar enough with php and javascript that I might be able to use them to do this, if given direction. Thanks for any help.
Here's one approach..
Create a folder called lang
Inside this folder create your language files, lets say en.php and fr.php
These files contain an array called $lang that contains all the swappable text for your site.
for example in en.php
$lang['header'] = 'Welcome to my site!';
and in fr.php
$lang['header'] = 'Bonjour!'; // my french is awesome
You can then load the right file based on (for example) a session value
session_start();
if ( ! isset($_SESSION['lang'])) $_SESSION['lang'] = 'en';
require ("lang/{$_SESSION['lang']}.php");
echo $lang['header'];
If you wanted to change the language you could do something like this
in your php you will need to switch the session lang value to the new language
if (isset($_GET['lang']))
{
$_SESSION['lang'] = $_GET['lang'];
header("Location: {$_SERVER['PHP_SELF']}");
exit;
}
And you would use a link like this
French Language

What's the best way (to avoid modifying repeated code) to build multilingual web pages

What's the best way (to avoid modifying repeated code) to building multilingual web pages?
I know how to build a multilingual web page without having to modify CSS and Javascript files.
But I can't think of a neat solution for HTML and Php files. Because if I have HTML or Php files for each language, I would have to modify each one if, for instance, I add an extra div or other element.
I was thinking to have something like this:
<div id="multilingual div">
<p><?php echo($multilingual-paragraph); ?></p>
</div>
(So, even if I modify these elements, I will just do it once, because the text that is in other language will show up from the variable).
I don't know Php, so I don't know how to tell Php to display a different variable according to the language (I think it has something to do with IF conditions)
Is this a good way of creating multilingual web pages or there are other methods?
(So with this
Check out the gettext() function. You don't really need to build different files for different languages. Altough, you'll have to struggle with the translation files.
You can implement a constants-solution for output messages. Using APC's cache functions, you can store multiple messages inside the cache and load them according to the pages you're viewing (this might not be an easy solution though, you need to know php for this).
This would allow you to maintain an array with values for each language in the cache. For example:
apc_constants_define('en',array('welcomeMessage'=>'Welcome!'));
apc_constants_define('es',array('welcomeMessage'=>'Bienvenidos!'));
apc_constants_define('de',array('welcomeMessage'=>'Willkommen!'));
through AJAX/select form, you can allow the user to choose the language they want to view your pages.
This language would be stored inside a session:
$_SESSION['language'] = 'en';
Next, on every page's top, you should check the session (simple switch statment) and load the constants from the cache accordingly.
apc_load_constants($_SESSION['language']);
then your html page would look like this:
<h1><?php echo welcomeMessage; ?></h1>
This is, as I see it, the most efficient way of internationalizing your website, and with an easily maintainable system, that doesn't require you to delve into the code when you want to translate your page to Romanian.
As you said, you have php and html language files, one way is to go like this:
$lang = '';
switch ($lang_file)
{
case 'en.php': $lang = 'whatever'; break;
case 'fr.php': $lang = 'whatever'; break;
// etc
}
<div id="multilingual div">
<p><?php echo $lang; ?></p>
// or you may include files
<p><?php include_once ($lang); ?></p>
</div>

Categories