inline-l10n.js | |
---|---|
inline-l10n is a ridiculously simple localization preprocessor
and micro-library for any kind of plain-text file. It is designed
to be simple to use and easy to debug, and works independently of
any specific localization format such as gettext or
To use it, simply surround any localizable strings in your
text file with
When localizing, the string
However, if no translation is found, the output will be:
Alternatively, if you'd prefer to specify your own key names
for locating translations, you can surround the left side of
a string with
Then a translation will be sought for Multi-line StringsNote also that localizable strings can span multiple lines, so this works:
Character EscapingTo keep things simple and debuggable, this library doesn't ever
escape the contents of localizations. If you're writing HTML in
a localizable string and need to write SecurityBecause this library doesn't escape the contents of localizations, it's assumed that all localized strings are trusted. | define(function() {
var L10N_RE = /L10N(?:\:([a-z\-]+))?\[\[\[([\s\S]+?)\]\]\]/g; |
Localizing
will return the string |
var InlineL10n = function InlineL10n(str, l10n) {
return str.replace(L10N_RE, function(match, key, value) {
if (!key)
key = value;
if (key in l10n)
return l10n[key];
return value;
});
return str;
}; |
Scanning For Localizable StringsMost localization code needs to "scrape" its files to find
localizable content.
will return |
InlineL10n.parse = function InlineL10n_parse(str) {
var defaultValues = {};
str.replace(L10N_RE, function(match, key, value) {
if (!key)
key = value;
defaultValues[key] = value;
});
return defaultValues;
};
return InlineL10n;
});
|