Skip to content

Commit

Permalink
i18n lib changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AppElent committed Jan 20, 2025
1 parent be503eb commit bddb28b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 127 deletions.
100 changes: 100 additions & 0 deletions src/config/locales.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LocaleObject } from 'yup';

export const languageOptions = {
en: {
icon: '/assets/flags/flag-uk.svg',
Expand All @@ -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'];
129 changes: 2 additions & 127 deletions src/libs/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;

0 comments on commit bddb28b

Please sign in to comment.