Skip to content

Instantly share code, notes, and snippets.

@codeincontext
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save codeincontext/15784828067029c27038 to your computer and use it in GitHub Desktop.

Select an option

Save codeincontext/15784828067029c27038 to your computer and use it in GitHub Desktop.
Quick and dirty translation helper
currentLocale = 'en';
locales = {
en: {
'common.between': 'less than __ but more than __'
}
};
t = function(key, ...insertions) {
let translatedString = locales[currentLocale][key];
return translatedString.replace(/__/g, () => insertions.shift());
}
t('common.between', 5, 10); //= 'less than 5 but more than 10'
@codeincontext
Copy link
Author

This is probably slower than the previous revision but it's neater so that counts for something right

@codeincontext
Copy link
Author

Things this doesn't do:

  • Pluralisation
  • Ordered insertions
  • default language on missing keys
  • logging missing keys
  • exporting all used keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment