Last active
August 29, 2015 14:24
-
-
Save codeincontext/15784828067029c27038 to your computer and use it in GitHub Desktop.
Quick and dirty translation helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
Author
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
This is probably slower than the previous revision but it's neater so that counts for something right