Internationalization

Get Language

Use the method http.lang to get the language of current user from cookie or header. eg.

let lang = http.lang();

If you want to support getting the user selected language from cookie, you need to set the language name in the cookie. You can modify it in the config file src/common/config/locale.js. eg.

export default {
  cookie_name: 'think_locale', // the cookie name to store language
  default: 'en' // default language
};

Use the method this.lang to get the corresponding language directly in Controller.

Parse the Language from URL

In some cases, the language is parsed from the URL. eg. the url of current page is https://www.thinkjs.org/zh-cn/doc/2.0/i18n.html, which contains the language zh-cn.

In this case, you need to use middleware to parse the language in your project. eg.

think.middleware('get_lang', http => {
  let supportLangs = think.config('locale.support');
  let lang = http.pathname.split('/')[0]; // get the language from URL

  if(supportLangs.indexOf(lang) > -1){
    http.pathname = http.pathname.substr(lang.length + 1);
  }else{
    lang = http.lang(); // get the language from cookie or header
    if(supportLangs.indexOf(lang) === -1){
      lang = http.config('locale.default'); //default supported language
    }
  }
  http.lang(lang, true); //set the language, and allow to add language directory into the template path
});

After parsing the language from URL, you could use method http.lang to set the language. And later, you can directly use http.lang to get the language in Controller.

After defining middleware get_lang, add it into the corresponding hook. eg.

export default {
  route_parse: ['prepend', 'get_lang'], //add get_lang prepend into route_parse hook
}

Language Variable Config

Projects that support international need to config the different variable values in different languages. The config file is located in src/common/config/locale/[lang].js, format is as follows.

// src/common/config/locale/zh-cn.js
export default {
  'title-home': 'ThinkJS Official Website - A Node.js MVC Framework Support All Of ES6/7 Features',
  'title-changelog': 'Update logs - ThinkJS Official Website',
}
// src/common/config/locale/en.js
export default {
  'title-home': 'ThinkJS - A Node.js MVC Framework Support All Of ES6/7 Features',
  'title-changelog': 'Changelog - ThinkJS'
}

Get the Language Variable

After config the language variable, we can get the value of current language by http.locale method. eg.

let homeTitle = http.locale('title-home');

If in Controller, we can get it directly by this.locale method. eg.

export default class extends think.controller.base {
  indexAction(){
    let homeTitle = this.locale('title-home');
  }
}

Use the Language Variable in Template

In template, use the function _ to get the value of corresponding language. The following is ejs template as an example.

<%- _('title-home') %>

Set the Template Language Path

In some projects, we need to customize different templates depending on the different languages. By this time, it is appropriate that adding a layer of language directory to the template path. eg. view/zh-cn/home/index_index.html , adds a layer of language directory zh-cn to the path.

Use the method http.lang to set language and add a layer of language directory in the template path. eg.

http.lang(lang, true); // true indicates that you can add a layer of language directory in the template path

In Controller, use the method this.lang to set. eg.

export default class extends think.controller.base {
  indexAction(){
    let lang = getFromUrl();
    this.lang(lang, true);
    ...
  }
}