UNPKG

apeman-react-mixins

Version:
68 lines (55 loc) 1.3 kB
/** * Mixin to handle locale. * @mixin ApLocaleMixin */ 'use strict' import React, {PropTypes as types} from 'react' import apemanlocale from 'apemanlocale' const LOCALE_LOCALE_KEY = '_apLocale' /** @lends ApLocaleMixin */ let ApLocaleMixin = { // -------------------- // Custom // -------------------- $apLocaleMixed: true, /** * Get locales * @returns {object} - Locale data. */ getLocale () { const s = this let locale = s[ LOCALE_LOCALE_KEY ] || s.context[ LOCALE_LOCALE_KEY ] if (!locale) { let msg = "Locale not initialized. You need to call `.registerLocale()` on this component or one of it's parents" throw new Error(msg) } return locale }, /** * Register locale data. * @param {object} locale */ registerLocale (locale) { const s = this s[ LOCALE_LOCALE_KEY ] = apemanlocale.locale(locale) }, // -------------------- // Specs // -------------------- contextTypes: { [LOCALE_LOCALE_KEY]: types.func }, childContextTypes: { [LOCALE_LOCALE_KEY]: types.func }, getChildContext () { const s = this return { [LOCALE_LOCALE_KEY]: s.getLocale() } } // -------------------- // Lifecycle // -------------------- } export default Object.freeze(ApLocaleMixin)