AshGettext.Resource (AshGettext v0.1.0)

Copy Markdown View Source

Opts a resource into translated action errors, enum labels and attribute names.

defmodule MyApp.Shop.Order do
  use Ash.Resource,
    domain: MyApp.Shop,
    extensions: [AshGettext.Resource]
end

No attribute is rewritten and no enum is replaced; the attributes do block stays as it is. The extension points each write action's error_handler at the library, and adds one public calculation, <attribute>_label, for each attribute whose type is an AshGettext.Enum (see AshGettext.Resource.Transformers.AddEnumLabels). Load it to get the label on the record: Ash.get!(Order, id, load: [:status_label]).

Attribute names are written by you, never made from the code name:

gettext do
  label :delivery_date, "Delivery date"
end

AshGettext.attribute_label/2 translates that text. An attribute with no label raises. A label for an attribute the resource lacks fails to compile.

Labels loaded on records and errors from actions use the language of the action's actor, else Gettext's current language. No action changes the process language.

What to translate is read from the resource's domain (AshGettext.Domain). A resource whose domain does not configure gettext, or which sets translate_errors? false, keeps its messages exactly as they are.

To avoid writing extensions: on every resource, put it in the base resource macro the app almost certainly already has.

Summary

Functions

gettext(body)

(macro)