Created
July 27, 2012 09:29
-
-
Save youtalk/3187089 to your computer and use it in GitHub Desktop.
Yet another internationalization for Node.js
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
var translate = function (locale, singular, plural) { | |
if (locale === undefined) { | |
if (debug) logger.warn('i18n: no locale found'); | |
locale = defaultLocale; | |
} | |
if (!locales[locale]) { | |
read(locale); | |
} | |
var capitalHead = new RegExp('^[A-Z]'); | |
var upperHead = singular.match(capitalHead); | |
singular = upperHead ? singular[0].toLowerCase() + singular.substring(1) : singular; | |
if (plural) { | |
plural = upperHead ? plural[0].toLowerCase() + plural.substring(1) : plural; | |
if (!locales[locale][singular]) { | |
logger.info('i18n: add %s (%s)', singular, locale); | |
locales[locale][singular] = { | |
'one': singular, | |
'other': plural | |
}; | |
write(locale); | |
} | |
} | |
if (!locales[locale][singular]) { | |
logger.info('i18n: add %s (%s)', singular, locale); | |
locales[locale][singular] = singular; | |
write(locale); | |
} | |
var sentence = locales[locale][singular]; | |
return upperHead ? sentence[0].toUpperCase() + sentence.substring(1) : sentence; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment