Search code examples
phpsqlarrayscookiesmultilingual

Web translating - single or separate files for different languages?


SHORT DESCRIPTION

It will be Lithuanian - English website (only 2 languages).

This will be jobs listing website, so content shouldn't be translated. If employer provided job in English so this should be in English even if Lithuanian language is selected. There should be translated only field names, buttons & error messages.


QUESTION IN SHORT

What is better way to have single file index.php for both languages or create separate files for LT and EN languages?


1st WAY

So I can make It in few ways, for example:

www.mysite.com/jobs -- default Lithuanian language
www.mysite.com/en/jobs -- English language

So in this case I need to have 2 duplicate websites (just translated) Lithuanian stored in root/ folder and English stored in root/en/ folder? So If I change something in Lithuanian website I need to make exact changes in English website?

It not looks like good practice to write code twice.


2nd WAY

Create variable something like $lang = "en";, store all field names in database in way like this:

Id    FieldName_lt      FieldName_en
1     Vardas            First Name
2     Pavardė           Last Name
3     El. paštas        E-mail 

And select from database via PHP like SELECT FieldName_ . $lang FROM table..., but It could be SQL Injected If I'll use variable in SQL query.


3rd WAY

Maybe It's better way to store field names in arrays (there will be maybe 150+ fields)?

If I'll go for 2nd or 3rd way, should I save language choice in cookies? So in this way website url always will be like below?

www.mysite.com/jobs.php?lang=en
www.mysite.com/jobs.php?lang=lt

Maybe there is another way to make It, without showing language choice in address bar at all? Or It's bad practice to hide language choice form address bar?


ADDITIONAL

In HTML form I'm using data validation in following:

<input type="text" id="first-name" placeholder="" required 
        data-validation="length alphanumeric" 
        data-validation-length="3-12" 
        data-validation-error-msg="User name has to be an alphanumeric value (3-12 chars)"/>

So how about error message translation?


Solution

  • The same as database approach you can use static file for each language and translation.

    en.php

    return [
      "somekey" => "English Translation"
    ];
    

    lt.php

    return [
      "somekey" => "Lithunian Translation"
    ];
    

    You can then mod rewrite to get language from url if you want some directory structure, or simple query parameter or cookies (as specified by others). If you are using any any RESTfull service it is also possible to set it in HTTP header. Many frameworks also there to help you parse data from url out of the box.

    $langCode is language code fetched from Query PAram, url path, header or cookie

    you can also use http://php.net/file_exists to check if translation file is available or not before you use require_once to pull the translation resource.

    Once you get the language code you can just use

    $stringResource = require_once "lang/{$langCode}.php";
    

    Then you can fetch all the resource by its key from $stringResource.

        <?php $stringResource = require_once "lang/{$langCode}.php"; ;?>
    
    <input type="text" id="first-name" placeholder="" required 
            data-validation="length alphanumeric" 
            data-validation-length="3-12" 
            data-validation-error-msg="<?php echo $stringResource['somekey'] ;?>"/>
    

    You can just edit the translation in editor. wont need to connect to database and as it is just assoc array. it would be way faster.

    Query:

    www.mysite.com/jobs.php?lang=en as already mentioned it will be ignored in term of SEO. Query parameters are ignored by crawlers.

    URL Path

    www.mysite.com/en/jobs.php
    

    here you need to do mod rewrite http://httpd.apache.org/docs/2.0/misc/rewriteguide.html which is basically just catch url and fetch out the en part and rewrite to something like www.mysite.com/jobs.php?lang=en

    Both data can be get from $_GET['lang']. but url path will have benefit on SEO.

    Cookies

    Will not be shown in address bar. but that also means if the link is shared to another user they will not see the language of origin they will see default language.

    https://support.google.com/webmasters/answer/182192?hl=en#1 as per google doc. i believe it would be nice to do it using url path.