Skip to content

Instantly share code, notes, and snippets.

@3dd13
Created April 3, 2014 01:52
Show Gist options
  • Save 3dd13/9946929 to your computer and use it in GitHub Desktop.
Save 3dd13/9946929 to your computer and use it in GitHub Desktop.
Working with i18n in geddy.this example shows how to see chinese translated text in http://localhost:4000/?locale=zh-tw
<!-- app/views/layouts/application.html.ejs -->
<!-- generate a link with to switch locale -->
<ul>
<li><%- linkTo(i18n.getText("nav.links.switch_to_english"), {locale: "en-us"}) %></li>
<li><%- linkTo(i18n.getText("nav.links.switch_to_chinese"), {locale: "zh-tw"}) %></li>
</ul>
// app/controllers/application.js
// decide the locale to be used in view, inside application controller before filter
var Application = function () {
this.before(function() {
this.i18n.setLocale(this.params.locale);
});
};
exports.Application = Application;
// config/locales/en-us.json
{
"main.header.courses": "courses",
}
// config/environment.js
// set default locale when no locale is provided
var config = {
generatedByVersion: '0.12.7',
i18n: {
defaultLocale: 'en-us'
}
}
module.exports = config;
<!-- app/views/main/index.html.ejs -->
<!-- show translated text -->
<h1><%= i18n.getText('main.header.courses') %></h1>
<!-- generate a link with current locale -->
<%- linkTo(i18n.getText("faq.question.when"), {controller: "about", action: "index", anchor: "faq", locale: i18n.getLocale()}) %>
// config/locales/zh-tw.json
{
"main.header.courses": "課程",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment