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
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 incompiler.strings
. You can also directly access other information about the compiler instance, such as its mixins.
_t(translationKey, [interpolationHash])
translationKey
: english string used to lookup translationsinterpolationHash
: optional hash of interpolations to their values that only gets passed if thetranslationKey
includes interpolations in the form of#{...}
or!{...}
- returns: translated string
Calls to _t
are generated by Yudi automatically in any internationalized jade source
Run tests with the following command:
npm test
- Create a test jade file at
test/jade/<test>.jade
- From the root directory, run
./scripts/generateTestFiles.coffee <test>.jade
- 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
Yudi, 玉帝, or the Jade Emperor in Chinese