开源软件名称(OpenSource Name):mozilla/node-webmaker-i18n开源软件地址(OpenSource Url):https://github.com/mozilla/node-webmaker-i18n开源编程语言(OpenSource Language):JavaScript 97.8%开源软件介绍(OpenSource Introduction):node-webmaker-i18nWebmaker Localization Components for node.js and the browser. This code is heavily inspired by, and borrows from, Mozilla's i18n-abide project. However, this code has been stripped down to support only those things needed by the Webmaker tools and apps, and is based on JSON instead of PO files, uses a different form of client-side localization, etc. UsageServer-Side with node.jsInstall the node.js module using npm:
ExampleThere is an example Express app in the
Now navigate to http://localhost:8000. You'll see examples of server-side and client-side usage. APIThe module exposes a number of useful functions, including: middlewareThe var i18n = require('webmaker-i18n');
...
app.use(i18n.middleware({
supported_languages: [
'en-US', 'th-TH', 'ru'
],
default_lang: 'en-US',
translation_directory: path.join( __dirname, 'locale' )
})); This will cause the app to look for three locales on startup:
You can change the root locale directory by passing When
Cookie Session locale supportIf have more than one server and want to share user's preferred locale you have to setup The language serving is in this order:
Dynamic MappingsOften one wants to map locale-specific languages to a default. For example, if there are 3 locales specified
for English: var i18n = require('webmaker-i18n');
...
app.use(i18n.middleware({
supported_languages: [
'en-US', 'en-GB', 'en-CA', 'th-TH', 'ru-RU'
],
default_lang: 'en-US',
warnings: true,
translation_directory: path.join( __dirname, 'locale' ),
mappings: {
'en': 'en-US',
'th': 'th-TH',
'ru': 'ru-RU'
}
})); Here 8 languages are identified, 5 locale-based, and 3 defaults with no locale. Using such mappings,
users can request warnings option is set to Global enabling langaugesIf you are using Transifex and want to download and enable all the languages supported in your project, you can accomplish this with the following steps:
You will have to download all the translation files first using:
Now all the languages in your Transifex project will be downloaded to "path_to_save_files", for example your locale directory. Each language will be stored as a locale-Country pair (i.e., en_US). var i18n = require('webmaker-i18n');
app.use( i18n.middleware({
supported_languages: ['*'],
default_lang: 'en-US',
translation_directory: path.join( __dirname, 'locale' ),
mappings: {
'en': 'en-CA'
}
})); Note: If you set ['*'] to the supported_languages option, the language codes will be read from the specified translation directory and supported_languages will be updated with the new list. This assumes you have already downloaded or otherwise created these directories yourself. For example, if you have locale/en_US and locale/fr the list of supported languages will include en-US and fr. localeInfoThe If the request comes in as "en-CA"
Note:
getStringsThe var ru = i18n.getStrings('ru'); stringsRouteThe app.get( "/strings/:lang?", i18n.stringsRoute( "en-US" ) ); gettextThe var someString = i18n.gettext("keyName", "language code or locale code here"); Note: This gettext is the same gettext that you can use in getLocalesThe var locales = i18n.getLocales(); getLanguagesThe var languages = i18n.getLanguages(); getSupportLanguagesThe var languages = i18n.getSupportLanguages(); formatThe // Named Example:
i18n.format("%(salutation)s %(place)s", {salutation: "Hello", place: "World"}, true);
// Positional Example:
i18n.format("%s %s", ["Hello", "World"]); languageFrom, localeFromThe // en-US (language) to en_US (locale)
var enUSlocale = localeFrom('en-US');
// en_US (locale) to en-US language)
var enUSlanguage = languageFrom('en_US'); languageNameForThe var languageName = languageNameFor('en-US');
// The above will return "English (US)"
var languageName = languageNameFor('th-TH');
// The above will return "ไทย" languageEnglishNameThe var languageName = languageEnglishName('en-US');
// The above will return "English (US)"
var languageName = languageEnglishName('th-TH');
// The above will return "Thai" getAllLocaleCodesThe var i18n = require("webmaker-i18n");
var allCodes = i18n.getAllLocaleCodes;
console.log(allCodes);
{
...
...
'ta': { nativeName: 'தமிழ்', englishName: 'Tamil' },
'ta-IN': { nativeName: 'தமிழ்', englishName: 'Tamil' },
te: { nativeName: 'తెలుగు', englishName: 'Telugu' },
'te-IN': { nativeName: 'తెలుగు', englishName: 'Telugu' },
'tg-TJ': { nativeName: 'тоҷикӣ', englishName: 'Tajik' },
'th-TH': { nativeName: 'ภาษาไทย', englishName: 'Thai' },
tl: { nativeName: 'Filipino', englishName: 'Filipino' },
'tl-PH': { nativeName: 'Filipino', englishName: 'Filipino' },
'tl-ST': { nativeName: 'tlhIngan-Hol', englishName: 'Klingon' },
'tr-TR': { nativeName: 'Türkçe', englishName: 'Turkish' },
'tt-RU': { nativeName: 'татарча', englishName: 'Tatar' },
'uk': { nativeName: 'Українська', englishName: 'Ukrainian' },
'uk-UA': { nativeName: 'Українська', englishName: 'Ukrainian' }
...
...
} Client-Side in the browserInstall the browser
The <!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
...
<script src="bower_components/webmaker-i18n/localized.js"></script> AMD Usagerequire(['path/to/localized'], function(localized) {
// Don't do anything until the DOM + localized strings are ready
localized.ready(function(){
var someText = localized.get('some key');
});
}); Forcing Localized on the GlobalIn some cases, it might be desirable to have the Global UsageIf you aren't using an AMD loader like require.js, the object will get added to the global: // Don't do anything until the DOM + localized strings are ready
Localized.ready(function(){
var someText = localized.get('some key');
}); Localized membersThe
function readyCallback() {
// Safe to use localized.get() now...
}
var options = { noCache: true, url: '/localized' }
localized.ready(options, readyCallback);
// NOTE: you could also call it like so:
// localized.ready(function(){...}); with no options.
<html lang="th-TH" dir="ltr">
...
<script>
...
var lang = localized.getCurrentLang();
// lang === 'th-TH'
...
</script>
var momentJSLang = langToMomentJSLang('en-US');
// The above will return "en"
var momentJSLang = langToMomentJSLang('th-TH');
// The above will return "th"
var momentJSLang = langToMomentJSLang('en-CA');
// The above will return "en-ca"
localized.ready(function(){
var localized = localized.get('some string key');
}); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论