【问题标题】:Creating a php file for translating the content dynamically创建一个用于动态翻译内容的 php 文件
【发布时间】:2014-12-05 22:43:32
【问题描述】:

我有一个包含大约 700 个术语的数据库。我只想编写一个名为 internationalize 的单个 php 文件,该文件将检查 mysql 数据库并返回正确的翻译。所有 PHP 变量都已设置,并且有一个包含所有语言翻译的视图。大约有 11 种语言。我看了很多地方,但没有一个适合我的情况。任何建议将不胜感激。

if (!isset($_SESSION))
{
    session_start();
}

if (isset($setup))
{
    $lang=$_SESSION['setupLang'];
}
elseif (isset($_SESSION['lang']))
{
    $lang=$_SESSION['lang'];
}
else
{
require("fetchMainConfig.php");
}

$lang_code = $lang; 
//file_dir contains the language codes for example: il,fr,pt,ge and so on
$sql = "SELECT file_dir FROM `hydroserver_translation`.`language_file_dir`";
if($lang_code = $sql){
    // What should go in here??? What is the best way to
    // dynamically translate the database?
}

我还附上了数据库视图的副本。我打算这样写代码,如果没有语言翻译,默认翻译是英文。

【问题讨论】:

  • 有什么理由不使用 gettext 和 POT 文件?
  • 您要翻译什么文字?我不明白输入是什么。
  • 看起来脚本根据所选择的语言为 php 变量赋值。该脚本似乎生成了几个变量,这些变量从数据库中获取其值并用于脚本的其余部分。
  • 我们希望在翻译时为用户提供更大的灵活性。由于数据库和网站包含多个科学变量,最好让用户输入翻译数据。 @A.L 这些是我们用来在网站上显示信息的所有 php 变量。所以我们基本上是在翻译整个网页的内容。
  • 我认为如果你有值和翻译,你想创建一个变量,$value 可以为每个值访问。你可以写$$database_result["value"] = translation。这个链接解释了它是如何工作的:php.net/manual/en/language.variables.variable.php 但这也可能会覆盖 php 中的重要变量或函数,所以我不推荐这种方法。为此最好使用数组,因为从数组中存储和检索数据更容易。

标签: php mysql language-translation


【解决方案1】:

这就是我所做的并且有效。感谢您的帮助!

<?php
//Connects to the database
$mysqlserver="servername";
$mysqlusername="langreader";
$mysqlpassword="readHSLlang@9";
$error=0;
$link=mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword) or $error=1;

$dbname = 'translation';
mysqli_select_db($link, $dbname) or $error=1;
if(!$error)
{//check which is the session language

$language = $lang_code;
//language file path
$file_path = "languages/" .$language. ".php";
//Check if file exists

$file_exists = file_exists($file_path);
if($file_exists){
// The file exists. Now just check when it was last time created.
$file_created_time =  filemtime($file_path);
$timezone = date_default_timezone_set('UTC');
$current_time = time();
//Time lapse to check the difference between the current time and the last created time
$time_lapse = (abs($current_time-$file_created_time)/60/60);
//SQL statement to access the view from the database
$sql = "SELECT * FROM translation.translations_by_language";
$terms = mysqli_query($link, $sql);
//Will create a new file if it has been more than four hours
if($time_lapse >= '4.0'){
//Deleting the existing file to avoid any parsing errors
unlink($file_exists);
//Writing the new language_file
$lang_file= fopen("languages/" .$language. ".php","c+");
//Loops through the query and shows the translated terms 
//and english terms if there are no translations for the term
$new_file = "<?php" . "\n "; 
fwrite($lang_file, $new_file);
while($row = mysqli_fetch_array($terms)) {


        if ($row[$language] != "")
            fwrite($lang_file,$row['php_variable']. " = " . '"' . addslashes($row[$language]) . '"' . ";" . "\n ");
        else
            fwrite($lang_file,$row['php_variable']. " = " . '"' . addslashes($row['english_phrase']) . '"' . ";" . "\n ");
    }
    $last_line = "?>";
fwrite($lang_file, $last_line);
fclose($lang_file);
}   
}
else{
// Creating a new file if the file doesn't exist
$sql = "SELECT * FROM hydroserver_translation.translations_by_language";
$terms = mysqli_query($link, $sql);
$lang_file= fopen("languages/" .$language. ".php","c+");
$new_file = "<?php" . "\n "; 
fwrite($lang_file, $new_file);
while($row = mysqli_fetch_array($terms)) {
print_r($row);
        if ($row[$language] != "")
//Provide the translation
            fwrite($lang_file,$row['php_variable']. " = " . '"' . addslashes($row[$language]) . '"' . ";" . "\n ");
//If translation doesn't exist, just use the english phrase
        else
            fwrite($lang_file,$row['php_variable']. " = " . '"' . addslashes($row['english_phrase']) . '"' . ";" . "\n ");
    }
    $last_line = "?>";
fwrite($lang_file, $last_line);
fclose($lang_file);
//New language file succesfully created!!!!
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2022-11-07
    相关资源
    最近更新 更多