使用PHP进行语言翻译 [英] Language translation using PHP
问题描述
我正在PHP中开发示例网站,我需要将整个网站翻译成波斯语.怎么可能在php中呢?我尝试使用以下代码..此代码对于deutsch转换将正常工作.
Hi i am devloping sample site in php i need to translate whole website in to persian. how can it possible in php?? I have tried using the following code.. This code will working fine for deutsch conversion.
1. class.translation.php
<?php
class Translator {
private $language = 'en';
private $lang = array();
public function __construct($language){
$this->language = $language;
}
private function findString($str) {
if (array_key_exists($str, $this->lang[$this->language])) {
echo $this->lang[$this->language][$str];
return;
}
echo $str;
}
private function splitStrings($str) {
return explode('=',trim($str));
}
public function __($str) {
if (!array_key_exists($this->language, $this->lang)) {
if (file_exists($this->language.'.txt')) {
$strings = array_map(array($this,'splitStrings'),file($this->language.'.txt'));
foreach ($strings as $k => $v) {
$this->lang[$this->language][$v[0]] = $v[1];
}
return $this->findString($str);
}
else {
echo $str;
}
}
else {
return $this->findString($str);
}
}
}
?>
2.Register.php
2.Register.php
<?php
require_once('class.translation.php');
if(isset($_GET['lang']))
$translate = new Translator($_GET['lang']);
else
$translate = new Translator('en');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php $translate->__('CSS Registration Form'); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15"/>
<link rel="stylesheet" type="text/css" href="css/default.css"/>
</head>
<body>
<form action="" class="register">
<h1><?php $translate->__('Registration'); ?><a class="flag_deutsch" title="deutsch" href="register1.php?lang=de"></a><a class="flag_english" title="english" href="register1.php"></a></h1>
<fieldset class="row1">
<legend><?php $translate->__('Account Details'); ?></legend>
<p>
<label><?php $translate->__('Email'); ?> *</label>
<input type="text"/>
<label><?php $translate->__('Repeat email'); ?> *</label>
<input type="text"/>
</p>
</fieldset>
<div><button class="button"><?php $translate->__('Register'); ?> »</button></div>
</form>
</body>
</html>
是否可以使用此代码转换为其他语言?我将register1.php?lang = de更改为register1.php?lang = fa(波斯语)..但是没有什么可能.
Is it possible to transilate to other laguages using this code?? I changed register1.php?lang=de to register1.php?lang=fa(persian).. But nothing hapens..anybody plese help
推荐答案
我可以尝试使用此方法.该方法已在我们的系统中实现,并且可以正常使用.
AS per me you can try this method.This method is already implemented in our system and it is working properly.
制作每种语言的php文件并定义所有变量,并在页面中使用这些变量.
Make php file of each language and define all the variables and use those variables in pages.
例如 对于英语
english.php
$hello="Hello";
persian.php
$hello=html_entity_decode(htmlentities("سلام"));
现在使用此变量来进行这样的页面.
Now use this variable to page like this.
your_page.php
<label><?php echo $hello; ?></label>
您已根据从URL获取语言变量加载了特定的语言文件.
You have load specific language file as per get language variable from URL.
最好将此语言变量定义到配置文件中.
It is better that you have define this language variable into config file.
config.php
if(isset($_GET['lang']) && $_GET['lang']=='persian')
{
require_once('persian.php');
}
else
{
require_once('english.php');
}
这篇关于使用PHP进行语言翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!