• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kenta-aktsk/translator: Model translation/globalization/localization library for ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

kenta-aktsk/translator

开源软件地址(OpenSource Url):

https://github.com/kenta-aktsk/translator

开源编程语言(OpenSource Language):

Elixir 100.0%

开源软件介绍(OpenSource Introduction):

Translator

Translator is a simple model translation/globalization/localization library for Elixir, inspired by globalize.

Dependencies

You need to use ecto 2.0 or higher.

Installation

Add translator to your list of dependencies in mix.exs:

def deps do
  [{:translator, github: "kenta-aktsk/translator"}]
end

Ensure translator is started before your application:

def application do
  [applications: [:translator]]
end

Basic Usage

Assume that you have user model like below:

defmodule MyApp.User do
  use MyApp.Web, :model

  schema "users" do
    field :email, :string
    field :name, :string
    field :profile, :string

    timestamps
  end

  @required_fields ~w(email name)a
  @optional_fields ~w(profile)a

  def changeset(user, params \\ %{}) do
    user
    |> cast(params, @required_fields ++ @optional_fields)
    |> validate_required(@required_fields)
  end
end

And assume that you have user record in your db table like below:

%{id: 1, email: "user01@example.com", name: "Kenta Katsumata", profile: "living in Tokyo"}

First of all, if you want to translate name and profile fields, define migration file for translation table like below:

defmodule MyApp.Repo.Migrations.CreateUserTranslation do
  use Ecto.Migration

  def change do
    create table(:user_translations) do
      add :user_id, references(:users, on_delete: :delete_all), null: false
      add :locale, :string, null: false
      add :name, :string, null: false
      add :profile, :text

      timestamps
    end

    create index(:user_translations, [:user_id, :locale], unique: true)
  end
end

Next, execute migration.

mix ecto.migrate

Next, define UserTranslation model like below:

defmodule MyApp.UserTranslation do
  use MyApp.Web, :model
  use Translator.TranslationModel,
    schema: "user_translations", belongs_to: MyApp.User, required_fields: [:name], optional_fields: [:profile]
end

Next, define has_one association and preload query function in User model. (function name is up to you.)

defmodule MyApp.User do
  alias MyApp.UserTranslation
  ...
  schema "users" do
    ...
    has_one :translation, UserTranslation
    ...
  end
  def preload_all(query, locale) do
    from query, preload: [translation: ^UserTranslation.translation_query(locale)]
  end
end

Next, insert some translation record like below:

INSERT INTO user_translations (user_id, locale, name, profile) VALUES (1, "en", "Kenta Katsumata", "living in Tokyo");
INSERT INTO user_translations (user_id, locale, name, profile) VALUES (1, "ja", "勝又健太", "東京在住");

Now you can get associated translation record like below:

alias MediaSample.{Repo, User}
user = User |> User.preload_all("ja") |> Repo.get!(1)
user.translation
# => %{user_id: 1, name: "勝又健太", profile: "東京在住"}

Insert, Update

You can use insert_or_update/4 function for inserting or updating translation record like below:

# create action in user controller 
def create(conn, %{"user" => user_params}) do
  changeset = User.changeset(%User{}, user_params)

  Repo.transaction fn ->
    user = Repo.insert!(changeset)
    UserTranslation.insert_or_update(Repo, user, user_params, "ja")
  end
end

# update action in user controller 
def update(conn, %{"id" => id, "user" => user_params}) do
  user = Repo.get!(User, id)
  changeset = User.changeset(user, user_params)

  Repo.transaction fn ->
    user = Repo.update!(changeset)
    UserTranslation.insert_or_update(Repo, user, user_params, "ja")
  end
end

It is up to you what locale to pass.

View helper

You can use view helper to get translated field like below:

# web.ex
def view do
  quote do
    ...
    import Translator.TranslationHelpers
  end
end

# templates/user/show.html.eex
<li>
  <%= translate(@user, :name) %>
</li>

# templates/user/form.html.eex
<%= text_input :user, :name, value: translate(@changeset, @user, :name), class: "form-control" %>

Example

You can check some example at my phoenix example repository media_sample.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ratthapon/thai-license-plate-recognition: A Thai license plate localization and ...发布时间:2022-08-16
下一篇:
NicoKuroKusagi/localization-abi-hub发布时间:2022-08-16
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap