我的仅使用基本php的多语言网站(没有zend_translate,gettext等)在将来会出现问题吗? [英] My multilingual website with only basic php (without zend_translate, gettext, etc...) will I have problems in the future?
问题描述
我尝试了gettext
,但是我的免费主机不允许.我想到了Zend_translate
,但是由于我的页面大部分是静态的,所以我不想使用框架中的元素.
I tried gettext
, but my free hosting doesn't allow it. I thought about Zend_translate
, but I didn't want to use elements from frameworks since my page is mostly static.
所以,我结束了本教程:
So, I ended up with this tutorial:
http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html
作者仅使用基本php(不确定)的地方,并且似乎可行,但是我不确定这是一种好的(或常见的)做法,还是将来会给我带来麻烦(添加和删除一堆代码.
Where the author only use basic php (not sure), and it seems to work, but I'm not quite sure if it a good (or common) practice or if it can cause me problems in the future (adding and deleting bunch of code).
这里是:
common.php :
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'de':
$lang_file = 'lang.es.php';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'languages/'.$lang_file;
?>
languages/lang.en.php:
<?php
/*
-----------------
Language: English
-----------------
*/
define('GREETING, Hello World');
?>
languages/lang.es.php:
<?php
/*
-----------------
Language: Espanol
-----------------
*/
define('GREETING, Hola Mundo');
?>
index.php:
include_once 'common.php';
<p><?php echo LANG_TEST; ?></p>
因此,如果我想将其更改为西班牙语,我只需在URL
中添加?lang=es
(在index.php
之后)
So, if I want to change it to Spanish I just add: ?lang=es
in the URL
(after index.php
)
推荐答案
首先,它不适用于您的代码.您将不得不使用
First off all, it will not work with your code. You would have to use
define('GREETING', 'Hello World').
检查PHP手册中的定义.
第二,为此使用竞争者是一个可怕的想法.您正在用大量常量填充全局命名空间,并冒着常量名称冲突的危险.请参见用户名命名指南.
Second, using contants for this is a horrible idea. You are littering the global namespace with tons of constants and risk constant nameclashing. See the Userland Naming Guide.
如果您不想使用Zend_Translate
(不必为此使用整个框架)并且不能使用gettext
,建议您使用数组来存储翻译,例如像这样的东西:
If you do not want to use Zend_Translate
(you don't have to use the entire framework for this) and cannot use gettext
, I suggest you use arrays for storing the translations, e.g. something like this:
$lang = array(
'greeting' => 'Hello World'
'something' => 'else'
);
然后您可以在模板中像这样使用它:
and then you can use it like this in your template:
<h1><?php echo $lang['greeting'] ?></h1>
这样,您只需确保$lang
尚未在全局范围内定义.
This way, you only have to make sure, $lang
is not already defined in the global scope.
有些人喜欢使用默认语言而不是翻译ID,例如他们更喜欢写
Some people prefer to use the default language instead of translation ids, e.g. they prefer to write
<h1><?php echo t('Hello World') ?></h1>
其中t
将用于将输入字符串映射到输出字符串.然后,翻译数组必须包含完整的句子,然后将其映射到其他语言,例如
where t
would function mapping the input string to the output string. The translation array would have to contain the full sentences then and map these to the other languages, e.g.
$lang = array(
'Hello World' => 'Hola Mundo'
);
但是,当然,您也可以使用$lang['Hello World']
进行访问.对于长字符串它变得尴尬.尽管如此,许多翻译功能仍允许您传递其他参数,以实现如下所示:
But of course, you could just access this with $lang['Hello World']
as well. It just gets awkward for long strings. Many translation functions allow you to pass in additional params though, to allow for something like this:
$lang = array(
'currentTime' => 'The current time is %s'
);
<h1><?php echo t('currentTime', date('H:i:s')) ?></h1>
这篇关于我的仅使用基本php的多语言网站(没有zend_translate,gettext等)在将来会出现问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!