Giter VIP home page Giter VIP logo

globalize-automatic's Introduction

Globalize Automatic

Installation

gem install globalize-automatic

Rails 5.0

In your Gemfile

gem 'globalize-automatic', github: 'takeyuweb/globalize-automatic', branch: 'rails-5-0'

and

bundle install

Configuration

# config/initializers/globalize_automatic.rb
require 'globalize-automatic'
Globalize::Automatic.translator = 
  Globalize::Automatic::Translator::EasyTranslate.new

# EasyTranslate configuration
EasyTranslate.api_key = 'xxx'

Example

宣言

class Post
  # Automatically translated from English.
  translates :title, :text, automatic: :en
end
class Post
  # Automatically translated from English or Japanese. (English preferred)
  translates :title, :text, automatic: %i(en ja)
end
class Post
  # Automatically translated from English
  translates :title, :text, automatic: { from: :en, to: %i(ja fr vi) }
end
class CreatePostAutomatics < ActiveRecord::Migration
  def up
    Post.create_translation_table! title: :string, text: :text
    Post.create_automatic_translation_table! :title, :text
  end
    
  def down
    Post.drop_translation_table!
    Post.drop_automatic_translation_table!
  end
end

自動翻訳対象を追加

class AddPostAutomatics < ActiveRecord::Migration
  def up
    Post.add_translation_fields! description: :text
    Post.add_automatic_translation_fields! :description
  end
    
  def down
    remove_column :post_automatic_translations, :description
    remove_column :post_automatic_translations, :description_automatically
  end
end

更新

自動翻訳

I18n.locale = :en
post.title = 'globalize'
post.save!

post.title_ja_automatic # => false
post.title_vi_automatic # => true

post.reload

I18n.locale = :ja
post.title # => nil
I18n.locale = :vi
post.title # => 'โลกาภิวัฒน์'
post.attributes = {
   title_en: 'globalize',
   title_ja_automatic: true
}
post.save!

post.reload

I18n.locale = :en
post.title # => 'globalize'
I18n.locale = :ja
post.title # => '国際化'

自動翻訳無効化

post.attributes = {
   title_en: 'globalize',
   title_fr_automatic: false,
   title_fr: 'Hoge'
}
post.save!

post.reload

I18n.locale = :en
post.title # => 'globalize'
I18n.locale = :vi
post.title # => 'โลกาภิวัฒน์'
I18n.locale = :fr
post.title # => 'hoge'

自動翻訳の原文優先順位

class Post
  # Automatically translated from English or Japanese. (English preferred)
  translates :title, :text, automatic: %i(en ja)
end

post = Post.new
post.attributes = {
    title_en: 'English',
    title_ja: '日本語'
}
post.save!

post.reload

I18n.locale = :fr
post.title # => 'Anglais' # It means 'English'.
class Post
  # Automatically translated from English or Japanese. (English preferred)
  translates :title, :text, automatic: %i(en ja)
end

post = Post.new
post.attributes = {
    title_en: nil,
    title_ja: '日本語'
}
post.save!

post.reload

I18n.locale = :fr
post.title # => 'Japonais' # It means '日本語'.

取得

翻訳文属性名

globalize-accessors

Post.globalize_attribute_names # => [:title_ja, :title_en, :title_fr, :title_vi, :text_ja, :text_en, :text_fr, :text_vi]

自動翻訳有効/無効属性名

Post.automatic_translation_attribute_names # => [:title_ja_automatically, :title_en_automatically, :title_fr_automatically, :title_vi_automatically, :text_ja_automatically, :text_en_automatically, :text_fr_automatically, :text_vi_automatically]

strong parameters

permitted = Post.globalize_attribute_names + Post.automatic_translation_attribute_names
params.require(:post).permit(*permitted)

非同期

ActiveJobを利用による非同期翻訳

# config/initializers/globalize_automatic.rb
Globalize::Automatic.asynchronously = true

他の翻訳ライブラリやサービスへの対応

Translatorクラスを書けば対応できます。

class YourTranslator < Globalize::Automatic::Translator
  def translate(text, from, to)
    # 適当な翻訳処理を行って訳文を返す
    return translated
  end
end

Globalize::Automatic.translator = YourTranslator::EasyTranslate.new

TODO

  • EasyTranslate依存の切り出し
    • 他の翻訳ライブラリやサービスへの対応。
      • Microsoft Translator
      • Gengo etc

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/takeyuweb/globalize_automatic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Licence

Copyright (c) 2017 Yuichi Takeuchi released under the MIT license

This project rocks and uses MIT-LICENSE.

globalize-automatic's People

Contributors

takeyuweb avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

globalize-automatic's Issues

Force auto-translate

Hi @takeyuweb

Any hint on how to force auto translate?

I have old data that needs to be translated. Yet the auto-translation needs the attribute to be dirty(AFAIK) to run.

But I tried this and it did not invoke auto translate.

s=Service.find(1)
s.title_will_change!
s.save

It did not save.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.