【问题标题】:PHP Localization FilesPHP 本地化文件
【发布时间】:2011-10-19 07:12:54
【问题描述】:

我正在为我的 PHP 应用程序实现语言文件。我正在使用常量来定义我想要翻译的每个单词。我在应用程序的根目录中放置了一个文件夹,其中包含 en.php、fr.php、de.php、it.php 等文件。这些翻译用于表单元素和错误消息以各自的语言显示。可以通过单击页面顶部的标志来选择语言。

我不确定的是,包含所有已定义单词的语言文件应该是一个文件还是分解为多个文件,这样我就可以为每个页面加载我需要的内容。我的应用程序变得非常大,加载一个巨大的 PHP 文件似乎是个坏主意。

将单个文件放在一个位置使加载它们变得简单,但我不确定这是否是这样做的方法。

谢谢!

【问题讨论】:

  • 你看过gettext吗?
  • 是的,我对 gettext 很熟悉,但我想控制翻译后的文本,所以我回避外部服务。
  • 不确定 gettext 作为外部服务是什么意思。您可以自己提供翻译文件。

标签: php file localization translation


【解决方案1】:

我的建议显然是使用分而治之的方法,如果您可以实现这样的模块化,即您只包含页面/请求特定的语言文件而不是缩短响应时间

【讨论】:

    【解决方案2】:

    我也同意分​​割它,根据您的需要创建文件。

    由于您需要文本文件,这是一个非常原始的演示示例,带有文本文件。不过我认为使用静态类看起来比简单的常量更有条理。

    ./index.php

    <?php  
    session_start();   
    ///////////////////////////////// LANGUAGE /////////////////////////////////// 
    if(!isset($_SESSION['langid']))/*This will run only once*/
        $_SESSION['langid']="en";//persistent store
    if(isset($_GET['langid'])) 
        $_SESSION['langid']=$_GET['langid'];//persistent store 
    
     /*** define the   path ***/  
    define ('__LANG_PATH',  'C:/wamp/www/lang/lang/'.$_SESSION['langid'].'/');
    
    echo"
    <a href=index.php?langid=en>English</a><br>
    <a href=index.php?langid=fr>French</a><br><br>";
    
    include('history.php');
    ?>
    

    ./history.php

    <?PHP
        include __LANG_PATH.'txtHistory.inc';
        echo txtArticle::$body_title;
        echo '<br>';
        echo txtArticle::$history_article
    ?>
    

    ./lang/en/txtHistory.inc

    <?PHP
     class txtArticle 
    {
        public static $body_title="Welcome to history page..";  
        public static $history_article=
    "Based at RAF Medmenham, a country house on the banks of the 
    Thames, the Central Interpretation Unit (CIU) was established in 
    April 1941. ."; 
    } 
    ?>
    

    ./lang/fr/txtHistory.inc

    <?PHP
     class txtArticle 
    {
        public static $body_title="Bienvenue à la page d'histoire..";   
        public static $history_article=
    "Basé à la RAF Medmenham, une maison de campagne sur les
    leur travail, les opérations de l'OPC et la guerre, qui incluse guider 
    les raids de la Dam Busters, a été largement méconnus jusqu'à présent.
    ";  
    } 
    ?>
    

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多