Internationalization (i18n) - Serialization #3325
Unanswered
jrrmcalcio
asked this question in
Help
Replies: 3 comments
-
Can you please share some code. How are you running the query, the model file. Its hard to provide solutions without looking at the code |
Beta Was this translation helpful? Give feedback.
0 replies
-
Ohhh my... that's right... sorry, ok, here we go: I have a controller that shows a post:
And then in the Post model I want to localize the posted_at field, for example "1 hour ago", "1 hora atras", etc.
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Custom base model import { I18n } from '@adonisjs/i18n';
import { BaseModel as LucidBaseModel } from "@adonisjs/lucid/orm";
import { CherryPick, ModelObject } from "@adonisjs/lucid/types/model";
export type SerializationContext = {
i18n?: I18n;
};
export type SerializationOptions = {
i18n?: I18n;
relations?: {
[relation: string]: SerializationOptions;
};
} & CherryPick;
export class BaseModel extends LucidBaseModel {
private static serializationContexts: SerializationContext[] = [];
serialize(options?: SerializationOptions | undefined): ModelObject {
const initialLevel = BaseModel.serializationContexts.length;
BaseModel.serializationContexts.push({
...BaseModel.serializationContext(),
i18n: options?.i18n
});
try {
return super.serialize(options);
} finally {
BaseModel.serializationContexts.pop();
if (BaseModel.serializationContexts.length != initialLevel)
throw new Error("Model serialization is out of sync!");
}
}
static serializationContext(): SerializationContext {
return BaseModel.serializationContexts[BaseModel.serializationContexts.length - 1] ?? {};
}
static serializationLocale(): string | undefined {
return BaseModel.serializationContext()?.i18n?.locale;
}
}; Any other model import { column } from "@adonisjs/lucid/orm";
import { BaseModel, SerializationPresets } from "#models/BaseModel"; // custom base model
export class City extends BaseModel {
@column({ isPrimary: true })
declare id: number;
@column()
declare code: string;
@column()
declare countryId: number;
@column()
declare districtId: number;
@column()
declare regionId: number;
@column({
serialize: (_, __, model) => {
return (model as City).getLocalizedName(BaseModel.serializationLocale());
}
})
declare name: string;
@column({ serialize: (value) => JSON.parse(value) })
declare i18n: string;
getLocalizedName(lang?: string): string {
if (!lang)
return this.name;
const i18n = JSON.parse(this.i18n);
return i18n[lang] ?? this.name;
}
}; Usage city.serialize({ i18n: ctx.i18n })) This works fine because serialization is sync. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello how can I access the user defined language in my serializations? for example I would like to format the dates according to the user defined language. I see we can use the HttpContext.get to get the current context and use the i18n instance from there, but first, not sure if it would work and second it require to activate the node ALS. Is there any other way instead of using the ALS?
Thanks,
Beta Was this translation helpful? Give feedback.
All reactions