Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: PHP Automatic language system

  1. #1
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Question PHP Automatic language system

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Last edited by djr33; 06-02-2010 at 02:24 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    auriaks (06-02-2010)

  4. #3
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    That was a really good post... Some case, it haven't answered my question, in some I got my answers

    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

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    How to use "hello world" in my main site? term, subterm, name?

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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'].
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    I still don't get the number meanings... Can you write me a simple working example?

  9. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's just a standard array (many layers):

    title.language.php
    $language['variable'] = 'value';

    Include the file, then echo that in your layout.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #9
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    in the file should be:

    PHP Code:
    $language['paint'] = 'piešti';
    $language['read'] = 'skaityti';
    $language['get'] = 'gauti'
    and so on..??

    And then in .php file:
    PHP Code:
    include('/languagefiles/file.language.php');
    echo 
    $language['paint']; 
    and the output will be "piešti"??

  11. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •