Internationalization (i18n)
Accella supports internationalization using i18next.
By placing translation files under the src/config/locales/
directory, you can translate model names, attribute names, error messages, and Enum values.
Translation files can be written in YAML, JSON, or JavaScript format as shown in the examples below.
en: translation: accelrecord: models: User: "User" attributes: User: firstName: "First Name" lastName: "Last Name"
{ "en": { "translation": { "accelrecord": { "models": { "User": "User" }, "attributes": { "User": { "firstName": "First Name", "lastName": "Last Name" } } } } }}
export default { en: { translation: { accelrecord: { models: { User: "User", }, attributes: { User: { firstName: "First Name", lastName: "Last Name", }, }, }, }, },};
To change the default language or other i18next settings, edit the initialization process in src/config/initializers/i18n.ts
.
Translation of Models
You can reference the translations for model names and attribute names using the Model.modelName.human
method and the Model.humanAttributeName(attribute)
method.
import { User } from "./models/index.js";
// The translation for accelrecord.models.User will be usedconsole.log(User.modelName.human); // => "User"// The translation for accelrecord.attributes.User.firstName will be usedconsole.log(User.humanAttributeName("firstName")); // => "First Name"
Translation of Error Messages
Validation error messages are also translatable and can be referenced using the following keys:
accelrecord.errors.models.[ModelName].attributes.[attribute].[messageKey]accelrecord.errors.models.[ModelName].[messageKey]accelrecord.errors.messages.[messageKey]errors.attributes.[attribute].[messageKey]errors.messages.[messageKey]
import { validates } from "accel-record/validations";import { ApplicationRecord } from "./applicationRecord.js";
class UserModel extends ApplicationRecord { static validations = validates(this, [["firstName", { presence: true }]]);}
In the example above, the translation of the message key 'blank'
will be used for the error message.
In this example, the following keys will be searched in order, and the first key found will be used:
accelrecord.errors.models.User.attributes.name.blankaccelrecord.errors.models.User.blankaccelrecord.errors.messages.blankerrors.attributes.name.blankerrors.messages.blank
import { User } from "./models/index.js";
const user = User.build({});user.validate();console.log(User.errors.fullMessages);// => ["First Name can't be blank"]
The message keys corresponding to each validation are as follows:
Validation | Option | Message Key | Interpolation |
---|---|---|---|
acceptance | - | accepted | - |
presence | - | blank | - |
length | minimum | tooShort | count |
length | maximum | tooLong | count |
uniqueness | - | taken | - |
format | - | invalid | - |
inclusion | - | inclusion | - |
numericality | equalTo | equalTo | count |
For those with interpolation set to count
, that part will be replaced with the value specified in the option when the error message contains {{count}}
.
Translation of Enums
You can define translations for each value of an Enum.
enum Role { MEMBER ADMIN}
model User { /* ... */ role Role @default(MEMBER)}
You can use User.role.options()
to retrieve the translations corresponding to each value of the Enum.
For each User
with a role
, you can retrieve the translation corresponding to the Enum value using the roleText
property.
import { User } from "./models/index.js";
User.role.options(); // => [["Member", "MEMBER"], ["Admin", "ADMIN"]]
const user = User.build({});user.role; // => "MEMBER"user.roleText; // => "Member"
In the example of user.role
, the following keys will be searched in order, and the first key found will be used:
enums.User.Role.MEMBERenums.defaults.Role.MEMBERenums.Role.MEMBER