I currently have the following 4 files in my config/locales of my root application:
-en.yml
-de.yml
-simple_form.en.yml
-simple_form.de.yml
In my application.rb which resides in a spec/dummy folder for testing the application gem I have the following line of code which seems to be retrieving the translations as expected:
# 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
I now wish to introduce to structure to the file structure of my locales folder but when I add the additional folders and change the load path in the application.rb I am getting translation not found errors. Here is my attempt:
Attempt at including structure in config/locales of my root application:
-views
-en.yml
-de.yml
-models
-en.yml
-de.yml
-forms
-simple_form.en.yml
-simple_form.de.yml
And my load path in the application.rb changed to:
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
According to the following rails guide:
http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-domain-name
To test the host application you need to change the i18n.load_path to the config folder of your main app and not the dummy spec for testing purposes. Code as follows:
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.default_locale = :en
I had a similar issue, I solved it by adding this line to my application.rb config:
# load the subfolders in the locales
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]
The following options all worked for me
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.yml"]
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**' '*.{rb,yml}').to_s]
After restarting the webserver of course...
Want to mention. All solutions above also include files in config/locales directory again (first time rails add it by itself). It's not a problem becase values will be rewritten with the same keys. But if you want to include ONLY subdirectory files inside config/locales it is better to use
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*', '**', '*.{rb,yml}')]
Example.
My structure:
config/
locales/
en.yml
breadcrumbs/
breadcrumbs.en.yml
If you do config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] you add en.yml several times:
irb(main):001:0> Rails.application.config.i18n.load_path
=> ["/home/air/projects/qq2/config/locales/en.yml"]
irb(main):002:0> Rails.application.config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
irb(main):003:0> Rails.application.config.i18n.load_path
=> ["/home/air/projects/qq2/config/locales/en.yml", "/home/air/projects/qq2/config/locales/en.yml", "/home/air/projects/qq2/config/locales/breadcrumbs/breadcrumbs.en.yml"]
with Dir[Rails.root.join('config', 'locales', '*', '**', '*.{rb,yml}')]:
irb(main):001:0> Rails.application.config.i18n.load_path
=> ["/home/air/projects/qq2/config/locales/en.yml"]
irb(main):002:0> Rails.application.config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*', '**', '*.{rb,yml}')]
irb(main):003:0> Rails.application.config.i18n.load_path
=> ["/home/air/projects/qq2/config/locales/en.yml", "/home/air/projects/qq2/config/locales/breadcrumbs/breadcrumbs.en.yml"]
In config/application.rb:
module PointsProject
class Application < Rails::Application
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
end
end
From Rails's guide on Internationalization: http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-domain-name
Related
I try to config my default locale to fr........ but it dont work.
In my application.rb :
config.load_defaults 5.1
config.i18n.locale = :fr
config.i18n.default_locale = :fr
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
but I always have my app with params "locale"=>"en" where is the mistake ?
You need to set default_locale as you have, and also prsent available locales:
config.i18n.default_locale = :fr
I18n.available_locales = [:fr]
in your application.rb
In my engine 'Utilizer', I'm trying to test routes with Rspec 3.2.
Namespace are isolated
# lib/utilizer/engine.rb
module Utilizer
class Engine < ::Rails::Engine
isolate_namespace Utilizer
...
end
end
The engine is mounted to the dummy app:
# spec/dummy/config/routes.rb
Rails.application.routes.draw do
mount Utilizer::Engine => "/utilizer", as: 'utilizer'
end
To the spec_helper.rb I've added a couple of configures as below (from here):
# spec/spec_helper.rb
RSpec.configure do |config|
...
config.before(:each, type: :routing) { #routes = Utilizer::Engine.routes }
...
end
When I defined a route:
# config/routes.rb
Utilizer::Engine.routes.draw do
resources :auths, id: /\d+/, only: [:destroy]
end
Rake shows it properly for the dummy app:
$ spec/dummy > bundle exec rake routes
$ utilizer /utilizer Utilizer::Engine
$ Routes for Utilizer::Engine:
$ auth DELETE /auths/:id(.:format) utilizer/auths#destroy {:id=>/\d+/}
BUT both Rspec tests
# spec/routing/auths_routing_spec.rb
require 'spec_helper'
describe "Auths page routing" do
let!(:auth) { create(:utilizer_auth) } # factory is working properly by itself
describe "#destroy" do
let!(:action) { { controller: "utilizer/auths", action: "destroy", id: auth.id } }
specify { { delete: "/auths/#{ auth.id }" }.should route_to(action) }
specify { { delete: auth_path(auth) }.should route_to(action) }
end
end
fail with errors (for the first and second tests correspondingly):
No route matches "/auths/1"
No route matches "/utilizer/auths/1"
But, Holmes, why?
Since RSpec 2.14 you can use the following:
describe "Auths page routing" do
routes { Utilizer::Engine.routes }
# ...
end
Source: https://github.com/rspec/rspec-rails/pull/668
I've fount a solution in Exoth's comment at the end of this discussion on Github (thanks to Brandan).
In my spec_helper instead of
config.before(:each, type: :routing) { #routes = Utilizer::Engine.routes }
I use
config.before(:each, type: :routing) do
#routes = Utilizer::Engine.routes
assertion_instance.instance_variable_set(:#routes, Utilizer::Engine.routes)
end
and it works.
I have this app in which I use a bunch of locales (which are adjusted to be more suited to the domain of the app, ex: instead of using es-MX, I just use mx as locale)
And I have configured the fallbacks in application.rb
config.i18n.default_locale = :en
config.i18n.fallbacks = {
# sites
'cl' => 'es',
'mx' => 'es',
'lat' => 'es',
'br' => 'en',
'us' => 'en',
# lang
'es' => 'en',
'pt' => 'br',
}
And I set the locale by url ex: localhost:3001/cl (for chilean locale)
here's my code in app_controller
before_filter :set_locale
private
def set_locale
if supported_locale?(params[:locale])
I18n.locale = params[:locale]
end
end
And my routes
# public urls for sites
scope '/:locale' do
# index
match '/' => 'main#index', via: :get, as: :site
end
So, the thing is when I am in production I have localhost:3001/cl
and it calls the _logo.cl.html.erb partial and the locale printed in the console it's cl.
But the text are still in english. And in development everything works fine. Anyone has any idea about this?
I'll leave a couple of images
production/us
production/cl
development/cl
The thing was that the production.rb file define
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
overwriting the custom fallback rules that I had defined in application.rb and I just remove those lines and the problem was solved
I upgraded an app to Rails 3.2 and installed Bourbon on it. When I make a CSS change locally, for some reason I'm not seeing any change made. Only when I precompile the assets after making a change do I see it in development. Does anyone know what might be going wrong?
Here's my development.rb:
POP::Application.configure do
# Settings specified here will take precedence over those
# in config/application.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
# # GMail settings
# ActionMailer::Base.smtp_settings = {
# :address => "smtp.gmail.com",
# :port => 587,
# :authentication => "plain",
# :enable_starttls_auto => true
# }
end
And application.rb:
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 POP
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]
# 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 spec generators
config.generators do |g|
g.test_framework :rspec,
:fixtures => true,
:view_specs => false,
:helper_specs => false,
:routing_specs => false,
:controller_specs => true,
:request_specs => true
g.fixture_replacement :factory_girl, :dir => "spec/factories"
end
config.assets.initialize_on_precompile = false
end
end
Looks like a previous question I hadn't found had the answer. All I needed to do was run:
bundle exec rake assets:clean
I've updated active_admin to version 0.3.0 to get internationalization working. But I have problems with it.
I have my pl.yml file updated with activeadmin section which looks like this:
pl:
active_admin:
blank_slate:
content: "Nie ma jeszcze rekordów."
link: "Nowy"
dashboard: "Dashboard2"
view: "Podgląd"
This didn't work, so I tried adding this code to my application.rb:
config.before_configuration do
I18n.locale = :pl
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*', '.{rb,yml}')]
I18n.reload!
end
Now internationalization seems to work in development environment, but I still have problems in other environments. I have problem with dashboard: key. Normally, in short, when I18n doesn't find the key it puts key: with capital letter, in this example it would be "Dashboard". But in my case i have something like this:
Develoment:
Production:
Is there anyone who had the same problem? I'm I doing something wrong, or is this an activeadmin bug? Any solution?
I had the same problem. I needed to do this to be able to get it to work in both production and development:
config.before_configuration do
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
I18n.locale = :nl
I18n.default_locale = :nl
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
config.i18n.locale = :nl
# bypasses rails bug with i18n in production\
I18n.reload!
config.i18n.reload!
end
config.i18n.locale = :nl
config.i18n.default_locale = :nl
Not very pretty, but probably caused by a bug in Rails.
in application.rb
config.i18n.default_locale = :fr
I18n.locale = config.i18n.locale = config.i18n.default_locale
I18n.reload!
The key reason maybe caused by : Rails chose the locale from enduser's browser, but not your config file. e.g. a Japanese is visiting your website with his browser using English , then your Rails app will show him the "English" text, but not Japanese that you want it to show.
According to Rails i18n document: http://guides.rubyonrails.org/i18n.html, you have to first of all:
edit config/application.rb to set the default_locale
config.i18n.default_locale = :cn
edit your app/controllers/application_controller.rb, to add a before_filter
before_filter :set_locale
# for those user whose browser is not using our default_locale, e.g. a Chinese using English broser,
# just like me. :)
def set_locale
I18n.locale = params[:local] || I18n.default_locale
end
in this case, you should have the "cn" as the default locale.
check your view page, by adding these line of code to any of your page. e.g.
# in products/index.html.erb
<h1>Products List</h1>
default_locale is: <%= I18n.default_locale %> <br/>
current_locale is: <%= I18n.locale %>
the output result should look like:
Products List
default_locale is: cn
current_locale is: cn
and your Rails application should work as you expect.
An alternative that seems to work is creating an initializer with the following:
# config/initializers/i18n_reload.rb
Rails.configuration.after_initialize do
I18n.reload!
end