Last active
July 30, 2016 00:49
-
-
Save macrozone/f1ab1bf6f205f5bfb7518bac0d2854d5 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
DRAFT for a i18n_service for meteor-mantra-apps, it uses https://github.com/vazco/meteor-universe-i18n/ | |
Made to be used in configs/context.js in mantra: | |
const supportedLocales = [ 'de', 'en', 'fr', 'it' ]; | |
const defaultLocale = 'de'; | |
const i18n = I18nService( | |
{universeI18n, SimpleSchema, FlowRouter}, | |
{supportedLocales, defaultLocale} | |
); | |
export default function () { | |
return { | |
Meteor, | |
FlowRouter, | |
(...) | |
i18n | |
}; | |
} | |
It has the current functions: | |
i18n.t(key, props): translate the given key (caution: only reactive in tracker-komposer) | |
use <i18n.T>-component to translate strings in react components. No need for a container. | |
T is usually injected from context | |
i18n.translateSchema(simpleSchema): adds translation to the simpleSchema | |
**/ | |
import _ from 'lodash'; | |
export default (context, { supportedLocales, defaultLocale = 'en' }) => { | |
const { universeI18n, SimpleSchema, FlowRouter } = context; | |
const supports = (locale) => supportedLocales.indexOf(locale) !== -1; | |
const getFallbackLocale = (locale) => { | |
if (!locale) { | |
return defaultLocale; | |
} else if (supports(locale)) { | |
return locale; | |
} | |
const [ lang ] = locale.split('-'); | |
if (supports(lang)) { | |
return lang; | |
} | |
return defaultLocale; | |
}; | |
const setLocale = (locale) => { | |
universeI18n.setLocale(getFallbackLocale(locale)); | |
}; | |
const getLocale = universeI18n.getLocale; | |
const translator = universeI18n.createReactiveTranslator(); | |
const T = universeI18n.createComponent(translator); | |
const LocaleRoutes = (baseRoutes = FlowRouter) => { | |
const setLocaleByRoute = ({ params: { locale } }) => { | |
if (supports(locale)) { | |
setLocale(locale); | |
} else { | |
FlowRouter.setParams({ locale: getFallbackLocale(locale) }); | |
} | |
}; | |
return baseRoutes.group({ | |
prefix: '/:locale?', | |
triggersEnter: [ setLocaleByRoute ], | |
}); | |
}; | |
const translateSchema = (schema, namespace) => { | |
// translate all the labels | |
const translations = translator(namespace); | |
const translatedDef = {}; | |
const _addSubSchemaTranslations = (parentFieldFullName = null, parentTranslation = {}) => { | |
schema.objectKeys(parentFieldFullName).forEach(field => { | |
const fullFieldName = parentFieldFullName ? parentFieldFullName + '.' + field : field; | |
const fieldTranslation = parentTranslation[field]; | |
const fieldDefinition = schema.getDefinition(fullFieldName); | |
const defaultTransform = (value) => (fieldTranslation && fieldTranslation[value]) || value; | |
let label = null; | |
if (fieldTranslation) { | |
if (_.isString(fieldTranslation)) { | |
label = fieldTranslation; | |
} else { | |
label = fieldTranslation.label; | |
} | |
} | |
// recursivly add subfields as well, but flat | |
if (schema.objectKeys(fullFieldName).length > 0) { | |
_addSubSchemaTranslations(fullFieldName, fieldTranslation); | |
} | |
translatedDef[fullFieldName] = { | |
label: label || namespace + '.' + fullFieldName, | |
uniforms: { | |
transform: defaultTransform, | |
...fieldDefinition.uniforms, // can override default transform | |
}, | |
}; | |
}); | |
}; | |
_addSubSchemaTranslations(null, translations); | |
const translatedScheme = new SimpleSchema([ schema, translatedDef ]); | |
const simpleSchemaMessages = evalSimpleSchemaRegexKeys( | |
universeI18n.getTranslation('simpleSchema') | |
); | |
translatedScheme.messages(simpleSchemaMessages); | |
return translatedScheme; | |
}; | |
return { | |
t: translator, | |
T, | |
translateSchema, | |
getFallbackLocale, | |
setLocale, | |
getLocale, | |
supports, | |
LocaleRoutes, | |
getSupportedLocales: () => (supportedLocales), | |
}; | |
}; | |
// allowes to use SimpleSchema.RegEx.Email keys in the translation file, instead of the actual regex | |
function evalSimpleSchemaRegexKeys(messages) { | |
if (messages.regEx) { | |
const regEx = messages.regEx.map(({ msg, exp }) => { | |
return { msg, exp: exp && exp.split('.').reduce((o, i) => o[i], global) }; | |
}); | |
return { ...messages, regEx }; | |
} | |
return messages; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment