refactor TS i18NField

This commit is contained in:
Jérémy Munsch 2024-01-31 16:59:54 +01:00
parent 0688f25474
commit f93cabb03a
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
9 changed files with 18 additions and 11 deletions

View file

@ -41,12 +41,13 @@ export default class ToolboxHelper {
return t(text, params);
}
public static getTranslatableField(model: TypeBrand | TypeRange | TypeProduct, field: "name" | "slug" | "description"): string {
public static getTranslatableField(model: Typei18nModel, field: Typei18nFieldList): string {
const { locale, fallbackLocale } = useAppStore().getI18n();
const i18nField: Typei18nField | null = (model[field as keyof typeof model] ?? null) as Typei18nField | null;
const i18nField: Typei18nField | null = (model[field as keyof typeof model] !== undefined ?
model[field as keyof typeof model] : null) as Typei18nField | null;
if (!i18nField) {
throw new Error(`getTranslatableField ${field} is not part of ${model}`);
return "undefined";
}
return i18nField[locale] !== undefined ? i18nField[locale] : i18nField[fallbackLocale];

View file

@ -77,7 +77,7 @@ export default defineComponent({
t(text: string, params: Record<string,string> = {}, localeToUse: string|null = null): string {
return ToolboxHelper.trans(text, params, localeToUse);
},
transField(model: TypeBrand | TypeRange | TypeProduct, field: "name" | "slug" | "description") {
transField(model: Typei18nModel, field: Typei18nFieldList) {
return ToolboxHelper.getTranslatableField(model, field);
},
refreshData() {

View file

@ -123,7 +123,7 @@ export default defineComponent({
t(text: string, params: Record<string,string> = {}, localeToUse: string|null = null): string {
return ToolboxHelper.trans(text, params, localeToUse);
},
transField(model: TypeBrand | TypeRange | TypeProduct, field: "name" | "slug" | "description") {
transField(model: Typei18nModel, field: Typei18nFieldList) {
return ToolboxHelper.getTranslatableField(model, field);
},
rangeFont(fontId: number) {

View file

@ -132,7 +132,7 @@ export default defineComponent({
t(text: string, params: Record<string,string> = {}, localeToUse: string|null = null): string {
return ToolboxHelper.trans(text, params, localeToUse);
},
transField(model: TypeBrand | TypeRange | TypeProduct, field: "name" | "slug" | "description") {
transField(model: Typei18nModel, field: Typei18nFieldList) {
return ToolboxHelper.getTranslatableField(model, field);
},
getContrastYIQ(hexcolor: string): string {

View file

@ -158,7 +158,7 @@ export default defineComponent({
t(text: string, params: Record<string,string> = {}, localeToUse: string|null = null): string {
return ToolboxHelper.trans(text, params, localeToUse);
},
transField(model: TypeBrand | TypeRange | TypeProduct, field: "name" | "slug" | "description") {
transField(model: Typei18nModel, field: Typei18nFieldList) {
return ToolboxHelper.getTranslatableField(model, field);
},
switchLocale(localeToSwitch:LocaleParams) {

8
index.d.ts vendored
View file

@ -6,7 +6,11 @@ export { };
// * --- MODELS From JSON Schema
type Locale = string;
type i18nField = Record<Locale, string>
type i18nFieldList =
"name" | "slug" | "description" |
"sub_title" | "footer_phrase";
type i18nField = Record<string, string>;
type i18nModel = SettingsModel | BrandModel | RangeModel | ProductModel;
/** Showroom Bo json data format */
type AppModel = {
@ -164,6 +168,8 @@ declare global {
type TypeFont = FontModel;
type Typei18nField = i18nField;
type Typei18nFieldList = i18nFieldList;
type Typei18nModel = i18nModel;
type ToastTypeEnum = ToastType;

View file

@ -117,7 +117,7 @@ export default defineComponent({
t(text: string, params: Record<string,string> = {}, localeToUse: string|null = null): string {
return ToolboxHelper.trans(text, params, localeToUse);
},
transField(model: TypeBrand | TypeRange | TypeProduct, field: "name" | "slug" | "description") {
transField(model: Typei18nModel, field: Typei18nFieldList) {
return ToolboxHelper.getTranslatableField(model, field);
},
titleAnimations(rangeIndex: number) {

View file

@ -120,7 +120,7 @@ export default defineComponent({
t(text: string, params: Record<string,string> = {}, localeToUse: string|null = null): string {
return ToolboxHelper.trans(text, params, localeToUse);
},
transField(model: TypeBrand | TypeRange | TypeProduct, field: "name" | "slug" | "description") {
transField(model: Typei18nModel, field: Typei18nFieldList) {
return ToolboxHelper.getTranslatableField(model, field);
},
setBrandToAnimate(): void {

View file

@ -125,7 +125,7 @@ export default defineComponent({
t(text: string, params: Record<string, string> = {}, localeToUse: string | null = null): string {
return ToolboxHelper.trans(text, params, localeToUse);
},
transField(model: TypeBrand | TypeRange | TypeProduct, field: "name" | "slug" | "description") {
transField(model: Typei18nModel, field: Typei18nFieldList) {
return ToolboxHelper.getTranslatableField(model, field);
},
getContrastYIQ(hexcolor: string): string {