From bddb28b312f17b704108fe32f0213002159c6c07 Mon Sep 17 00:00:00 2001 From: AppElent Date: Mon, 20 Jan 2025 20:50:25 +0100 Subject: [PATCH] i18n lib changes --- src/config/locales.tsx | 100 ++++++++++++++++++++++++++++++++ src/libs/i18n/index.ts | 129 +---------------------------------------- 2 files changed, 102 insertions(+), 127 deletions(-) diff --git a/src/config/locales.tsx b/src/config/locales.tsx index 4bb4d2b..55d6b93 100644 --- a/src/config/locales.tsx +++ b/src/config/locales.tsx @@ -1,3 +1,5 @@ +import { LocaleObject } from 'yup'; + export const languageOptions = { en: { icon: '/assets/flags/flag-uk.svg', @@ -17,4 +19,102 @@ export const languageOptions = { }, }; +export const localeSettings: LocaleObject = { + mixed: { + default: 'common:errors.default', + required: ({ path }) => ({ key: 'common:errors.fieldRequired', values: { field: path } }), + defined: ({ path }) => ({ key: 'common:errors.fieldDefined', values: { field: path } }), + notNull: ({ path }) => ({ key: 'common:errors.fieldNotNull', values: { field: path } }), + oneOf: ({ path, values }) => ({ + key: 'common:errors.fieldOneOf', + values: { field: path, values }, + }), + notOneOf: ({ path, values }) => ({ + key: 'common:errors.fieldNotOneOf', + values: { field: path, values }, + }), + }, + string: { + // email: 'field_invalid_email', + // url: 'field_invalid_url', + length: ({ path, length }) => ({ + key: 'common:errors.stringLength', + values: { field: path, length }, + }), + min: ({ min, path }) => ({ + key: 'common:errors.fieldMin', + values: { length: min, field: path }, + }), + max: ({ max, path }) => ({ + key: 'common:errors.fieldMax', + values: { length: max, field: path }, + }), + email: ({ path }) => ({ key: 'common:errors.fieldEmail', values: { field: path } }), + url: ({ path }) => ({ key: 'common:errors.fieldUrl', values: { field: path } }), + uuid: ({ path }) => ({ key: 'common:errors.fieldUuid', values: { field: path } }), + trim: ({ path }) => ({ key: 'common:errors.fieldTrim', values: { field: path } }), + lowercase: ({ path }) => ({ key: 'common:errors.fieldLowercase', values: { field: path } }), + uppercase: ({ path }) => ({ key: 'common:errors.fieldUppercase', values: { field: path } }), + }, + number: { + min: ({ min, path }) => ({ + key: 'common:errors.fieldMinValue', + values: { length: min, field: path }, + }), + max: ({ max, path }) => ({ + key: 'common:errors.fieldMaxValue', + values: { length: max, field: path }, + }), + lessThan: ({ path, less }) => ({ + key: 'common:errors.fieldMinValueLength', + values: { field: path, length: less }, + }), + moreThan: ({ path, more }) => ({ + key: 'common:errors.fieldMaxValueLength', + values: { field: path, length: more }, + }), + positive: ({ path }) => ({ key: 'common:errors.fieldPositive', values: { field: path } }), + negative: ({ path }) => ({ key: 'common:errors.fieldNegative', values: { field: path } }), + integer: ({ path }) => ({ key: 'common:errors.fieldInteger', values: { field: path } }), + }, + date: { + min: ({ min, path }) => ({ + key: 'common:errors.fieldMinDate', + values: { length: min, field: path }, + }), + max: ({ max, path }) => ({ + key: 'common:errors.fieldMaxDate', + values: { length: max, field: path }, + }), + }, + boolean: { + isValue: ({ path, value }) => ({ + key: 'common:errors.fieldBoolean', + values: { field: path, value }, + }), + }, + object: { + noUnknown: ({ path }) => ({ key: 'common:errors.fieldNoUnknown', values: { field: path } }), + }, + array: { + min: ({ min, path }) => ({ + key: 'common:errors.fieldMinArray', + values: { length: min, field: path }, + }), + max: ({ max, path }) => ({ + key: 'common:errors.fieldMaxArray', + values: { length: max, field: path }, + }), + length: ({ length, path }) => ({ + key: 'common:errors.fieldLengthArray', + values: { length, field: path }, + }), + }, + // use functions to generate an error object that includes the value from the schema + // number: { + // min: ({ min }) => ({ key: 'field_too_short', values: { min } }), + // max: ({ max }) => ({ key: 'field_too_big', values: { max } }), + // }, +}; + export const namespaces = ['foodhub']; diff --git a/src/libs/i18n/index.ts b/src/libs/i18n/index.ts index 9122d99..8a836e8 100644 --- a/src/libs/i18n/index.ts +++ b/src/libs/i18n/index.ts @@ -1,10 +1,10 @@ -import { languageOptions, namespaces } from '@/config/locales'; +import { languageOptions, localeSettings, namespaces } from '@/config/locales'; import i18n from 'i18next'; import LanguageDetector from 'i18next-browser-languagedetector'; import Backend from 'i18next-http-backend'; // adding lazy loading for translations, more information here: https://github.com/i18next/i18next-http-backend import { initReactI18next } from 'react-i18next'; import { i18nextPlugin } from 'translation-check'; -import { LocaleObject, setLocale } from 'yup'; +import { setLocale } from 'yup'; const customSaveMissingKeys = ( key: string, @@ -63,134 +63,9 @@ const initI18n = () => { }); }; -const localeSettings: LocaleObject = { - mixed: { - default: 'common:errors.default', - required: ({ path }) => ({ key: 'common:errors.fieldRequired', values: { field: path } }), - defined: ({ path }) => ({ key: 'common:errors.fieldDefined', values: { field: path } }), - notNull: ({ path }) => ({ key: 'common:errors.fieldNotNull', values: { field: path } }), - oneOf: ({ path, values }) => ({ - key: 'common:errors.fieldOneOf', - values: { field: path, values }, - }), - notOneOf: ({ path, values }) => ({ - key: 'common:errors.fieldNotOneOf', - values: { field: path, values }, - }), - }, - string: { - // email: 'field_invalid_email', - // url: 'field_invalid_url', - length: ({ path, length }) => ({ - key: 'common:errors.stringLength', - values: { field: path, length }, - }), - min: ({ min, path }) => ({ - key: 'common:errors.fieldMin', - values: { length: min, field: path }, - }), - max: ({ max, path }) => ({ - key: 'common:errors.fieldMax', - values: { length: max, field: path }, - }), - email: ({ path }) => ({ key: 'common:errors.fieldEmail', values: { field: path } }), - url: ({ path }) => ({ key: 'common:errors.fieldUrl', values: { field: path } }), - uuid: ({ path }) => ({ key: 'common:errors.fieldUuid', values: { field: path } }), - trim: ({ path }) => ({ key: 'common:errors.fieldTrim', values: { field: path } }), - lowercase: ({ path }) => ({ key: 'common:errors.fieldLowercase', values: { field: path } }), - uppercase: ({ path }) => ({ key: 'common:errors.fieldUppercase', values: { field: path } }), - }, - number: { - min: ({ min, path }) => ({ - key: 'common:errors.fieldMinValue', - values: { length: min, field: path }, - }), - max: ({ max, path }) => ({ - key: 'common:errors.fieldMaxValue', - values: { length: max, field: path }, - }), - lessThan: ({ path, less }) => ({ - key: 'common:errors.fieldMinValueLength', - values: { field: path, length: less }, - }), - moreThan: ({ path, more }) => ({ - key: 'common:errors.fieldMaxValueLength', - values: { field: path, length: more }, - }), - positive: ({ path }) => ({ key: 'common:errors.fieldPositive', values: { field: path } }), - negative: ({ path }) => ({ key: 'common:errors.fieldNegative', values: { field: path } }), - integer: ({ path }) => ({ key: 'common:errors.fieldInteger', values: { field: path } }), - }, - date: { - min: ({ min, path }) => ({ - key: 'common:errors.fieldMinDate', - values: { length: min, field: path }, - }), - max: ({ max, path }) => ({ - key: 'common:errors.fieldMaxDate', - values: { length: max, field: path }, - }), - }, - boolean: { - isValue: ({ path, value }) => ({ - key: 'common:errors.fieldBoolean', - values: { field: path, value }, - }), - }, - object: { - noUnknown: ({ path }) => ({ key: 'common:errors.fieldNoUnknown', values: { field: path } }), - }, - array: { - min: ({ min, path }) => ({ - key: 'common:errors.fieldMinArray', - values: { length: min, field: path }, - }), - max: ({ max, path }) => ({ - key: 'common:errors.fieldMaxArray', - values: { length: max, field: path }, - }), - length: ({ length, path }) => ({ - key: 'common:errors.fieldLengthArray', - values: { length, field: path }, - }), - }, - // use functions to generate an error object that includes the value from the schema - // number: { - // min: ({ min }) => ({ key: 'field_too_short', values: { min } }), - // max: ({ max }) => ({ key: 'field_too_big', values: { max } }), - // }, -}; - if (!i18n.isInitialized) { initI18n(); setLocale(localeSettings); } -// i18n -// .use(Backend) // Load translations dynamically if needed -// .use(LanguageDetector) // Detect user language -// .use(initReactI18next) // Bind i18n to React -// .use(i18nextPlugin) // GUI -// .init({ -// fallbackLng: 'en', -// supportedLngs: Object.keys(languageOptions), -// backend: { -// loadPath: '/locales/{{lng}}/{{ns}}.json', // locale files path -// }, -// saveMissing: true, // Enable missing key reporting -// defaultNS: 'common', -// ns: namespaces, -// debug: true, // Optional: Debug missing keys in the console -// interpolation: { -// escapeValue: false, // React already escapes -// }, -// saveMissingTo: 'current', -// missingKeyHandler: (lng, ns, key, fallbackValue) => { -// // Save missing keys and their fallback -// missingKeys[key] = fallbackValue || ''; -// console.warn(`[i18n] Missing key: "${key}"`); -// }, -// returnObjects: true, -// }); - export default initI18n;