jeresig / i18n-node-2

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
507 stars 79 forks source link

Testing to see if a translation exists #34

Closed cressie176 closed 9 years ago

cressie176 commented 10 years ago

First of all thanks for sharing your hard work on i18n-2. Secondly I'd like to make a change but wanted to check it was something you're interested in first.

We're using the following snippet to localise 'Word document', 'Powerpoint document', etc.

req.i18n.__('fileType.' + attachment.type);                

If someone uploads a file type that has no translation for this will end up displaying 'fileType.xyz', when really we want to display nothing. We could maintain a list of known filetypes but I'd rather test to see if the translation is available.

var key = 'fileType.' + attachment.type;
req.i18n.__e(key) ? req.i18n.__(key) : '';

A further enhancement could be to pass the default value to req.i18n.__ but because of the var args I suspect this would require a new method, e.g.

req.i18n.__d(key, 'default template if key not found', 'arg1', 'arg2');

Not quite sure how this could be made to work with plurals though. Assuming you're happy with the above I'll fork and PR

gjuchault commented 9 years ago

Well you can check if your i18n instance has locales by checking i18n.locales if this is what you want ?

cressie176 commented 9 years ago

No - it was more about how to configure defaults if a particular translation wasn't found. Something a bit similar to Spring's [Message Source]. The project's moved on considerably since posting so I'll close.