MCPcopy
hub / github.com/mashpie/i18n-node / translate

Function translate

i18n.js:950–1067  ·  view source on GitHub ↗
(locale, singular, plural, skipSyncToAllFiles)

Source from the content-addressed store, hash-verified

948 * read locale file, translate a msg and write to fs if new
949 */
950 const translate = (locale, singular, plural, skipSyncToAllFiles) => {
951 // add same key to all translations
952 if (!skipSyncToAllFiles && syncFiles) {
953 syncToAllFiles(singular, plural)
954 }
955
956 if (locale === undefined) {
957 logWarn(
958 'WARN: No locale found - check the context of the call to __(). Using ' +
959 defaultLocale +
960 ' as current locale'
961 )
962 locale = defaultLocale
963 }
964
965 // try to get a fallback
966 if (!locales[locale]) {
967 locale = getFallback(locale, fallbacks) || locale
968 }
969
970 // attempt to read when defined as valid locale
971 if (!locales[locale]) {
972 read(locale)
973 }
974
975 // fallback to default when missed
976 if (!locales[locale]) {
977 logWarn(
978 'WARN: Locale ' +
979 locale +
980 " couldn't be read - check the context of the call to $__. Using " +
981 defaultLocale +
982 ' (default) as current locale'
983 )
984
985 locale = defaultLocale
986 read(locale)
987 }
988
989 // dotnotaction add on, @todo: factor out
990 let defaultSingular = singular
991 let defaultPlural = plural
992 if (objectNotation) {
993 let indexOfColon = singular.indexOf(':')
994 // We compare against 0 instead of -1 because
995 // we don't really expect the string to start with ':'.
996 if (indexOfColon > 0) {
997 defaultSingular = singular.substring(indexOfColon + 1)
998 singular = singular.substring(0, indexOfColon)
999 }
1000 if (plural && typeof plural !== 'number') {
1001 indexOfColon = plural.indexOf(':')
1002 if (indexOfColon > 0) {
1003 defaultPlural = plural.substring(indexOfColon + 1)
1004 plural = plural.substring(0, indexOfColon)
1005 }
1006 }
1007 }

Callers 2

i18nFunction · 0.85
syncToAllFilesFunction · 0.85

Calls 8

syncToAllFilesFunction · 0.85
logWarnFunction · 0.85
getFallbackFunction · 0.85
readFunction · 0.85
localeAccessorFunction · 0.85
localeMutatorFunction · 0.85
logDebugFunction · 0.85
writeFunction · 0.85

Tested by

no test coverage detected