I am building a Rails 4 site for a client in Singapore, Malaysia, Taiwan and China.
The locale code for a Chinese speaking Malaysian is zh-MY.
I would like to keep a base zh-CN (Simplified Chinese) set of locale files and for zh-MY to fallback to zh-CN.
Just having a zh is not correct as zh-TW (Traditional Chinese) is what Taiwan uses and there are big differences between that and zh-CN.
So here's my config/application.rb file as per the Rails Guide.
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require "i18n/backend/fallbacks"
module MyAwesomeApp
class Application < Rails::Application
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
# all translations from config/locales/**/*.rb,yml are auto loaded.
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
# The default locale is :en
config.i18n.default_locale = :en
# See http://guides.rubyonrails.org/i18n.html#localized-views for a discussion of
# how language codes fall-back.
config.i18n.available_locales = [:en, :'zh-CN', :'zh-TW', :'en-SG', :'en-MY', :'zh-MY']
I18n.fallbacks.map(:'zh-MY' => :'zh-CN')
end
end
But this simply doesn't work.
When I actually set the locale to :zh-MY it does not fall back to :zh-CN but to :en
What am I missing?
update:
If I puts "I18n.fallbacks #{I18n.fallbacks}" it says I18n.fallbacks {}. Clearly I18n.fallbacks.map is failing.
update
As per the suggestion in the comments I looked at I18n.fallbacks[:'zh-MY'] in the very next line after I checked I18n.fallbacks and it returns [:"zh-MY", :zh, :"zh-CN", :en]
update
With a binding.pry in my application controller I have checked the locale etc and observe this:
[1] pry(#<ServicesController>)> I18n.locale
=> :"zh-MY"
[2] pry(#<ServicesController>)> I18n.fallbacks
=> {:en=>[:en], :"zh-MY"=>[:"zh-MY", :zh, :en]}
So somewhere between the Rails app starting up and the controller's set_locale method being called, I18n.fallbacks is being reset to the default.
Don't ask me why but this works, despite what the official docs say.
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require "i18n/backend/fallbacks"
Bundler.require(*Rails.groups)
module MyAwesomeApp
class Application < Rails::Application
# all translations from config/locales/**/*.rb,yml are auto loaded.
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
# The default locale is :en
config.i18n.default_locale = :en
# See http://guides.rubyonrails.org/i18n.html#localized-views for a
# mostly correct discussion of how language codes fall-back.
config.i18n.available_locales = [:en, :'zh-CN', :'zh-TW', :'en-SG', :'en-MY', :'zh-MY']
config.i18n.fallbacks = {:'zh-MY' => :'zh-CN'}
end
end
Removing the I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) and setting the fallbacks via config.i18n.fallbacks = {:'zh-MY' => :'zh-CN'} rather than I18n.fallbacks.map(:'zh-MY' => :'zh-CN') makes it all work perfectly.
And now in my controller, at the same breakpoint as discussed in the 3rd question update:
[1] pry(#<ServicesController>)> I18n.fallbacks
=> {:en=>[:en], :"zh-MY"=>[:"zh-MY", :zh, :"zh-CN", :en]}
I hope this is helpful to others.
Related
This is just a curious irritant, but why does my app not include the expected line in config/application.rb, or anywhere else?
require 'rails/all'
This app was generated using Rails Composer in early 2014, if that makes a difference. Also, it is Rails 4.2.1.
The issue arose only because I am studying the Configuring Rails Applications and the The Rails Initialization Process guides as I have a need to modify my initialization process. Both state that the config/application.rb file is expected to contain that line, but mine does not. And, yes, the app runs just fine locally and on Heroku, so... Why?
My file is:
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module Theappname
class Application < Rails::Application
config.generators do |g|
# Enable Chrome Source Maps so CSS and JS can be debugged
#g.sass_options = { :debug_info => true }
# don't generate RSpec tests for views and helpers
g.test_framework :rspec, fixture: true
g.fixture_replacement :factory_girl, dir: 'spec/factories'
g.view_specs false
g.helper_specs false
end
# Rails 4 should include all helpers for controllers and views
config.action_controller.include_all_helpers = true
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
#config.time_zone = 'Eastern Time (US & Canada)'
config.time_zone = 'UTC' # Don't use local time or you won't notice time issues.
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not check for unavailable locales
I18n.enforce_available_locales = false
# not needed at 4.0
config.assets.initialize_on_precompile = false
# Load files in lib
config.autoload_paths += %W(#{config.root}/lib)
# Extend Rails classes in lib/core_ext/<classname>.rb... See above?
#config.autoload_paths += Dir[File.join(Rails.root, "lib", "core_ext", "*.rb")].each {|l| require l }
# 20150711 Default Date formats
#default_date_formats = { :default => '%d.%m.%Y' }
default_date_formats = { :default => '%Y.%m.%d' }
Time::DATE_FORMATS.merge!(default_date_formats)
Date::DATE_FORMATS.merge!(default_date_formats)
# 20150808 Adding Delayed_Job queueing for daily_report and such
config.active_job.queue_adapter = :delayed_job
end
end
You can require 'rails/all' if that suits your fancy, but the parts of rails necessary to run your application are required with these lines:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
If I were to guess the motivation for this, it is probably that you don't necessarily need or want all of the parts of rails in your application, so better to explicitly require the ones you want instead of everything. In this case if you were to require 'rails/all' you would end up with action_view, active_job, and rails/test_unit as well as the above. The files required can be found in railties.
I have been trying to validate that the first name does not include anything but letters, numbers, hyphens, or underscores.
In my users.rb model I am using this code:
validates :first_namename, :firstname_convention => true
Which goes to the FirstNameConvention class:
class FirstnameConventionValidator < ActiveModel::EachValidator
def validate_each(record, field, value)
unless value.blank?
record.errors[field] << "is not alphanumeric (letters, numbers, underscores or periods)" unless value =~ /^[[:alnum:]._-]+$/
record.errors[field] << "should start with a letter" unless value[0] =~ /[A-Za-z]/
record.errors[field] << "contains illegal characters" unless value.ascii_only?
end
end
end
And that file is stored in app/validators - a folder that I had to make.
I get this error:
ArgumentError in UsersController#index
Unknown validator: 'FirstnameConventionValidator'
I have tried placing:
config.autoload_paths += %W["#{config.root}/app/validators"]
In config/application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ThorCinema
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.autoload_paths << Rails.root.join('app', 'validators')
end
end
But this still doesn't work.
What am I doing wrong?
app/validators is a good place to store validators, all you need to do, is to tell Rails about this folder existence. Add this line to your autoload paths in config/application.rb (YourAppName::Application section):
config.autoload_paths << Rails.root.join('app', 'validators')
That's it
I am a noob in Ruby/Rails and I am creating my first project now.
I am using Devise gem for authentication system. I have installed it and I am on fight to change the default messages from "en" (default language) to "pt-BR".
I have a file called devise.pt-BR.yml inside /config/locales/ with all translations for this language and I have followed a few tips but when I restart the server, I still get "en" as the default language.
I don't want to have two or more languages, I just need to work with "pt-BR" instead "en".
My environment:
Fedora 16
Ruby 1.9.2p320
Rails 3.2.6
Devise 2.1.2
/config/application.rb content (look at bottom):
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Foo
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true
# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
config.active_record.whitelist_attributes = true
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.i18n.load_path += Dir[Rails.root.join('devise', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :"pt-BR"
I18n.locale = :"pt-BR"
end
end
Change the line
config.i18n.default_locale = :"pt-BR"
to
config.i18n.default_locale = "pt-BR"
I was wrong, it's working!
I had to drop it inside application.rb:
config.i18n.load_path += Dir[Rails.root.join('locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :'pt-BR'
And put "devise.pt-BR.yml" inside /config/locales/
I was looking for "Sign in" and label from fields at "Log in" view but I think these strings are not translatable, I will generate these views and manually revise them.
Thank you.
Try renaming your locale file to pt-BR.yml, or else change your reference in application.rb to devise.pt-BR.yml.
I'm using Rails 3 with Globalize3 0.2.0.beta4
Ideally I need :fr to fallback to :en and vice versa.
There are cases when only a French translation is available and I need to show it even if the locale is :en.
I tried
config.i18n.fallbacks = { :fr => :en, :en => :fr }
but somewhat unsurprisingly it causes a stack level too deep error.
I'm changing my answer.
To enable fallbacks, add the following to your environment.rb file:
#support for locale fallbacks
require "i18n/backend/fallbacks"
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
Then, you can enable circular fallbacks like you were trying to, eg:
config.i18n.fallbacks = {'en' => 'fr', 'fr' => 'en'}
In this case, if something is missing in the en locale, it'll check the fr locale, and then the other way around. I don't get any errors running this.
Source: http://batsov.com/articles/2012/09/12/setting-up-fallback-locale-s-in-rails-3/
If you pass an array of locales they will be set as default fallbacks for all locales.
config.i18n.fallbacks = [:en, :fr]
Unfortunately, I haven't found a way to set up just two locales to fall back to each other.
In the end I monkey patched Globalize3. Not great as I have to update the patch whenever the site needs a new locale, but hey, it worked.
module Globalize
class << self
def fallbacks(locale = self.locale)
case locale
when :en then [:en, :fr]
when :fr then [:fr, :en]
end
end
end
end
This seems to have changed to this:
Globalize.fallbacks = {:en => [:en, :fr], :fr => [:fr, :en]}
Got from the official docs:
https://github.com/globalize/globalize#fallback-locales-to-each-other
In latest i18n gem (0.7.0) I have found it necessary to define fallback locales like this (in config/application.rb):
# Custom I18n fallbacks
config.after_initialize do
I18n.fallbacks = I18n::Locale::Fallbacks.new(at: :"de-DE", ch: :"de-DE", gb: :"en-US")
end
You also need to set config.i18n.fallbacks = true in all config/environments/*.rb files.
I have a rails 3 app, and am trying to do testing. I do the following command rspec spec/controllers/ and get the following error:
/config/application.rb:42:in `<<': can't modify frozen array (TypeError)
from c:/Users/#####/documents/#####/config/application.rb:42
It is pointing to my config/application.rb file which I have provided below.
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
module PadOnRails
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# JavaScript files you want as :defaults (application.js is always included).
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
config.autoload_paths << "#{config.root}/lib"
end
end
has anyone else experienced this exact kind of problem. If so how do you fix it because I have deleted my project and did a git clone all over and still get the same error.
Try the method the comments suggest:
config.autoload_paths += %W{#{config.root}/lib}
This doesn't modify the initial array, but adds the given array to config.autoload_paths and saves it into a new array in config.autoload_paths.