Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Home

Adrian Duyzer edited this page · 9 revisions
Clone this wiki locally

This is the future home of development and usage documentation for Forge (at least, the stuff that shouldn't more reasonably end up in the source code).

Internationalization

Steps to Internationalization

  1. Set up the fields
  2. Set up the languages
  3. Generate the migrations
  4. Hell yeah that's it

Set up the fields

Open config/i18n_fields.yml, you'll see a file that looks like this:

tables:
  events:
    - title
    - location
    - description

  galleries:
    - title

  pages:
    - title
    - content
    - slug
    - path
    - seo_title
    - seo_keywords
    - seo_description

Add any more tables and fields that you may want.

Set up the languages

Open config/initializers/forge, towards the top you'll see a commented out languages section

# config.languages = {
#   "French" => :fr,
#   "German" => :de
#   # etc.
# }

Uncomment this and add any other languages you may want, following the same format:

config.languages = {
  "French" => :fr
}

Generate the migrations

Run

bundle exec rails generate forge:i18n_migrations

This will generate a migration to add columns for the fields and languages that you've selected in those YAML files. Like so:

class AddI18nFields0735c9d56bba0c3ba8c8e45b5dcdb8ba < ActiveRecord::Migration
  def change
    add_column :sales, :title_fr, :string
    add_column :sales, :title_de, :string
    add_column :sales, :description_fr, :text
    add_column :sales, :description_de, :text
    add_column :product_categories, :title_fr, :string
    add_column :product_categories, :title_de, :string
    ...
  end
end

If you add other languages or fields to your config files you can run the generator again, it will only attempt to add fields that don't already exist.

Some things to note

The form builders have been changed considerably to ensure that you don't have to do a damn thing in the actual template code to get this to work. The generators have been updated to reflect this. Let's look at some before and after examples:

Before:

= form_for [:forge, @thing] do |f|
  = title_row("Title", :explanation => "Give it a title") { f.text_field :title, :class => "title" }
  = side_row("A Special Thing", :explanation => "You know, a thing.") { f.text_field :a_thing }
  = content_row("Do your content") { f.cktext_area :content, :your => "options" }

After:

= form_for [:forge, @thing], :builder => ForgeFormBuilder do |f|
  = f.title_field :title, :explanation => "Give your thing a title"
  = f.text_field :a_thing, :label => "A Special Thing", :explanation => "You know, a thing."
  = f.cktext_area :content, :label => "Do your content", :your => "options"

If you need the original text_field, cktext_area, or text_area helper you can still call them with f.super_text_field, f.super_text_area, or f.super_cktext_area

If you don't want an hr to be included with your form field, pass it the option :last => true

Front End Magics

has_foreign_language gem

https://github.com/factore/has_foreign_language

gem 'has_foreign_language

In each model that will be internationalized on the front end you have to list each field. i.e. page.rb: has_foreign_language :title, :content, :seo_title, :seo_keywords, :seo_description

config/initializers/forge.rb

Add this:

# Set default I18n
I18n.default_locale = :en

routes.rb

Add/edit this:

scope "(:locale)", :constraints => {:locale=> /[a-z]{2}(-[A-Z]{2})?/ } do
  [include all required front end resources]
  resources :posts, :only => [:index, :show]
end
match ':locale/posts/:month/:year', :controller => 'posts', :action => 'index'
match ':locale/*slugs', :controller => 'pages', :action => 'show'
match ':locale', :to => "index#index"

routes_helper.rb

Edit this:

def page_path(page)
  "/#{I18n.locale}#{page.path}"
end

forge_controller.rb

Add this to set the locale to :en every time the application is loaded. Forge itself has no other languages:

before_filter :set_locale

private
def set_locale
  I18n.locale = :en
end

application_controller.rb

Add this to set the locale every time the application is loaded:

before_filter :set_locale

private
def set_locale
  I18n.locale = session[:locale] = params[:locale] || session[:locale] || I18n.default_locale
end

Add this to add the locale param to every slug:

private
def default_url_options(options={})
  {:locale => I18n.locale}
end

forge_controller.rb

Add this to negate the application controllers to keep the locale params out of forge routes:

 private
 def default_url_options(options={})
   {:locale => nil}
 end

page.rb

The slug validation doesn't like the :locale in the path. To make this work just add '/en' to the path begin checked against Rails.application.routes:

  def path_is_not_route
    match = Rails.application.routes.recognize_path("/en#{path}", :method => :get) rescue nil
    errors.add(:base, "There's already something happening at #{MySettings.site_url}#{path}.  Try giving the page a different name.") if match && !match[:slugs]
  end

application_helper.rb (optional)

Add something like this to create a dynamic link for switching the current locale.

def locale_link
  if I18n.locale == :en
    link_to("Francais", request.original_fullpath.gsub("/en", "/fr"))
  else
    link_to("English", request.original_fullpath.gsub("/fr", "/en"))
  end
end
Something went wrong with that request. Please try again.