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'
'?>
Related
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
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.
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
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.
I am building a client site with english and french versions.
On the english pages, I'd like users to be able to click a 'french version' link that would automatically take them to the french version OF THAT PAGE.
So if my url structure is like this:
http://mysite.com/en/page-name
I'd like the 'french version' link to point to:
http://mysite.com/fr/page-name
Can someone please offer me the php to take the current page url, and substitute the /en/ for /fr/ in the link code? I know this is probably something very simple, but I'm a php newb.
In your page template or rendering just use this for your link. Assume the current language is stored in a variable called $current_lang. At the top of your page add this code (it doesn't have to be at the top, just somewhere before you try to use the link):
<?php
$french_link = str_replace("/$current_lang/", '/fr/', $_SERVER['REQUEST_URI']);
?>
Now just use $french_link as your link to that page. So in your anchor link use this:
French Version
Note that most servers have PHP "short tags" enabled so you could use this for your link:
French Version
The <?= syntax is just shorthand for <?php echo. I personally prefer this syntax, but there are many on the other side who have good reasons for not using it. See this discussion for details.