I18n Internationalization
For developing the multi-language application, build-in I18n support by egg-i18n plugin
# Default Language
Default en-US
. Assume that we want to switch the default language to Simplified Chinese:
// config/config.default.js |
# Switch Language
Here we have some ways to switch the application's current language (Modified records will set to the cookie locale
), the next request will use the language setting directly.
Priority from high to low:
- query:
/?locale=en-US
- cookie:
locale=zh-TW
- header:
Accept-Language: zh-CN,zh;q=0.5
If want to modify parameter name of query or cookie
// config/config.default.js |
# Writing I18n Multi-language Documents
Configuration of multi-language are independent, stored in config/locale/*.js
- config/locale/ |
Not only take effects in the application directory, but also in the directory of framework or plugin config/locale
Note: It's locale, not locals.
Example:
// config/locale/zh-CN.js |
Or using JSON file:
// config/locale/zh-CN.json |
# Getting Multi-language Texts
Use __
(Alias: gettext
) function to get the multi-language texts under locale directory
Note: __
is two underscores
Take above multi-language configuration as example:
ctx.__('Email') |
If texts contain format function like %s
,%j
, we can call by the way similar to util.format()
// config/locale/zh-CN.js |
Support array, subscript and placeholder, such as
// config/locale/zh-CN.js |
# Use in Controller
class HomeController extends Controller { |
# Use in View
Assume we are using template engine Nunjucks
<li>{{ __('Email') }}: {{ user.email }}</li> |