Skip to content

Instantly share code, notes, and snippets.

@jcasimir
Created August 13, 2012 18:17
Show Gist options
  • Save jcasimir/3342899 to your computer and use it in GitHub Desktop.
Save jcasimir/3342899 to your computer and use it in GitHub Desktop.
1.9.3-p125 :044 > accepts = "en,en-US;0.8,es;0.2"
=> "en,en-US;0.8,es;0.2"
1.9.3-p125 :045 > accepts.scan(/([\w-]{2,})/).map(&:first).map(&:to_sym)
=> [:en, :"en-US", :es]
1.9.3-p125 :046 > accepts.downcase.scan(/([\w-]{2,})/).map(&:first)
=> ["en", "en-us", "es"]
####
symbols = Symbol.all_symbols
# => [...]
I18n.locale = "garbage_from_a_user"
# => "garbage_from_a_user"
I18n.locale
# => :garbage_from_a_user
Symbol.all_symbols - symbols
# => [:garbage_from_a_user]
#####
class ApplicationController < ActionController::Base
before_filter :set_locale
private
def set_locale
I18n.locale = params[:locale]
end
end
#####
class ApplicationController < ActionController::Base
before_filter :set_locale
private
def set_locale
if params[:locale]
locale = params[:locale].downcase
I18n.locale = locale if locale_available?(locale)
end
end
def locale_available?(locale)
I18n.available_locales.map(&:to_s).include?(locale)
end
end
#####
class ApplicationController < ActionController::Base
before_filter :set_locale
def default_url_options(options = {})
{ :locale => I18n.locale }.merge(options)
end
#...
end
#####
class ApplicationController < ActionController::Base
before_filter :set_locale
def default_url_options(options = {})
{ :locale => I18n.locale }.merge(options)
end
private
def set_locale
if params[:locale]
locale = params[:locale].downcase
I18n.locale = locale if locale_available?(locale)
end
end
def locale_available?(locale)
I18n.available_locales.map(&:to_s).include?(locale)
end
end
#####
class ApplicationController < ActionController::Base
before_filter :set_locale
private
def store_locale
session["locale"] = I18n.locale
end
def locale_available?(locale)
I18n.available_locales.map(&:to_s).include?(locale)
end
end
#####
## Setting the Locale
session[:locale] = I18n.locale
## Fetching the Locale
I18n.locale = session[:locale]
## Different Users
session["locale_for_#{current_user.to_param}"] = I18n.locale
I18n.locale = session["locale_for_#{current_user.to_param}"]
## User Preferences
Image: preferences?
## Store in the DB
rails generate migration add_locale_to_users locale:string
## ApplicationController
before_filter :set_locale
private
def set_locale
I18n.locale = current_user.locale || I18n.default_locale
end
## Composite Strategy
Image: quilt
## Composite Code
class ApplicationController < ActionController::Base
include LocaleController
end
module LocaleController
def self.included(controller)
controller.class_eval do
before_filter :set_locale
def default_url_options(options = {})
{ :locale => I18n.locale }.merge(options)
end
def set_locale
LocaleSetter.set_by(:user => current_user,
:params => params,
:session => session,
:http => request.env['HTTP_ACCEPT_LANGUAGE'])
end
end
end
end
module LocaleSetter
LOOKUP_CHAIN = [:params, :user, :session, :http, :default]
# def self.set_by(inputs)
# by_params(inputs[:params]) ||
# by_user(inputs[:user]) ||
# by_session(inputs[:session], inputs[:user]) ||
# by_http(inputs[:http]) ||
# I18n.default_locale
# end
def self.set_by(inputs)
LOOKUP_CHAIN.detect do |lookup|
send("by_#{lookup}", inputs[lookup]) if params[lookup]
end
end
def self.by_params(params)
locale = params[:locale].downcase
locale if locale_available?(locale)
end
def self.by_session(session)
session[:locale]
end
def self.by_user(user)
user.locale
end
def self.by_http(accepts)
(accept_list(accepts) & I18n.available_locales).first

end
def self.locale_available?(locale)
I18n.available_locales.map(&:to_s).include?(locale)
end
private
def self.accept_list(accepts)

accepts.downcase.scan(/([\w-]{2,})/).map{|l| l.first.to_sym}

end

end
#### Using Lookup Files
Image: Dictionary
#### Normal YAML
en:
article:
deleted: "Article has been deleted."
error: "Article failed to save."
created: "Article created successfully."
new: "New Article"
update: "Save Changes"
create: "Save Article"
comment:
created: "Comment posted, you're internet famous."
spam: "Burn your own face off, spammer."
create: "Post Comment"
status:
moderation: "Comment is pending moderation."
approved: "Comment approved."
#### t and I18n.t
t 'article.deleted'
I18n.t 'comment.created'
t 'comment.status.approved'
#### Interpolation
en:
generic:
deleted: "%{subject} has been deleted."
error: "%{subject} failed to save."
t 'generic.deleted', :subject => "Comment"
'(Ugh, what about "Comment"?)'
en:
models:
comment: "Comment"
article: "Article"
generic:
deleted: "%{subject} has been deleted."
error: "%{subject} failed to save."
t 'generic.deleted', :subject => t('models.comment')
'But wait...'
### Automatic Lookups with ActiveRecord
Image: Robot, assistant, file cabinet
### The Magic!
a = Article.new(:body => "boo")
a.save
a.errors.first.inspect
a.errors.first.last
# In Translation File...
en:
activerecord:
errors:
models:
article:
attributes:
title:
blank: "You need a title!"
### Validation Key
Insert the table from the RailsGuide
### Lookup Path & Priority
activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages
### Validation Message Lookup
en:
activerecord:
errors:
models:
article:
attributes:
title:
blank: "You need a title!"
### In the View Layer
<%= errors_for(@article) %>
module ApplicationHelper
def errors_for(subject)
if subject.errors
render(:partial => 'common/errors',
:subject => subject.errors)
end
end
end
<%= if errors.any? %>
<div class='errors'>
<ul>
<% errors.each do |e|
<li><%= e.last %></li>
<% end %>
</ul>
</div>
<% end %>
### Attribute & Model Names
en:
activerecord:
attributes:
article:
title: "Title"
comment:
body: "Your comment"
### Applied in the View
<%= errors_for(@article) %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
@nateklaiber
Copy link

Should line 154 set I18n.locale?

def set_locale
        I18n.locale = LocaleSetter.set_by(:user => current_user,
                            :params => params,
                            :session => session,
                            :http => request.env['HTTP_ACCEPT_LANGUAGE'])
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment