Multi language php script

不羁岁月 提交于 2019-12-03 06:06:50

问题


How to create multi language main menu in html/php script now i have

<li>
<a href="{url p='poceni-letalske-karte.html'}">
<span>{t t="Letalske Karte"}</span>
</a>
</li>

and

<option value='EN'>English</option> which it go to mysite.com/EN i want when user select English language EN code it also change main menu text how to do that ? This is website Letalske karte

I found this script http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html But i don't know how to set to /EN/ as now in this script is set to index.php?lang=en


回答1:


My Approach would be to do the following:

Step 1: Setup a folder tree structure like this:

Languages
 -en
   -lang.en.php
 -fr
   -lang.fr.php
 -de
   -lang.de.php

keep making new folders with all the other languages you want to support

Step 2: Create our language files, i will start with languages/en/lang.en.php

<?php   
  $lang['label']      = 'Value for this label';
  $lang['firstname']  = 'First Name';
  $lang['lastname']   = 'Last Name';
  $lang['phone']      = 'Phone';       
  // ETC
?>

you would repeat this for every other language, ill do fr for example languages/fr/lang.fr.php . NOTE how the labels stay the same in english

<?php   
  $lang['label']      = 'Valeur pour ce label';
  $lang['firstname']  = 'Prénom';
  $lang['lastname']   = 'Nom de famille';
  $lang['phone']      = 'Téléphone';       
  // ETC
?>

Step 3: Check if the user has requested a language change, via a url variable

<?php
  // Start a Session, You might start this somewhere else already.
  session_start();

  // What languages do we support
  $available_langs = array('en','fr','de');

  // Set our default language session
  $_SESSION['lang'] = 'en';   

  if(isset($_GET['lang']) && $_GET['lang'] != ''){ 
    // check if the language is one we support
    if(in_array($_GET['lang'], $available_langs))
    {       
      $_SESSION['lang'] = $_GET['lang']; // Set session
    }
  }
  // Include active language
  include('languages/'.$_SESSION['lang'].'/lang.'.$_SESSION['lang'].'.php');

?>

Step 4: you can access your language parts like so and it would change based on what language file is loaded.

<?php
  echo $lang['firstname'];
?>

hope this helps get you started as an idea




回答2:


Used the above code, but session is overwritten every load to EN, so changed it to

<?php
// Start a Session, You might start this somewhere else already.
session_start();

// What languages do we support
$available_langs = array('en','zh-cn','es');




if(isset($_GET['lang']) && $_GET['lang'] != ''){
    // check if the language is one we support
    if(in_array($_GET['lang'], $available_langs))
    {
        $_SESSION['lang'] = $_GET['lang']; // Set session
    }
}



// Set our default language session ONLY if we've got nothing
if ($_SESSION['lang']=='') {
    $_SESSION['lang'] = 'en';

}


// Include active language
include('languages/'.$_SESSION['lang'].'/lang.'.$_SESSION['lang'].'.php');

?>



回答3:


If your language file is really long, you could break it up by page by adding this above the language include:

// for login page
$textpart = 'login';

and have the language page split up the array thusly

<?php   
switch ($textpart) {
  //login page
  case 'login':
    $lang['label']      = 'Value for this label';
    $lang['firstname']  = 'First Name';
    $lang['lastname']   = 'Last Name';
    $lang['phone']      = 'Phone';
    break;

  //home page
  case 'home':
    // ETC
}

// All pages
   $lang['title']       = 'Title';
   // ETC

?>



回答4:


LM PHP (Language management PHP)

Multi-language management and support for the PHP.


Sample :

<?php
include "LMPHP.php";
/////////////////////////////
$langs = new LMPHP();

$langs -> language_add("en","English");
$langs -> language_add("fr","Germany");

$langs -> language_active("en");
$langs -> word_add("Bye","Good Bye!");
$langs -> word_add("HowAre");

$langs -> language_active("fr");
$langs -> word_add("Bye","Au revoir!");

print_r( $langs -> words );

echo $langs -> word_get("Bye");

Code Repository : https://github.com/BaseMax/LMPHP

Full Sample Code : https://github.com/BaseMax/LMPHP/blob/master/Sample.php



来源:https://stackoverflow.com/questions/17369947/multi-language-php-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!