Skip to content

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.

src/config/locales/models/user/en.yml
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.

src/sample.ts
import { User } from "./models/index.js";
// The translation for accelrecord.models.User will be used
console.log(User.modelName.human); // => "User"
// The translation for accelrecord.attributes.User.firstName will be used
console.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]
src/models/user.ts
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.blank
accelrecord.errors.models.User.blank
accelrecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank
src/sample.ts
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:

ValidationOptionMessage KeyInterpolation
acceptance-accepted-
presence-blank-
lengthminimumtooShortcount
lengthmaximumtooLongcount
uniqueness-taken-
format-invalid-
inclusion-inclusion-
numericalityequalToequalTocount

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.

prisma/schema.prisma
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.

src/sample.ts
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.MEMBER
enums.defaults.Role.MEMBER
enums.Role.MEMBER