[英]Easy way to translate a website
我有一個相當靜態的網站,只有一些基本的 PHP 用法。 現在客戶想要翻譯這個網站。 我確實有一個解決方案,但它很麻煩,我想知道其他人是如何做的以及標准是什么(在框架等中)。
我的方式(為了更容易理解,我已經簡化了一點):我從數據庫中為每種語言生成一個 PHP 數組,並將這個數組存儲在一個語言文件中,比如西班牙語的 es.php。
然后我使用 tr 函數翻譯 HTML 中的字符串,如下所示:
之前: <h1>Hello World</h1>
之后: <h1><?php echo tr('Hello World'); ?></h1>
<h1><?php echo tr('Hello World'); ?></h1>
<h1>Hola Mundo</h1>
為西班牙語提供<h1>Hola Mundo</h1>
。
這很麻煩且容易出錯。 我必須遍歷每個 .php 文件,並用這個帶有 echo 的 PHP 標記替換所有硬編碼的字符串。
有沒有更好的辦法? 其他人是怎么做的? 如果需要,我可以詳細說明我的實現。
您應該查看 PHP GETTEXT 擴展名,它非常快,並且會掃描您的 PHP 文件中的字符串以使用 .MO 和 .PO 文件進行翻譯
然后,您可以簡單地執行類似__('Hello World');
或者如果您已經擁有tr('Hello World');
所有字符串tr('Hello World');
那么您可以修改tr
函數以通過__(string)
或gettext(string)
傳遞它,例如..
function tr($string){
__($string)
}
我想對你來說有點晚了,但如果像我這樣的人偶然發現了這個線程......因為我目前遇到了與你相同的問題。 不幸的是,似乎沒有一種“不麻煩的方法”可以用 PHP 來做到這一點。 一切似乎都涉及大量的函數調用(如果你有很多文本)。
嗯...有一種方便的方法。 雖然不完全安全。 在發送給用戶之前操作輸出緩沖區:=> http://dev-tips.com/featured/output-buffering-for-web-developers-a-beginners-guide
因此,您可以根據選擇的語言定義一個填充“from->to”-data 的數組,並通過循環替換緩沖區中的所有可讀文本。
但是當然...如果你例如用“send”(德語)替換“send”(英語)並且你鏈接到“send.html”,它會破壞那個鏈接。
因此,如果不僅要翻譯長的、絕對唯一的字符串,還要翻譯較短的字符串,則必須只處理用戶可讀的文本。 也有一個解決方案 - 但是,這是基於 JavaScript 的:=> http://www.isogenicengine.com/documentation/jquery-multi-language-site-plugin/
我翻譯我的網站的方式是在文檔類型下有 lang 然后是你選擇的 lang,例如下面,
然后創建一個 php 函數以通過 $_POST['get'] 獲取它,並讓該函數加載選定的語言文件/db 等,
試試這個解決方案。 這個對我有用。 它有法語和英語翻譯。
索引.php
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>How to Translate your Site in Runtime using Jquery - demo</title>
<link rel="stylesheet" href="css/main.css" type="text/css" />
<script src="js/jquery.min.js"></script>
<script src="js/main.js"></script>
</head>
<body>
<div class="example">
<h3><a href="#">How to Translate your Site in Runtime using Jquery - demo</a></h3>
<div id="content">
<div class="lang_switcher">
<button id="en" class="lang">EN</button>
<button id="fr" class="lang">FR</button>
</div>
<div style="clear:both;"></div>
<!-- nav menu start -->
<ul id="nav">
<li><a href="#nogo" key="home" class="tr">Home</a></li>
<li><a href="#nogo" key="peoples" class="tr">Peoples >></a>
<ul>
<li><a href="#nogo" key="all_list" class="tr">All list</a></li>
<li><a href="#nogo" key="online" class="tr">Online</a></li>
</ul>
</li>
<li><a href="#nogo" key="articles" class="tr">Articles >></a>
<ul>
<li><a href="#nogo" key="js" class="tr">JavaScript</a></li>
<li><a href="#nogo" key="php" class="tr">PHP</a></li>
<li><a href="#nogo" key="html" class="tr">HTML</a></li>
<li><a href="#nogo" key="css" class="tr">CSS</a></li>
</ul>
</li>
<li><a href="#nogo" key="contact_us" class="tr">Contact us</a></li>
</ul>
<!-- nav menu end -->
<div style="clear:both;"></div>
<h2 key="welcome" class="tr">Welcome guests</h2>
<hr />
<div key="a_man" class="tr">A man bribes a rabbit with wicked dentures to run away with him in a sailboat via an ambulance. Bribing Koalas to remain illegally in one place. Trees anchor me in place. / Your mom drives the ambulance, but the city is farther than it appears.</div>
</div>
</div>
</body>
</html>
主文件
// preparing language file
var aLangKeys=new Array();
aLangKeys['en']=new Array();
aLangKeys['fr']=new Array();
aLangKeys['en']['home']='Home';
aLangKeys['en']['peoples']='Peoples >>';
aLangKeys['en']['all_list']='All list';
aLangKeys['en']['online']='Online';
aLangKeys['en']['articles']='Articles >>';
aLangKeys['en']['js']='JavaScript';
aLangKeys['en']['php']='PHP';
aLangKeys['en']['html']='HTML';
aLangKeys['en']['css']='CSS';
aLangKeys['en']['contact_us']='Contact us';
aLangKeys['en']['welcome']='Welcome guests';
aLangKeys['en']['a_man']='A man bribes a rabbit with wicked dentures to run away with him in a sailboat via an ambulance. Bribing Koalas to remain illegally in one place. Trees anchor me in place. / Your mom drives the ambulance, but the city is farther than it appears.';
aLangKeys['fr']['home']='Accueil';
aLangKeys['fr']['peoples']='Peuples >>';
aLangKeys['fr']['all_list']='Toutes les listes';
aLangKeys['fr']['online']='En ligne';
aLangKeys['fr']['articles']='Articles >>';
aLangKeys['fr']['js']='JavaScript';
aLangKeys['fr']['php']='Php';
aLangKeys['fr']['html']='Html';
aLangKeys['fr']['css']='Css';
aLangKeys['fr']['contact_us']='Contactez nous';
aLangKeys['fr']['welcome']='Bienvenue aux invites';
aLangKeys['fr']['a_man']="Un homme soudoie un lapin avec des prothèses méchantes pour s'enfuir avec lui dans un voilier via une ambulance. Corruption des Koalas pour qu'ils restent illégalement à un endroit. Les arbres m'ancrent en place. / Votre mère conduit l'ambulance, mais la ville est plus loin qu'il n'y paraît.";
$(document).ready(function() {
// onclick behavior
$('.lang').click( function() {
var lang = $(this).attr('id'); // obtain language id
// translate all translatable elements
$('.tr').each(function(i){
$(this).text(aLangKeys[lang][ $(this).attr('key') ]);
});
} );
});
還有一些 css-main.css
body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.example{background:#FFF;width:500px;height:500px;font-size:80%;border:1px #000 solid;margin:0.5em 10% 0.5em;padding:1em 2em 2em;-moz-border-radius:3px;-webkit-border-radius:3px}
.lang_switcher{float:right;overflow:hidden;}
/* nav menu styles */
#nav,#nav ul{list-style:none;font:10px verdana, sans-serif;border:1px solid #000;background:#fff;position:relative;z-index:200;border-color:#eca #b97 #a86 #edb;border-width:1px 2px 2px 1px;margin:0;padding:0 0 5px}
#nav{height:25px;padding:0}
#nav table{border-collapse:collapse}
#nav li{float:left;padding:0 20px 0 10px}
#nav li li{float:none}
#nav li a li{float:left}
#nav li a{display:block;float:left;color:#888;height:25px;padding-right:5px;line-height:25px;text-decoration:none;white-space:nowrap}
#nav li li a{height:20px;line-height:20px;float:none}
#nav li:hover{position:relative;z-index:300;background:#fff}
#nav a:hover{position:relative;z-index:300;text-decoration:underline;color:#b75}
#nav :hover ul{left:0;top:22px}
#nav a:hover ul{left:-10px}
#nav li:hover li:hover > ul{left:-15px;margin-left:100%;top:-1px}
#nav li:hover > ul ul{position:absolute;left:-9999px;top:-9999px;width:auto}
#nav li:hover > a{text-decoration:underline;color:#b75}
#nav a:hover a:hover ul,#nav a:hover a:hover a:hover ul,#nav a:hover a:hover a:hover a:hover ul,#nav a:hover a:hover a:hover a:hover a:hover ul{left:100%;top:-1px}
#nav ul,#nav a:hover ul ul,#nav a:hover a:hover ul ul,#nav a:hover a:hover a:hover ul ul,#nav a:hover a:hover a:hover a:hover ul ul{position:absolute;left:-9999px;top:-9999px}
我做了一個課程來做我的
class WILang
{
function __construct()
{
$this->WIdb = WIdb::getInstance();
}
public static function all($jsonEncode = true) {
// determine lanuage
$language = self::getLanguage();
//echo $language;
$WIdb = WIdb::getInstance();
//$file = WILang::getFile($language);
//echo $file;
//echo $language;
if ( ! self::isValidLanguage($language) )
die('Language file doesn\'t exist!');
else {
$sql = "SELECT * FROM `wi_trans` WHERE `lang` = :file";
$query = $WIdb->prepare($sql);
$query->bindParam(':file', $language, PDO::PARAM_STR);
$query->execute();
//$result = array();
while ($result = $query->fetchAll(PDO::FETCH_ASSOC)) {
echo "{";
foreach ($result as $res) {
echo '"' .$res['keyword'] .'":"' . $res['translation'] . '",';
//return array($res['keyword'] => $res['translation'] ,);
}
echo "}";
}
}
}
public static function get($key ) //, $bindings = array()
{
// determine language
$language = self::getLanguage();
$WIdb = WIdb::getInstance();
$sql = "SELECT * FROM `wi_trans` WHERE `keyword`=:key AND lang=:lang";
$query = $WIdb->prepare($sql);
$query->bindParam(':key', $key, PDO::PARAM_STR);
$query->bindParam(':lang', $language, PDO::PARAM_STR);
$query->execute();
$res = $query->fetch(PDO::FETCH_ASSOC);
if($res > 0)
return $res['translation'];
else
return '';
}
public static function setLanguage($language)
{
// check if language is valid
if ( self::isValidLanguage($language) ) {
//set language cookie to 1 year
setcookie('wi_lang', $language, time() + 60 * 60 * 24 * 365, '/');
// update session
WISession::set('wi_lang', $language);
//refresh the page
header('Location: ' . $_SERVER['PHP_SELF']);
}
}
public static function getLanguage()
{
// check if cookie exist and language value in cookie is valid
if ( isset ( $_COOKIE['wi_lang'] ) && self::isValidLanguage ( $_COOKIE['wi_lang'] ) )
return $_COOKIE['wi_lang']; // return lang from cookie
else
return WISession::get('wi_lang', DEFAULT_LANGUAGE);
}
private static function getTrans($language)
{
$WIdb = WIdb::getInstance();
//$file = WILang::getFile($language);
//echo $file;
//echo $language;
if ( ! self::isValidLanguage($language) )
die('Language file doesn\'t exist!');
else {
//$language = include $file;
//return $language;
$sql = "SELECT * FROM `wi_trans` WHERE `lang` = :file";
$query = $WIdb->prepare($sql);
$query->bindParam(':file', $language, PDO::PARAM_STR);
$query->execute();
//$result = array();
while ($result = $query->fetchAll(PDO::FETCH_ASSOC)) {
echo "{";
foreach ($result as $res) {
echo '"' .$res['keyword'] .'":"' . $res['translation'] . '",';
//return array($res['keyword'] => $res['translation'] ,);
}
echo "}";
}
}
}
private static function getFile($language)
{
$WIdb = WIdb::getInstance();
$sql = "SELECT * FROM `wi_lang` WHERE `lang` = :file";
$query = $WIdb->prepare($sql);
$query->bindParam(':file', $language, PDO::PARAM_STR);
$query->execute();
$res = $query->fetch(PDO::FETCH_ASSOC);
//echo $res['lang'];
if ($res > 0)
return $res['lang'];
else
return '';
}
private static function isValidLanguage($lang)
{
$file = self::getFile($lang);
//echo $file;
if($file == "")
//if ( ! file_exists( $file ) )
return false;
else
return true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.