View Full Version : PHP Automatic language system
auriaks
06-02-2010, 01:54 PM
Hi,
I want to create language system for my website.
I have languages.txt where I store all my website's words in many languages like:
piešti = paint = pintura = .... (and so on...)
How I can use words from languages.txt in my website by using PHP.
One more thing: It is possible to activate the language automatically, based on from which country visitor comes?
E.g. if visitor is from England word would be visible as "paint"
if visitor is from Lithuania word would be visible as "piešti"
if visitor is from Spain word would be visible as "pintura"
Thanks in advance :D
djr33
06-02-2010, 02:18 PM
It's all possible, but it gets complicated.
The simplest to answer is the idea of geolocation. The only information that you have is the IP address. This gives about 90% reliable information for the location of the visitor, but that isn't all the information you need: what if someone is on vacation in a different country? Etc.
Using English as your default language is a good idea, then if you want to "guess" based on IP that is ok, but you must be sure to have a clear icon (a flag is good) for the language and a way to change it. If a visitor doesn't understand the webpage (and it's not in a language they'd expect-- for example English is good-- people won't be angry because your page is in English), they will leave. In other words, if you guess wrong, you will lose a visitor (in some cases), unless you have a very clear way to choose the language.
So think of geolocation technology as a helpful guess to give the users a hint, but not something that really is reliable.
You could automate that on the first time the load your site you change the language based on IP, or you could give them a message in the language. Display the site in English, but add a popup of some sort that says "Hello, visitor. Would you like to see this in your language?" (that notice should be in whatever the IP suggests).
For that, there are a few options, but one that I like is there. There are free and (improved) paid versions:
http://www.maxmind.com/
---
Translation is complicated. First, the only way to do this accurately is to use phrases. So you have started well.
Here are the basics (and it's too much to get into the details right now-- but ask if you have specific questions).
1. Create a language file for each language, like phrases.english.txt, etc.
2. NEVER write text directly into your HTML. Use a PHP to create it instead.
You can use a function like language('phrase');
Or you can just use a variable: some systems use something like echo $text['456'];, but that is confusing.
Notes:
You probably will want more than one file per language: index.english.txt, help.english.txt, etc.
You will need a way to load these phrases and track the user's language. I suggest creating a session variable ($_SESSION['language']) and using that, along with a function like load_langugage_file('index');
I'm working on a big project involving this right now so ask if you have more questions.
By the way, there are also standard ways (I believe), but I don't like them so I'm ignoring some of them.
It's called 'i18n'.
Look at this, for example:
http://www.debian.org/doc/manuals/intro-i18n/
As a general note remember that the most important part of designing a translation system is to allow the translations to be very different. Some languages have gender, some languages have case, some languages have plural, etc., and others don't. When you are translating the LEAST SPECIFIC situation is better, because then the language can decide. If you force a phrase to work a certain way, it might not translate well. For example, you could try to do: "I like $sitename". But then in Japanese that has to be "Watashi-wa $sitename-ga suki-des" [for-me $sitename is-likeable]. If you allow the whole phrase to be translated, that's fine. But if you try to only translate "I like" then it will not work. Hope that helps. Obviously in real situations everything gets much more complex. For example, I still don't know how to do "Welcome, $user!", and have the gender correct in Spanish, for example.
And this is very important: use UTF8! If you do any other character encoding it will at some point give you problems with unusual characters (unless every language you're using only has Western characters).
You must use UTF8 in your HTML, in your HTML editor, in your database, and possibly on the server.
auriaks
06-02-2010, 03:59 PM
That was a really good post... Some case, it haven't answered my question, in some I got my answers :D
What I think:
I will use flags and English as a default language.
I WANT to use session with language, but I don't know how to use it...
I will use UTF8.
I can use both: text file and mySql... Text file would be more complex, but more comfortable for me.
I will use translation for "all sentence" like in phpBB-forums... And I want to use text file, can you help me to create language.txt (where I will store ALL sentenses in one language) which I could use in my website..?
Thanks again :D
djr33
06-02-2010, 07:22 PM
Text files are fine. I actually use PHP files with arrays of values:
$language['term']['subterm']['name'] = 'Hello World';
Then all you need to do is include the file and you have $language available. (My system is more complex than this, but it's based on this idea).
MySQL isn't really needed for this, but it would work also. It's easier if you plan to edit the translations on the site (rather than in the text file).
Session is easy enough, if it's configured correctly on your server.
Include <?php session_start(); ?> at the very beginning of every page.
Then you can use $_SESSION['variablename'] on all of your pages. It will be a constant value (between pages) for any single user, and NEVER shared between users.
Also, unless you echo it, the user cannot see the values in $_SESSION.
auriaks
06-03-2010, 05:23 AM
How to use "hello world" in my main site? term, subterm, name? :)
djr33
06-03-2010, 07:40 AM
include('/languagefiles/file.language.php');
echo $language['1']['2']['3'];
Alternatively you can use a simpler naming system with only one value, like ['1.2.3'] rather than ['1']['2']['3'].
auriaks
06-03-2010, 03:15 PM
I still don't get the number meanings... Can you write me a simple working example?
djr33
06-03-2010, 09:58 PM
It's just a standard array (many layers):
title.language.php
$language['variable'] = 'value';
Include the file, then echo that in your layout.
auriaks
06-03-2010, 10:07 PM
in the file should be:
$language['paint'] = 'piešti';
$language['read'] = 'skaityti';
$language['get'] = 'gauti';
and so on..??
And then in .php file:
include('/languagefiles/file.language.php');
echo $language['paint'];
and the output will be "piešti"??
djr33
06-03-2010, 10:11 PM
Yes, that's correct. It may get more complicated than that, but that's the basic idea for any system like this. Or you could use a database if you'd like.
Also, the 'name' for the variable is not a language. You will need a file that looks like this also:
$language['paint'] = 'paint';
(english)
auriaks
06-03-2010, 10:14 PM
why? if I use english.language.php
then I will get
$language['paint'] = 'paint';
if
lithuanian.language.php
$language['paint'] = 'piešti';
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.