ActiveAdmin I18n.locale changed automatically on test development - ruby-on-rails

I going to uncomment before action on config/initializers/active_admin.rb
# config.before_action :do_something_awesome
But when I activate this default comment out, test failed for I18n.locale changing.
I'm using :ja but it looks change to en
config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
config.i18n.default_locale = :ja
test failed like this.
"XXX"=>"translation missing: en.xxx.yyy"
development environment looks like is not affected.
I don't understand why even if just uncomment below line.
config.before_action :do_something_awesome
Please help me if you know anything that cloud help me.
Environment Information
Rails 5.2.0
ActiveAdmin 1.3.0

Related

Locale fallbacks in Rails 4 not working

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.

Rails custom configuration returns an empty hash

I'm using Rails 4, and I would like to use the custom configuration functionality as explained here:
http://guides.rubyonrails.org/configuring.html#custom-configuration
I created the following YAML file (config\prefs.yml):
development:
password: test
And I added this to my config/application.rb:
module MyApp
class Application < Rails::Application
# ...
config.x.prefs = Rails.application.config_for(:prefs)
end
end
When I go to the rails console, I get this:
> Rails.configuration.x.prefs
=> {}
Why isn't Rails correctly loading the configuration?
I'm guessing the following:
You have the Spring gem bundled in.
Your custom configuration somehow got initialized in the state it is currently in. (i.e. empty)
The config\prefs.yml isn't tracked by Spring, so it doesn't know the environment needs to be reloaded.
If i'm correct, you'll just have to create an initializer with the following code:
Spring.watch "config/prefs.yml"
And, of course, you'll have to reload the console each time the config is changed. I've managed to reproduce and solve your issue with this, so i hope this helps.
I try your code on my machine and working fine i think maybe problem with configuration you did in config/application.rb or you need to reload your rails console with reload command reload!
that my configuration for 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 WSApp
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.x.prefs = Rails.application.config_for(:prefs)
end
end
that your file prefs.yml
development:
password: test
here is the result

Rails 4.2 - how to turn off I18n error `translation missing` on views

In our Rails 4.2 app views, there is I18n error whenever there is no key found in zh-CN file:
translation missing: zh-CN.no key
Here is in local.rb under config/initializers/:
I18n.default_locale = 'zh-CN' if Rails.env.production? || Rails.env.development?
We would like to turn off this error and prevent it from showing on views. There is a post about the error for Rails 3. However the solutions are not working with Rails 4.2. Also config.i18n.fallbacks = false/true does not do the trick.
You must add both a fallback locale and a fallback language.
config.i18n.default_locale = :en
config.i18n.fallbacks = true
This should force I18n to fallback to english. I use it extensively on a Rails 4.1 app, I'm not sure if something changed over it for 4.2 though.
We can config I18n callbacks in Rails application. For example, when zh-CN translation missing, I18n will fallback to en. Configuration in my Rails 4.2.2 application like below:
config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
config.i18n.default_locale = :"zh-CN"
config.i18n.fallbacks = true
config/initializers/i18n.rb
Rails.configuration.after_initialize do
I18n.fallbacks.map(:"zh-CN" => :en)
end
Reference doc:
https://github.com/svenfuchs/i18n/wiki/Fallbacks
http://web.archive.org/web/20151019133539/paulgoscicki.com/archives/2015/02/enabling-i18n-locale-fallbacks-in-rails/

Rails does not permit changing locale

I'm trying to change the locale that my rails (4.1.4) application is using, but every time I try to change it in the console using commands such as
I18n.locale = :es
I18n.default_locale = :de
an I18n::InvalidLocale error is brought up. This is the case for every locale I test, including regional ones such as :en-US. This is true even if I have a *.yml file set up for that locale. The only one that works is :en, which is the default for me.
I feel like this should be something pretty basic as all the resources I find seem to skip past this as a given, but it's not working for me and I can't figure out where to look for the problem source.
Did you add the new locale in application.rb ?
config.i18n.enforce_available_locales = false
config.i18n.available_locales = [:en, :es, :de]
config.i18n.default_locale = :de
You also have to create the files en.yml , es.yml and de.yml in your config/locale folder.
Thanks! When I looked in that file, I realized that in addition to the answer you gave, Rails provides it's own solution commented out -
# 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]
So basically it takes a translation file such as pt-br.yml and adds that to the list of valid locales.
Make sure you have added es.yml and de.yml to the config/locales folder.

Set language in Devise

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.

Resources