Extjs 中的国际化无需导出项目?

发布于 2024-11-10 08:36:32 字数 481 浏览 1 评论 0原文

我正在开发一个使用 ExtJS 3.2.1 进行 UI 设计的项目。我想同样实现国际化(i18n)。 (不导出项目) ?

我引用了以下链接:

http://www.sencha.com/learn/Tutorial:Localizing_Ext< /a>
http://extjstutorial.org/extjs/meertalig-i18n-met- extjs-en-codeigniter/

任何人都可以建议一些分步教程/电子书以供进一步参考吗?

谢谢!

I am developing a project using ExtJS 3.2.1 for UI design. I want to implement Internationalization (i18n) in the same. (without exporting the project) ?

I have referred the following links :

http://www.sencha.com/learn/Tutorial:Localizing_Ext
http://extjstutorial.org/extjs/meertalig-i18n-met-extjs-en-codeigniter/

Can any one suggest some step-by-step tutorail/ebook for further reference?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2024-11-17 08:36:32

除了本地化 Ext 的内置字符串之外,根据您的链接,我们还有一个返回本地化字符串的全局函数,并在我们想要显示字符串的任何地方调用它。我们还将用户当前选择的语言存储在变量和 cookie 中。

根据我们的经验,我建议您可能希望将服务器上的本地化字符串存储在数据库中,并通过加载时的 Ajax 调用将它们关闭。这允许您管理这些字符串,而无需部署代码更改。

当用户更改语言时,我们设置语言 cookie 并重新加载整个浏览器窗口。

// global shortcut function for retrieving a localized string
function i18n(key, arrInsertValues) {
    return Local.getLocalizedString(key, Local.languageCode, arrInsertValues);
}

// "Local" is a simple "static" object containing methods and localization strings
Local = {

    // Default locale code - set based on cookie at the bottom of this script
    languageCode: 'en',
    languageCodeDefault: 'en',
    charset: 'utf-8',

    languages: [
        ['en', 'English', 'utf-8'],
        ['ja', '日本語', 'utf-8']
    ],

    getLocalizedString: function(key, languageCode, arrInsertValues) {
        if (!this.localizedStrings[key]) {
            // return empty string if key is undefined
            return '';
        }
        if (!this.localizedStrings[key][languageCode]) {
            // return default language string or empty string if the string for the specified language is undefined
            return this.formatString(this.localizedStrings[key][this.lcDefault] || '', arrInsertValues);
        }
        // give 'em what they asked for
        return (this.formatString(this.localizedStrings[key][languageCode], arrInsertValues));
    },


    // returns a localized string formatted to replace values {0},{1} etc with values from the passed array
    formatString: function(string, arrInsertValues) {
        var formattedString = string;
        if (arrInsertValues && arrInsertValues.constructor.toString().indexOf("Array") != -1) {
            for (var i = 0; i < arrInsertValues.length; i++) {
                formattedString = formattedString.replace('{' + i + '}', arrInsertValues[i]);
            }
        }
        return formattedString;
    },

    localizedStrings: {
        tEN: { en: 'Eng', ja: '英語' },
        tJPN: { en: 'Jpn', ja: '日本語' },            
        tYes: { en: 'Yes', ja: 'はい' },
        tNo: { en: 'No', ja: 'いいえ' },    
        tAnd: { en: 'and', ja: 'と' },
        tOr: { en: 'or', ja: 'or' },    
        tDateFormat : { en: 'Y\/m\/d - g\:iA', ja: 'G\:i - Y年m月d日' },    
        tGoodMorning: { en: 'Good morning, {0}.', ja: '{0}様、おはようございます。' }
    }
}

// this is the first script to run, so we can set default language here based on cookie
var cookie = new Ext.state.CookieProvider();
Local.languageCode = cookie.get('languageCode') ? cookie.get('languageCode') : Local.languageCodeDefault;

In addition to localizing Ext's built-in strings, as per your link, we have a global function that returns a localized string, and call that everywhere we want to display a string. We also store the user's currently selected language in a variable and a cookie.

I would suggest from our experience that you probably want to store the localized strings on your server in a DB and bring them down via an Ajax call on load. This allows you to manage these strings without deploying code changes.

When the user changes the language, we set the language cookie and reload the whole browser window.

// global shortcut function for retrieving a localized string
function i18n(key, arrInsertValues) {
    return Local.getLocalizedString(key, Local.languageCode, arrInsertValues);
}

// "Local" is a simple "static" object containing methods and localization strings
Local = {

    // Default locale code - set based on cookie at the bottom of this script
    languageCode: 'en',
    languageCodeDefault: 'en',
    charset: 'utf-8',

    languages: [
        ['en', 'English', 'utf-8'],
        ['ja', '日本語', 'utf-8']
    ],

    getLocalizedString: function(key, languageCode, arrInsertValues) {
        if (!this.localizedStrings[key]) {
            // return empty string if key is undefined
            return '';
        }
        if (!this.localizedStrings[key][languageCode]) {
            // return default language string or empty string if the string for the specified language is undefined
            return this.formatString(this.localizedStrings[key][this.lcDefault] || '', arrInsertValues);
        }
        // give 'em what they asked for
        return (this.formatString(this.localizedStrings[key][languageCode], arrInsertValues));
    },


    // returns a localized string formatted to replace values {0},{1} etc with values from the passed array
    formatString: function(string, arrInsertValues) {
        var formattedString = string;
        if (arrInsertValues && arrInsertValues.constructor.toString().indexOf("Array") != -1) {
            for (var i = 0; i < arrInsertValues.length; i++) {
                formattedString = formattedString.replace('{' + i + '}', arrInsertValues[i]);
            }
        }
        return formattedString;
    },

    localizedStrings: {
        tEN: { en: 'Eng', ja: '英語' },
        tJPN: { en: 'Jpn', ja: '日本語' },            
        tYes: { en: 'Yes', ja: 'はい' },
        tNo: { en: 'No', ja: 'いいえ' },    
        tAnd: { en: 'and', ja: 'と' },
        tOr: { en: 'or', ja: 'or' },    
        tDateFormat : { en: 'Y\/m\/d - g\:iA', ja: 'G\:i - Y年m月d日' },    
        tGoodMorning: { en: 'Good morning, {0}.', ja: '{0}様、おはようございます。' }
    }
}

// this is the first script to run, so we can set default language here based on cookie
var cookie = new Ext.state.CookieProvider();
Local.languageCode = cookie.get('languageCode') ? cookie.get('languageCode') : Local.languageCodeDefault;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文