0

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?

Infinity
  • 828
  • 4
  • 15
  • 41
  • Yes, u can keep user's preferences like Language Choice in Cookies also. There is nothing wrong with this approach. – Suresh May 04 '17 at 07:11
  • Usually asking for a better way or the best way to do something is flagged as "too broad" or "opinion based" – Cleptus May 04 '17 at 07:55
  • @bradbury9 yes, I know, but I don't know how to ask It in other words. I don't know which one way is best and how to achieve It correctly. That's why I'm asking for users suggestions. – Infinity May 04 '17 at 08:11
  • You could go with the language table in second way. Send both languages up to the client and let the client code select which language to present to the user. In that way you would not have to do a round trip to the server if/when the user changes the language. And the route can be the same for both languages. – JustAnotherCoder May 04 '17 at 09:13

3 Answers3

1

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.

varuog
  • 3,031
  • 5
  • 28
  • 56
  • Thank you for an answer, It sounds as nice way to go. Could you explain this part where you wrote about getting language from url, query parameter or cookies? So if I'll use this way - language choice will be invisible in address bar? – Infinity May 11 '17 at 06:37
  • @Infinity have edited the answer. let me know if anything else i can do. – varuog May 11 '17 at 06:52
  • So It looks like URL path is best way to go, how about mod rewriting depending on parameter? How can I `/en/` to address bar if `$lang = "en"`, otherwise left default structure? I don't need to show like this `www.mysite.lt/lt/etc`, I need It only for English `www.mysite.lt/en/etc`, that because for real my domain will be not `.com`, but `.lt` – Infinity May 11 '17 at 06:57
  • @Infinity while you write modrewrite you can check if there is language code is there or not. if not it will be empty that basically means $_GET['lang'] will be empty. you can then just use control structure (if-else) to load the resource if its i not empty. – varuog May 11 '17 at 07:04
  • Thank you for trying to help, I got stuck in this part. Could you provide sample code of modrewrite? – Infinity May 11 '17 at 07:26
  • @Infinity `RewriteEngine on ^http://www.example.com/(en|bn|fr)?(.*)$ http://www.example.com$2?lang=$1` something like this should work. i have not tested it and it is just for example. `(en|bn|fr)` here will be the available language list. please follow the provided mode rewrite doc link for more details – varuog May 11 '17 at 08:28
0

I think the approach with the Database is fine. Whenever you want to change the translation for something you will just have to edit the table entry. You can write a class which doeas the translation for you, so you only have to pass the language-id and the language.

How to save the language/ how to display it: It depends on how it is meant to be used. If it is likely people often share a link to your site, you could inclue the language in the url, e.g. as a GET paramater. If it should "just" stay the same for the user who visited the site, cookies are a nice approach.

Tobias F.
  • 1,050
  • 1
  • 11
  • 23
0

Using a cookie to store the language preference is an option but might cause some issues for SEO and this will be relevant for a job site.

Without using cookies you can either put the language in the directory path as you suggested. You don't need to have 2 separate websites then, you can use url rewriting to change a part of the path into a query parameter (for apache use mod_rewrite).

Using just a query param, just as your second suggestion will work too but looks less nice.

Make sure you offer your users a option to switch language, for example using flag icons or just text links.

Wouter de Winter
  • 701
  • 7
  • 11
  • Thank you for an answer, could you tell me more about putting language in the directory path? If I don't need 2 separate websites how could I achieve It? The same jobs.php file will be used for `www.mysite.com/jobs` and `www.mysite.com/en/jobs`? – Infinity May 04 '17 at 07:23
  • You can have a look here http://stackoverflow.com/questions/16854820/htaccess-rule-for-language-detection – Wouter de Winter May 05 '17 at 08:21