Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created August 16, 2012 23:33
Show Gist options
  • Save wrburgess/3374506 to your computer and use it in GitHub Desktop.
Save wrburgess/3374506 to your computer and use it in GitHub Desktop.
Example of Localization in Devise Views

Install Devise

#out of article scope

Generate Devise Views

rails g devise:views

Add file to config/locale/devise.en.yml

Add file to config/locale/devise.ja.yml

class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale
def set_locale
I18n.locale = extract_locale
end
private
def extract_locale
allowed_env = %w{en ja} #english, japanese only
env = (request.env['HTTP_ACCEPT_LANGUAGE'] || "").scan(/^[a-z]{2}/).first
allowed_env.include?(env) ? env : env = "en" #default to english
end
end
# english example
# => config/locales/en.yml
devise:
confirmations:
new:
resend_confirmation_instructions: 'Resend confirmation instructions'
email: 'Email'
mailer:
confirmation_instructions:
welcome: 'Welcome'
you_can_confirm: 'You can confirm your account email through the link below'
#You can confirm your account email through the link below
confirm_my_account: 'Confirm my account'
reset_password_instructions:
hello: 'Hello'
someone_has_requested: 'Someone has requested a link to change your password, and you can do this through the link below.'
#Someone has requested a link to change your password, and you can do this through the link below.
change_my_password: 'Change my password'
if_you_did_not_request: 'If you did not request this, please ignore this email.'
#If you did not request this, please ignore this email.
your_password_will_not_change: 'Your password will not change until you access the link above and create a new one.'
# Your password will not change until you access the link above and create a new one.
unlock_instructions:
hello: 'Hello'
you_account_has_been_locked: 'Your account has been locked due to an excessive amount of unsuccessful sign in attempts.'
#Your account has been locked due to an excessive amount of unsuccessful sign in attempts.
click_the_link_below: 'Click the link below to unlock your account'
unlock_my_account: 'Unlock my account'
passwords:
new:
forgot_your_password: "Forgot your password"
send_me_reset_password_instructions: "Send me reset password instructions"
email: "Email"
edit:
change_your_password: "Change your password"
new_password: "New password"
confirm_new_password: "Confirm new password"
change_my_password: "Change my password"
registrations:
edit:
edit: "Edit"
email: "Email"
password: "Password"
password_confirmation: "Password confirmation"
current_password: "Current password"
cancel_my_account: "Cancel my account"
update: "Update"
back: "Back"
are_you_sure: "Are you sure?"
unhappy: "Unhappy?"
we_need_password_to_confirm: "(we need your current password to confirm your changes)"
leave_blank_if_no_change: "(leave blank if you don't want to change it)"
new:
sign_up: "Sign up"
email: "Email"
password: 'Password'
password_confirmation: "Password confirmation"
sessions:
new:
sign_in: 'Sign in'
email: 'Email'
password: 'Password'
remember_me: 'Remember me'
shared:
links:
forgot_your_password: "Forgot your password?"
did_not_receive_confirm_instructions: "Didn't receive confirm instructions?"
did_not_receive_unlock_instructions: "Did not receive unlock instructions?"
sign_in_with: "Sign in with"
sign_in: "Sign in"
sign_up: "Sign up"
unlocks:
new:
resend_unlock_instructions: "Resend unlock instructions"
email: 'Email'
# japanese example
# => config/locales/ja.yml
devise:
confirmations:
new:
resend_confirmation_instructions: 'Ja:Resend confirmation instructions'
email: 'Ja:Email'
mailer:
confirmation_instructions:
welcome: 'Ja:Welcome'
you_can_confirm: 'Ja:You can confirm your account email through the link below'
#You can confirm your account email through the link below
confirm_my_account: 'Ja:Confirm my account'
reset_password_instructions:
hello: 'Ja:Hello'
someone_has_requested: 'Ja:Someone has requested a link to change your password, and you can do this through the link below.'
#Someone has requested a link to change your password, and you can do this through the link below.
change_my_password: 'Ja:Change my password'
if_you_did_not_request: 'Ja:If you did not request this, please ignore this email.'
#If you did not request this, please ignore this email.
your_password_will_not_change: 'Ja:Your password will not change until you access the link above and create a new one.'
# Your password will not change until you access the link above and create a new one.
unlock_instructions:
hello: 'Ja:Hello'
you_account_has_been_locked: 'Ja:Your account has been locked due to an excessive amount of unsuccessful sign in attempts.'
#Your account has been locked due to an excessive amount of unsuccessful sign in attempts.
click_the_link_below: 'Ja:Click the link below to unlock your account'
unlock_my_account: 'Ja:Unlock my account'
passwords:
new:
forgot_your_password: "Ja:Forgot your password"
send_me_reset_password_instructions: "Ja:Send me reset password instructions"
email: "Ja:Email"
edit:
change_your_password: "Ja:Change your password"
new_password: "Ja:New password"
confirm_new_password: "Ja:Confirm new password"
change_my_password: "Ja:Change my password"
registrations:
edit:
edit: "Ja:Edit"
email: "Ja:Email"
password: "Ja:Password"
password_confirmation: "Ja:Password confirmation"
current_password: "Ja:Current password"
cancel_my_account: "Ja:Cancel my account"
update: "Ja:Update"
back: "Ja:Back"
are_you_sure: "Ja:Are you sure?"
unhappy: "Ja:Unhappy?"
we_need_password_to_confirm: "Ja:(we need your current password to confirm your changes)"
leave_blank_if_no_change: "Ja:(leave blank if you don't want to change it)"
new:
sign_up: "Ja:Sign up"
email: "Ja:Email"
password: 'Ja:Password'
password_confirmation: "Ja:Password confirmation"
sessions:
new:
sign_in: 'Ja:Sign in'
email: 'Ja:Email'
password: 'Ja:Password'
remember_me: 'Ja:Remember me'
shared:
links:
forgot_your_password: "Ja:Forgot your password?"
did_not_receive_confirm_instructions: "Ja:Didn't receive confirm instructions?"
did_not_receive_unlock_instructions: "Ja:Did not receive unlock instructions?"
sign_in_with: "Ja:Sign in with"
sign_in: "Ja:Sign in"
sign_up: "Ja:Sign up"
unlocks:
new:
resend_unlock_instructions: "Ja:Resend unlock instructions"
email: 'Ja:Email'
require_relative '../../spec_helper'
describe Admin::UsersController do
describe "localization on #index" do
it 'returns AppName in English' do
page.driver.header 'Accept-Language', 'en'
visit('/admin/users')
find('.brand').should have_content('LessonNote')
end
it 'returns AppName in Japanese' do
page.driver.header 'Accept-Language', 'ja'
visit('/admin/users')
find('.brand').should have_content('Ja:LessonNote')
end
it 'returns AppName in English as default' do
page.driver.header 'Accept-Language', 'de'
visit('/admin/users')
find('.brand').should have_content('LessonNote')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment