Skip to content
Snippets Groups Projects
Commit 12067371 authored by Mathieu Massaviol's avatar Mathieu Massaviol
Browse files

Refactor translateValue (by @mda) #29

parent 12de073e
No related branches found
No related tags found
1 merge request!75Resolve "Permettre la recherche d'un jeu de données"
import type { LocalizedProperty } from '@/declarations' import type { LocalizedProperty } from '@/declarations'
import { useI18n, type I18nScope } from 'vue-i18n' import { useI18n, type I18nScope } from 'vue-i18n'
const CACHE_TRANSLATE_KEYVALUE = []
export function useTranslateValue(useScope: I18nScope = 'global') { export function useTranslateValue(useScope: I18nScope = 'global') {
const { t, mergeLocaleMessage } = useI18n({ const { t, mergeLocaleMessage } = useI18n({
useScope useScope
...@@ -13,26 +15,40 @@ export function useTranslateValue(useScope: I18nScope = 'global') { ...@@ -13,26 +15,40 @@ export function useTranslateValue(useScope: I18nScope = 'global') {
return localizedProperty return localizedProperty
} }
// For each localized property of an external resource, let key: string
// we add each localized value as a message in i18n.
// We take the first found value as the message key, /**
// this way, if we can't fallback to any locale, at least we * First time we find this localizedProperty
// print a real message. *
const keyValue = Object.values(localizedProperty)[0] * We init it
*/
// when we have severeal values for one locale, we arbitrary const lpCached = CACHE_TRANSLATE_KEYVALUE.find(e => e.lp === localizedProperty)
// take the first one. if (!lpCached) {
const key: string = typeof keyValue === 'string' ? keyValue : keyValue[0] // For each localized property of an external resource,
// we add each localized value as a message in i18n.
Object.keys(localizedProperty).forEach(function (locale) { // We take the first found value as the message key,
if (Array.isArray(localizedProperty[locale])) { // this way, if we can't fallback to any locale, at least we
// when we have severeal values for one locale, we arbitrary // print a real message.
// take the first one. const keyValue = Object.values(localizedProperty)[0]
mergeLocaleMessage(locale, { [key]: localizedProperty[locale][0] })
} else { // when we have severeal values for one locale, we arbitrary
mergeLocaleMessage(locale, { [key]: localizedProperty[locale] }) // take the first one.
} key = typeof keyValue === 'string' ? keyValue : keyValue[0]
});
Object.keys(localizedProperty).forEach(function (locale) {
if (Array.isArray(localizedProperty[locale])) {
// when we have severeal values for one locale, we arbitrary
// take the first one.
mergeLocaleMessage(locale, { [key]: localizedProperty[locale][0] })
} else {
mergeLocaleMessage(locale, { [key]: localizedProperty[locale] })
}
});
CACHE_TRANSLATE_KEYVALUE.push({ keyValue, lp: localizedProperty })
} else {
const keyValue = lpCached.keyValue
key = typeof keyValue === 'string' ? keyValue : keyValue[0]
}
return t(key) return t(key)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment