Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to implement multi-language PHP

Tags:

php

First of all, I know that there is a lot of answers about multi-language functionality, I didn't find the answer for what I want.

I thought about three ways that I can work with. I need to create the languages files using PHP each time I'm adding a new value to a web form interface.

The first two are kind of similar - using arrays or defines in a specific language file and include it on the start of the run. But in this case I might load thousands of definitions or arrays just to use a few in each page.

The third way is to create function that is called each time with the keyword or the full phrase and using IF-s or switch to choose the right term (or returning the key called if no match).

What is the best way to do that? I decided to do some tests. I tried three different ways and measured the time and memory took for it:

  1. I defined an array (22 values) with and run over it from 1 to 1,000,000 - checked for calling value - 1 of three by using the % operator to choose what to use and just setting it on a variable

    • Time took: 0.476591110229 second
    • Memory: 6536 bytes
  2. I used the same array (22 values) and called it using function - return $arr[$string]; (just for convenient way to work and the ability to change it to different way if I'll need)

    • Time took: 0.960635185242 second
    • Memory: 6704 bytes
  3. I created a function with list of strings and using switch-->case I chose the returning string

    • Time took: 1.46953487396 second
    • Memory: 848 bytes

Well, now the question is what's the right choice - preferring time or preferring memory.

And in case that the sites are big and it would take a lot of memory - I couldn't change it because it is built with arrays - If it works with function I can always change it.

like image 591
Joe Polico Avatar asked Sep 26 '11 18:09

Joe Polico


1 Answers

In terms of code, something like this will be great to you. It must be based in the choice of the user (Choosing a language from a button or menu) or based on the browse language (it is not the best aprouch).

index.php

<?php

// verify the user's choice
if ($_POST[lang] == "en") 
{
    include_once("en_language.php");
}

if ($_POST[lang] == "pt") 
{
    include_once("pt_language.php");
}

// calling a lable in your page
echo LABEL_MENU;


// the rest of your file
...
?>

en_language.php

<?php
// ENGISH Language file
define('LABEL_MENU','Main Menu');

// the whole file with the labels of your system

?>

pt_language.php

<?php
// PORTUGUESE Language file
define('LABEL_MENU','Menu Principal');

// the whole file with the labels of your system

?>

complementing

If you wish use array type than Constant values with define(), but I'm not sure what is faster than..

$arrays = array("MAIN_MENU" => "Main Menu", "LEFT_MENU" => "Left Menu"); 
echo $arrays["MAIN_MENU"];
like image 62
devasia2112 Avatar answered Sep 29 '22 12:09

devasia2112