geminstallglobalize-automaticIn your Gemfile
gem'globalize-automatic',github: 'takeyuweb/globalize-automatic',branch: 'rails-5-0'and
bundle install# config/initializers/globalize_automatic.rbrequire'globalize-automatic'Globalize::Automatic.translator=Globalize::Automatic::Translator::EasyTranslate.new# EasyTranslate configurationEasyTranslate.api_key='xxx'classPost# Automatically translated from English.translates:title,:text,automatic: :enendclassPost# Automatically translated from English or Japanese. (English preferred)translates:title,:text,automatic: %i(enja)endclassPost# Automatically translated from Englishtranslates:title,:text,automatic: {from: :en,to: %i(jafrvi)}endclassCreatePostAutomatics < ActiveRecord::MigrationdefupPost.create_translation_table!title: :string,text: :textPost.create_automatic_translation_table!:title,:textenddefdownPost.drop_translation_table!Post.drop_automatic_translation_table!endendclassAddPostAutomatics < ActiveRecord::MigrationdefupPost.add_translation_fields!description: :textPost.add_automatic_translation_fields!:descriptionenddefdownremove_column:post_automatic_translations,:descriptionremove_column:post_automatic_translations,:description_automaticallyendendI18n.locale=:enpost.title='globalize'post.save!post.title_ja_automatic# => falsepost.title_vi_automatic# => truepost.reloadI18n.locale=:japost.title# => nilI18n.locale=:vipost.title# => 'โลกาภิวัฒน์'post.attributes={title_en: 'globalize',title_ja_automatic: true}post.save!post.reloadI18n.locale=:enpost.title# => 'globalize'I18n.locale=:japost.title# => '国際化'post.attributes={title_en: 'globalize',title_fr_automatic: false,title_fr: 'Hoge'}post.save!post.reloadI18n.locale=:enpost.title# => 'globalize'I18n.locale=:vipost.title# => 'โลกาภิวัฒน์'I18n.locale=:frpost.title# => 'hoge'classPost# Automatically translated from English or Japanese. (English preferred)translates:title,:text,automatic: %i(enja)endpost=Post.newpost.attributes={title_en: 'English',title_ja: '日本語'}post.save!post.reloadI18n.locale=:frpost.title# => 'Anglais' # It means 'English'.classPost# Automatically translated from English or Japanese. (English preferred)translates:title,:text,automatic: %i(enja)endpost=Post.newpost.attributes={title_en: nil,title_ja: '日本語'}post.save!post.reloadI18n.locale=:frpost.title# => 'Japonais' # It means '日本語'.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_namesparams.require(:post).permit(*permitted)ActiveJobを利用による非同期翻訳
# config/initializers/globalize_automatic.rbGlobalize::Automatic.asynchronously=trueTranslatorクラスを書けば対応できます。
classYourTranslator < Globalize::Automatic::Translatordeftranslate(text,from,to)# 適当な翻訳処理を行って訳文を返すreturntranslatedendendGlobalize::Automatic.translator=YourTranslator::EasyTranslate.new- EasyTranslate依存の切り出し
- 他の翻訳ライブラリやサービスへの対応。
- Microsoft Translator
- Gengo etc
- 他の翻訳ライブラリやサービスへの対応。
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.
Copyright (c) 2017 Yuichi Takeuchi released under the MIT license
This project rocks and uses MIT-LICENSE.