### Installation and Setup Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md Instructions on how to add AshTrans as a dependency in your `mix.exs` file, configure the formatter, and integrate it with Cldr providers. ```elixir iex> {:ash_trans, "~> 0.1.0"} import_deps: [..., :ash_trans] use Cldr, providers: [AshTrans], locales: ["it", "en"] ``` -------------------------------- ### Ash Resource with Translations Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md An example of a complete Ash.Resource definition that includes the AshTrans extension, defining attributes, actions, and the `translations` block for multi-language support. ```elixir defmodule MyApp.Post do @moduledoc false use Ash.Resource, domain: MyApp.Domain, data_layer: Ash.DataLayer.Ets, extensions: [AshTrans.Resource] attributes do uuid_v7_primary_key :id attribute :title, :string, public?: true attribute :body, :string, public?: true end actions do defaults [:read, :destroy, update: :*, create: :*] end translations do public? true fields [:title, :body] locales [:it] end end ``` -------------------------------- ### Translating Resource Fields Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md Provides examples of using `AshTrans.translate/2` to get all translated fields for a given locale and `AshTrans.translate_field/3` to retrieve a specific translated field. ```elixir post_it = AshTrans.translate(post, :it) %{title: "Titolo", body: "Corpo"} = post_it "Titolo" = AshTrans.translate_field(post, :title, :it) ``` -------------------------------- ### Creating a Resource with Translations Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md Shows how to create a new resource instance with translated fields using an Ash.Changeset. Translations are provided as a nested map structure keyed by locale. ```elixir post = Ash.Changeset.for_create(MyApp.Post, :create, %{ title: "Title", body: "Body", translations: %{ it: %{ title: "Titolo", body: "Corpo" } } }) |> Ash.create!() ``` -------------------------------- ### Post Form Component with Liveview and Translations Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md This Liveview component handles the creation and updating of a Post resource. It utilizes Ash Phoenix Form for form management and Cldr for locale-specific translations, including dynamic fields for titles and bodies based on selected locales. ```elixir defmodule MyAppWeb.Post.FormComponent do @moduledoc false use MyAppWeb, :live_component alias AshPhoenix.Form alias MyApp.Cldr alias MyApp.Post @impl true def render(assigns) do ~H"""
<%= @post.body %>
""" end @impl true def mount(%{"id" => id}, _session, socket) do post = Ash.get!(Post, id) |> Cldr.AshTrans.translate(post) {:ok, socket |> assign(:post, post)} end end ``` -------------------------------- ### Adding Translations to an Ash Resource Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md Demonstrates how to extend an Ash.Resource with AshTrans.Resource to enable translation capabilities. It covers defining translatable fields and locales within the resource's `translations` block. ```elixir use Ash.Resource, extensions: [..., AshTrans.Resource] translations do public? true fields [:name, :description] locales [:it] locales MyApp.Cldr.AshTrans.locale_names() end ``` -------------------------------- ### Configure Cldr with AshTrans Provider Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md This snippet shows how to configure the Cldr module to use the AshTrans provider and define supported locales. This enables Cldr to manage available and current locales for translations. ```elixir defmodule MyApp.Cldr do use Cldr, providers: [AshTrans], locales: ["it", "en"] end ``` -------------------------------- ### AshTrans.Resource Translations Options Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/dsls/DSL:-AshTrans.Resource.md Defines the configuration options for the `translations` resource in the AshTrans DSL. This includes settings for public access, supported locales, and fields to be translated. ```APIDOC AshTrans.Resource.translations: Options: public?: boolean Whether the embedded resource should be public or not. Defaults to false. locales: list(atom) The locales to add to the translations resource. Defaults to []. fields: list(atom) A list of fields to add to the translation fields. Defaults to []. ``` -------------------------------- ### Embedded Translation Resources Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md Defines the structure for embedded resources used by AshTrans to manage translations for specific fields within a resource. This includes `Translations` and `Translations.Fields` modules. ```elixir defmodule MyApp.Post.Translations do use Ash.Resource, data_layer: :embedded attributes do attribute :it, MyApp.Post.Translations.Fields end end defmodule MyApp.Post.Translations.Fields do use Ash.Resource, data_layer: :embedded attributes do attribute :title, :string, public?: true attribute :body, :string, public?: true end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.