|
| 1 | +/* |
| 2 | + The MIT License |
| 3 | + |
| 4 | + Copyright (c) 2017-2019 EclipseSource Munich |
| 5 | + https://github.com/eclipsesource/jsonforms |
| 6 | + |
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | + |
| 14 | + The above copyright notice and this permission notice shall be included in |
| 15 | + all copies or substantial portions of the Software. |
| 16 | + |
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + THE SOFTWARE. |
| 24 | +*/ |
| 25 | + |
| 26 | +import get from 'lodash/get'; |
| 27 | +import { |
| 28 | + ErrorTranslator, |
| 29 | + JsonFormsI18nState, |
| 30 | + Translator, |
| 31 | + JsonFormsState, |
| 32 | +} from '../store'; |
| 33 | +import { defaultErrorTranslator, defaultTranslator } from './i18nUtil'; |
| 34 | + |
| 35 | +export const fetchLocale = (state?: JsonFormsI18nState) => { |
| 36 | + if (state === undefined) { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + return state.locale; |
| 40 | +}; |
| 41 | + |
| 42 | +export const fetchTranslator = (state?: JsonFormsI18nState) => { |
| 43 | + if (state === undefined) { |
| 44 | + return defaultTranslator; |
| 45 | + } |
| 46 | + return state.translate; |
| 47 | +}; |
| 48 | + |
| 49 | +export const fetchErrorTranslator = (state?: JsonFormsI18nState) => { |
| 50 | + if (state === undefined) { |
| 51 | + return defaultErrorTranslator; |
| 52 | + } |
| 53 | + return state.translateError; |
| 54 | +}; |
| 55 | + |
| 56 | +export const getLocale = (state: JsonFormsState) => |
| 57 | + fetchLocale(get(state, 'jsonforms.i18n')); |
| 58 | + |
| 59 | +export const getTranslator = |
| 60 | + () => |
| 61 | + (state: JsonFormsState): Translator => |
| 62 | + fetchTranslator(get(state, 'jsonforms.i18n')); |
| 63 | + |
| 64 | +export const getErrorTranslator = |
| 65 | + () => |
| 66 | + (state: JsonFormsState): ErrorTranslator => |
| 67 | + fetchErrorTranslator(get(state, 'jsonforms.i18n')); |
0 commit comments