/yudi

jade i18n module

Primary LanguageJavaScriptOtherNOASSERTION

Yudi

Yudi is a custom jade compiler we use here at Coursera to internationalize our jade templates. This compiler transforms jade source by collecting all strings that can be translated and wrapping them with _t("...") so translations can be injected through the _t function.

Yudi collects strings from 3 sources:

  • all text elements, unless they only contains numbers, symbols, or whitespace
  • select attributes. Only attributes with raw string value whose names appear in the list of attributes passed in as options.attrs are collected
  • any string literal wrapped with _t("...") in any escaped JavaScript code within the jade source

Usage

var Yudi = require('yudi');

var content = 'jade source string....';
var js = jade.compileClient(content, {
  filename: filename,
  compiler: Yudi,
  attrs: [...],
  postCompile: function(compiler) { ... }
});
  • attrs is list of attribute names of html attributes in your jade source you want Yudi to wrap with _t
  • postCompile is a function you can use to execute code after compilation is complete. The compiler instance will be passed into the callback. You can access the list of strings the compiler has collected in compiler.strings. You can also directly access other information about the compiler instance, such as its mixins.

_t function signature

_t(translationKey, [interpolationHash])

  • translationKey: english string used to lookup translations
  • interpolationHash: optional hash of interpolations to their values that only gets passed if the translationKey includes interpolations in the form of #{...} or !{...}
  • returns: translated string

Calls to _t are generated by Yudi automatically in any internationalized jade source

Test

Run tests with the following command:

npm test

Adding a new jade test

  1. Create a test jade file at test/jade/<test>.jade
  2. From the root directory, run ./scripts/generateTestFiles.coffee <test>.jade
  3. You should see a <test>.js and <test>.json. <test>.js contains the compiled js template source with _t injected. <test>.json contains a list of strings Yudi found in the jade source

Naming

Yudi, 玉帝, or the Jade Emperor in Chinese