php 多语言php脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17369947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi language php script
提问by Luka Boltes
How to create multi language main menu in html/php script now i have
我现在如何在 html/php 脚本中创建多语言主菜单
<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
<option value='EN'>English</option>
mysite.com/EN
当用户选择英语语言 EN 代码时,它会转到我想要的位置,它还会更改主菜单文本怎么做?这是网站Letalske karte
I found this script http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.htmlBut i don't know how to set to /EN/ as now in this script is set to index.php?lang=en
我找到了这个脚本http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html但我现在不知道如何设置为 /EN/在这个脚本中设置为index.php?lang=en
回答by Dave
My Approach would be to do the following:
我的方法是执行以下操作:
Step 1: Setup a folder tree structure like this:
步骤 1:设置文件夹树结构,如下所示:
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
第 2 步:创建我们的语言文件,我将从 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
您会为其他所有语言重复此操作,例如,我会做 fr languages/fr/lang.fr.php
。注意英文标签是如何保持不变的
<?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
第 3 步:通过 url 变量检查用户是否请求更改语言
<?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.
第 4 步:您可以像这样访问您的语言部分,它会根据加载的语言文件而改变。
<?php
echo $lang['firstname'];
?>
hope this helps get you started as an idea
希望这有助于让你开始一个想法
回答by user1367323
Used the above code, but session is overwritten every load to EN, so changed it to
使用了上面的代码,但是每次加载到EN时会话都会被覆盖,因此将其更改为
<?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');
?>
回答by Rob
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
?>
回答by Max Base
LM PHP (Language management PHP)
LM PHP(语言管理PHP)
Multi-language management and support for the PHP.
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 revtheitroad!");
print_r( $langs -> words );
echo $langs -> word_get("Bye");
Code Repository : https://github.com/BaseMax/LMPHP
代码库:https: //github.com/BaseMax/LMPHP
Full Sample Code : https://github.com/BaseMax/LMPHP/blob/master/Sample.php
完整示例代码:https: //github.com/BaseMax/LMPHP/blob/master/Sample.php