2

翻译提示 1

我遇到了一个问题,我需要为其他国家的客户翻译我的应用程序。我在应用程序中的所有自定义字符串都没有包含在 Ext JS 的语言环境文件中,所以我必须想出自己的翻译方式。

4

3 回答 3

1

You might want to consider using a gettext javascript plugin for the frontend, such as Jed. http://slexaxton.github.io/Jed/. That would mean you can keep using gettext dictionaries for your entire app.

I ended up creating a core.po dictionary, which contains all the backend strings, and a frontend.po dictionary which is dedicated to the frontend.

You can generate an output that is suitable for jed plugin by using a simple conteoller. I open sourced some sections of the work I did while implementing it. see https://github.com/aporat/zend-translate-skelton/blob/master/library/TranslateGettext/BackendProxyController.php

The controller generates a Jed gettext dictionary, and this javascript is included in the layout.phtml

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

Controller:

<?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;
    }

}
于 2013-04-22T14:15:30.643 回答
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 可以处理文件以建议需要翻译的字符串。

于 2013-04-15T14:18:44.083 回答
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. IE:

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”,然后将所有字符串更改为法语。

注意:不要忘记在<header>您的网络文件中包含您的语言文件。您可以通过在 PHP 中使用您想要显示的语言(我在 ZF application.ini 文件中设置我的)来动态更改此设置,然后在您的标题中您可以使用以下行:

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

翻译提示 2

如果您需要将所有 Ext JS 组件翻译成英语以外的语言,您可以在header. 例如,如果你想要法语: <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>

于 2013-04-15T11:12:31.990 回答