使用PHP扩展JS翻译(Zend Framework)

时间:2013-04-15 11:12:32

标签: php zend-framework extjs extjs4 translation

翻译提示1

我遇到了一个问题,我需要让我的应用程序可以翻译给其他国家的客户。应用程序中的所有自定义字符串都没有被Ext JS的语言环境文件覆盖,所以我不得不想出自己的翻译方式。

3 个答案:

答案 0 :(得分:1)

在我的应用程序中,我使用mo / po文件来处理服务器端的翻译。由于我想将所有语言字符串保存在一个中心位置(我的.po文件),因此我使用了“Language.js”文件而不是“English.js”和“French.js”。该文件的内容如下所示:

window.applanguage = {
    /*General form elements*/
    login : <?=$this->translate("Login")?>,
    OK: <?=$this->translate("OK")?>,
    changepassword: <?=$this->translate("Change password")?>,
    currentpassword: <?=$this->translate("Current password")?>,
    sam: <?=$this->translate("System Access Manager")?>,
    userid: <?=$this->translate("User ID")?>,
    adminid: <?=$this->translate("Admin ID")?>,
    email: <?=$this->translate("Email")?>,
    password: <?=$this->translate("Password")?>,
    newpassword: <?=$this->translate("New password")?>,
    confirmpassword: <?=$this->translate("Confirm password")?>,
    confirm: <?=$this->translate("Confirm")?>,
    confirmation: <?=$this->translate("Confirmation")?>,
    wentwrong: <?=$this->translate("Something went wrong")?>,
    username: <?=$this->translate("Username")?>,
    passvalidity: <?=$this->translate("Password Validity (days)")?>,
    product: <?=$this->translate("Product")?>,
    accesslevel: <?=$this->translate("Access Level")?>,
    timeoutmins: <?=$this->translate("Timeout (mins)")?>,
    cancel: <?=$this->translate("Cancel")?>,
    save: <?=$this->translate("Save")?>,
    reset: <?=$this->translate("Reset")?>,
    passwordutility: <?=$this->translate("Change password utility")?>,
    expireform: <?=$this->translate("Session expired, please log in to continue.")?>,
    adduser: <?=$this->translate("Add user")?>,
    edituser: <?=$this->translate("Edit user")?>,
    removeuser: <?=$this->translate("Remove user")?>,
    resetuser: <?=$this->translate("Reset user")?>,
    add: <?=$this->translate("Add")?>
};

这样,我将所有翻译保存在同一个地方,并且poedit可以处理文件以建议需要翻译的字符串。

答案 1 :(得分:1)

您可能需要考虑为前端使用gettext javascript插件,例如Jed。 http://slexaxton.github.io/Jed/。这意味着您可以继续为整个应用程序使用gettext词典。

我最终创建了一个core.po字典,其中包含所有后端字符串,以及一个专门用于前端的frontend.po字典。

您可以使用简单的conteoller生成适合jed插件的输出。我开源了我在实现它时所做的一些工作。见https://github.com/aporat/zend-translate-skelton/blob/master/library/TranslateGettext/BackendProxyController.php

控制器生成一个Jed gettext字典,这个javascript包含在layout.phtml

<script src="/language?language=en"></script>

控制器:

<?php

  class TranslateGettext_BackendProxyController extends Zend_Controller_Action
  {

    /**
     * See http://slexaxton.github.com/Jed/
     * This is a json proxy for the frontend i18n
     */
    public function indexAction()
    {

        $localeCode = $this->_getParam('locale');
        $locale = new Zend_Locale($localeCode);

        $translate = Zend_Registry::get('Zend_Translate');

        if ($translate->isAvailable($locale->getLanguage())) {
            $entries = Gettext\Extractors\Po::extract(APPLICATION_PATH . '/../languages/' . $locale->getLanguage() . '/LC_MESSAGES/frontend.po');
        } else {
            $entries = Gettext\Extractors\Po::extract(APPLICATION_PATH . '/../languages/en/LC_MESSAGES/frontend.po');
        }

        echo 'var i18n = new Jed({locale_data : ';
        echo Gettext\Generators\Jed::generate($entries, true);
        echo '});';

        exit;
    }

}

答案 2 :(得分:0)

我决定在名为“resources”的文件夹中创建一个“English.js”文件(尽管你可以称之为“locale”或任何你想要的)。在这个文件中,我创建了一个对象,其中包含我需要翻译的所有自定义字符串,看起来像下面的内容:

window.applanguage = {
    /*General form elements*/
    login : "Login",
    OK: "OK",
    changepassword: "Change password",
    currentpassword: "Current password",
    sam: "System Access Manager",
    userid: "User ID",
    adminid: "Admin ID",
    email: "Email",
    password: "Password",
    newpassword: "New password",
    confirmpassword: "Confirm password",
    confirm: "Confirm",
    confirmation: "Confirmation",
    wentwrong: "Something went wrong",
    username: "Username",
    passvalidity: "Password Validity (days)",
    product: "Product",
    accesslevel: "Access Level",
    timeoutmins: "Timeout (mins)",
    cancel: "Cancel",
    save: "Save",
    reset: "Reset",
    passwordutility: "Change password utility",
    expireform: "Session expired, please log in to continue.",
    adduser: "Add user",
    edituser: "Edit user",
    removeuser: "Remove user",
    resetuser: "Reset user",
    add: "Add"
};

无论我需要翻译自定义字符串,我只需将其替换为window.applanguage.string_to_translate。即:

Ext.Msg.show({
    closable: false,
    title: window.applanguage.info,
    msg: window.applanguage.selectuserfirst,
    buttons: Ext.Msg.OK,
    icon: Ext.Msg.INFO
});

现在,如果你想用法语申请(尽管很繁琐)你可以复制“English.js”文件,名称是“French,js”,并将所有字符串改为法语。

NB :不要忘记将您的语言文件包含在网络文件的<header>中。你可以通过PHP中的全局(我在ZF application.ini文件中设置)使用你想要显示的语言来动态地改变它,然后在你的标题中你就可以拥有这一行:

<script type="text/javascript" src="../extjs/resources/<?php echo $languageFile; ?>.js"></script>

翻译提示2

如果您需要将所有Ext JS组件翻译成英语以外的语言,您可以在header中包含EXT JS语言环境文件。例如,如果你想要法语: <script type="text/javascript" src="../ext-4.2.0/locale/ext-lang-fr.js"></script>

使用动态PHP选项,它看起来像这样(这适用于ZF,但您可以在应用程序中使用全局变量):

// get params from application.ini
$config = new Zend_Config_Ini('../application/configs/application.ini', 'development');
$langFile = $config->tranlation->language->file;
$extLang = null;
switch($langFile){
    case 'English':
        $extLang= "ext-lang-en_GB";
        break;
    case 'French':
        $extLang= "ext-lang-fr";
        break;
    case 'Spanish':
        $extLang= "ext-lang-es";
        break;
    case 'German':
        $extLang= "ext-lang-de";
        break;
    case 'Chinese (Simplified)':
        $extLang= "ext-lang-zh_CN";
        break;
    case 'Chinese (Traditional)':
        $extLang= "ext-lang-zh_TW";
        break;
}

然后在你的<header>地方: <script type="text/javascript" src="../ext-4.2.0/locale/<?php echo $extLang; ?>.js"></script>