over 3 years ago

系統:

Mac OS X Yosemite 10.10.1
Ruby 2.1.5
Rails 4.1.8

相關連結

Bing Translator ( https://github.com/relrod/bing_translator-gem )

設定步驟如下

1. Gemfile 設定

添加必要的 gem 套件至 Gemfile 檔中

# Use bing_translator for Post permalink
gem 'bing_translator'

2. Sign Up Microsoft Translator

按照 Bing Translator 中,Getting a Client ID and Secret的步驟,申請使用 Microsoft Translator 的權限,必須取得以下三種資訊。

  • 用戶端識別碼 (CLIENT ID)
  • 用戶端密碼 (CLIENT SECRET)
  • 主要帳戶金鑰 (ACCOUNT KEY)

3. 環境參數的設定

這邊是使用 gem figaro 設定環境參數,安裝教學可參考 此篇文章

config/application.yml

BING_APP_ID: 'YOUR_BING_APP_ID'
BING_APP_SECRET: 'YOUR_BING_APP_SECRET'
BING_ACCOUNT_KEY: 'YOUR_BING_ACCOUNT_KEY'

3. Model 設定

新增 app/models/concerns/translate_permalink.rb

module TranslatePermalink
  extend ActiveSupport::Concern

  included do
    class_attribute :permalink_attributes, :permalink_field, :before_methods, :after_methods
    after_save :create_permalink

    def create_permalink
      if self.permalink.nil?
        chinese_permalink = self.class.permalink_attributes.collect do |attr_name|
          self.send(attr_name)
        end * '-'
        self.class.before_methods.each do |method|
          chinese_permalink = self.send(method, chinese_permalink)
        end

        english_permalink = Translator.t(chinese_permalink)
        self.class.after_methods.each do |method|
          english_permalink = self.send(method, english_permalink)
        end

        self.update_attribute(self.class.permalink_field, english_permalink.parameterize)
      end
    end
  end

  module ClassMethods
    def translate_permalink(attr_names, options = {})
      options = { permalink_field: 'permalink' }.merge(options)
      self.permalink_attributes = Array(attr_names)
      self.permalink_field = options[:permalink_field]
      self.before_methods = Array(options[:before_methods])
      self.after_methods = Array(options[:after_methods])
    end
  end

  class Translator
    class <<self
      def t(text)
        self.translator.translate(text, to: 'en')
      end

      def translator
        @translator ||= BingTranslator.new(ENV['BING_APP_ID'], ENV['BING_APP_SECRET'], false, ENV['BING_ACCOUNT_KEY'])
      end
    end
  end
end

app/models/post.rb

class Post < ActiveRecord::Base
  include TranslatePermalink
  ...
  translate_permalink :title
  ...
  def to_param
    param = super
    param << "-#{permalink}" unless permalink.empty?
    param
  end
  ...
end

4. Controller 設定

app/controllers/posts_controller.rb,移除 new action

class PostsController < ApplicationController
  ...
  def index
    @post = Post.new
    @posts = Post.all
    respond_with(@posts)
  end
  ...
  def create
    @post = current_user.posts.build(post_params)
    @post.save
    redirect_to edit_post_path(@post)
  end
  ...
end

5. View 設定

app/views/posts/index.html.slim

.container

  .toolbar

    = html_button t(:new_post), icon: 'plus', 'data-toggle' => 'modal', 'data-target' => '#new_post_modal'
    = render 'new_post_modal', post: @post
    ...

新增 app/views/posts/_new_post_modal.html.slim

#new_post_modal.modal.fade

  .modal-dialog

    .modal-content

      .modal-header

        button.close type="button" data-dismiss="modal"

          span aria-hidden="true" &times;

          span.sr-only Close

        h4.modal-title= t(:new_post)
      .modal-body

        = bootstrap_form_for post, layout: :horizontal do |f|
          = f.text_field :title, placeholder: 'Post Title', autofocus: true
          = f.form_group do
            =' f.primary t(:submit)
            = html_button t(:cancel), 'data-dismiss' => 'modal'

移除 app/views/posts/new.html.slim

← Post 管理 - part 2 - Vote Up & Vote down Post 使用 Vagrant 和 Chef 開發 Rails - Part 1 - Vagrant →
 
comments powered by Disqus