### 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"""
<.simple_form for={@form} id="post-form" phx-target={@myself} phx-change="validate" phx-submit="save" > <.input type="select" label={gettext("Language")} field={@form[:locale]} options={locale_options()} /> <.input class={hide_input?(@form[:locale]) && "hidden"} label={gettext("Title")} field={@form[:title]} /> <.inputs_for :let={translations} field={@form[:translations]}> <.inputs_for :let={field} :for={locale <- Cldr.AshTrans.locale_names()} field={translations[locale]} > <.input class={hide_translation_input?(@form[:locale], locale) && "hidden"} label={gettext("Title")} field={field[:title]} /> <.input class={hide_input?(@form[:locale]) && "hidden"} type="textarea" label={gettext("Body")} field={@form[:body]} /> <.inputs_for :let={translations} field={@form[:translations]}> <.inputs_for :let={field} :for={locale <- Cldr.AshTrans.locale_names()} field={translations[locale]} > <.input class={hide_translation_input?(@form[:locale], locale) && "hidden"} type="textarea" label={gettext("Body")} field={field[:body]} />
""" end @impl true def update(%{post: post} = assigns, socket) do {:ok, socket |> assign(assigns) |> assign_form(assigns.live_action, post)} end @impl true def handle_event("validate", %{"post" => params}, socket) do form = Form.validate(socket.assigns.form, params) {:noreply, assign(socket, :form, form)} end def handle_event("save", %{"post" => params}, socket) do save_post(socket, socket.assigns.live_action, params) end defp save_post(socket, :edit, params) do case Form.submit(socket.assigns.form, params: params) do {:ok, post} -> notify_parent({:saved, post}) {:noreply, socket |> put_flash(:info, gettext("Post updated successfully")) |> push_patch(to: socket.assigns.patch, replace: true)} {:error, form} -> {:noreply, assign(socket, :form, form)} end end defp save_post(socket, :new, params) do case Form.submit(socket.assigns.form, params: params) do {:ok, post} -> notify_parent({:saved, post}) {:noreply, socket |> put_flash(:info, gettext("Post created successfully")) |> push_navigate(to: socket.assigns.patch.(post))} {:error, form} -> {:noreply, assign(socket, :form, form)} end end defp assign_form(socket, :new, _) do form = Form.for_create(Post, :create, as: "post", forms: [auto?: true] ) |> AshTrans.add_forms(Cldr.AshTrans.locale_names()) assign(socket, :form, to_form(form)) end defp assign_form(socket, :edit, post) do form = Form.for_update(post, :update, as: "post", forms: [auto?: true] ) |> AshTrans.add_forms(Cldr.AshTrans.locale_names()) assign(socket, :form, to_form(form)) end defp hide_input?(field) do field.value && to_string(field.value) != to_string(default_locale()) end defp hide_translation_input?(field, locale) do !field.value || to_string(field.value) != to_string(locale) end defp default_locale do Cldr.default_locale().cldr_locale_name end defp current_locale do Cldr.get_locale().cldr_locale_name end defp locale_options do [default_locale() | Cldr.AshTrans.locale_names()] |> Enum.map(&{Cldr.LocaleDisplay.display_name!(&1), &1}) end defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) end ``` -------------------------------- ### Phoenix LiveView for Displaying Translated Posts Source: https://github.com/timisrobert/ash_trans/blob/main/documentation/tutorials/get-started-with-ash-trans.md This Elixir code defines a Phoenix LiveView component to display a post, including its title and body. It uses Ash.get! to fetch the post and Cldr.AshTrans.translate to handle translations, ensuring the correct locale is applied. ```elixir defmodule MyAppWeb.PostLive.Show do use MyAppWeb, :live_view alias MyApp.Cldr alias MyApp.Post @impl true def render(assigns) do ~H"""

<%= @post.title %>

<%= @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.