I've installed rails_admin gem on my localized site (3 languages) and i need administration zone (/admin) to be always in English. Any idea how to do that? Maybe I can force locale for route?
Thank you.
I haven't used rails_admin but a quick scan of it's repo would indicate it's using whatever locale is set in you app. I'm guessing you set that in a before_filter in your application.rb via one of the methods outlined in the Rails i18n guide. You'll have to make that before_filter a bit cleverer. Perhaps something like:
if self.kind_of? RailsAdmin::ApplicationController
I18n.locale = :en
else
# Your current code
end
Related
Hope somebody can point me at the right direction with this...
Basically, i have the locales setup and it works fine. However, i need to depending on how the user gets to the site (example_company.com, example_company.cz or example_company.de..) have slightly different content(views and layout).
I've managed to boil it down to a constant or env variable that if i was to run multiple instances of the site(1 for each country), i could set on the server so that i get the behaviour i need with 1 code base.
My question is, how are people dealing with this in general? is there any way i can serve all countries on the same instance and set some flag based on .com or .cz or whatever, that dictates which 'version' they get without effecting the url itself?
I already have the locales in the url and would prefer not to mix the two as i will have to support multiple languages for each version. For example, french and czech would still support english.. But if i go to the french one, i will only show 2 locales (french and english)...
Hope i managed to explain properly.. if not let me know and i will try again.
If you use Rails' built-in i18n support, you can easily select locales by TLD.
From the official Rails i18n guide:
One option you have is to set the locale from the domain name where your application runs. For example, we want www.example.com to load the English (or default) locale, and www.example.es to load the Spanish locale. Thus the top-level domain name is used for locale setting. This has several advantages:
The locale is an obvious part of the URL.
People intuitively grasp in which language the content will be displayed.
It is very trivial to implement in Rails.
Search engines seem to like that content in different languages lives at different, inter-linked domains.
You can implement it like this in your ApplicationController:
before_action :set_locale
def set_locale
I18n.locale = extract_locale_from_tld || I18n.default_locale
end
# Get locale from top-level domain or return nil if such locale is not available
# You have to put something like:
# 127.0.0.1 application.com
# 127.0.0.1 application.it
# 127.0.0.1 application.pl
# in your /etc/hosts file to try this out locally
def extract_locale_from_tld
parsed_locale = request.host.split('.').last
I18n.available_locales.map(&:to_s).include?(parsed_locale) ? parsed_locale : nil
end
Be sure to read the i18n guide in full. It covers how to use the built-in i18n support. A big advantage is you don't need separate views for each locale.
I am using the 0.4.0.pre version of the routing-filter for Rails 4 at the suggestion of the person that posted that version in RubyGems. You will not find this version when you do a search on the RubyGems website. I found out about this when I posted a comment on an existing issue in GitHub.
https://rubygems.org/gems/routing-filter/versions/0.4.0.pre
I am finding differences between this version and the one I currently use in my Rails 3 applications. I believe it is the latest version 0.3.1. With this gem the locale is included in my URLs and in the debug info at the bottom when running localhost as expected. However when I use the 0.4.0.pre version the locales do not show up in URLs or the debug info. I have links at the top of the header section where a person can select their locale. When one of those links is clicked the locale appears in the URL. When I click another link (text or icon) the locale disappears but the locale is set in a cookie (a supposed no no but will keep it for now). The translations are correct for the selected locale but unless I do something special in my links for the person to know which one they clicked, no one will know the locale. This is a problem because for now I am setting up the locale files and all of them will be in English until I am able to get translations for the other languages.
Here is the code I have in both applications in application_controller.rb where I set the locale and create a cookie.
before_filter :set_locale
private
def set_locale
I18n.locale = (params[:locale] if params[:locale].present?) || cookies[:locale] || 'en'
cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale.to_s
end
Here is the code I have in config/routes.rb.
filter :locale
Here is the code for my locale line in my header in my Rails 4 application. I have similar code in my Rails 3.2.13 application.
<%= link_to_unless_current "English", locale: "en" %> <%= link_to_unless_current "Español", locale: "es" %> <%= link_to_unless_current "Français", locale: "fr" %>
I'm not sure if the issue is with the 0.4.0.pre gem or if I need to make additional i18n changes in my Rails 4 application. From reading the i18n documentation I'm not seeing anything additional I need to do. There is no specific documentation about 0.4.0.pre so I assume I should be able to do what I am doing with the Rails 3 version of routing-filter.
I have posted this in the issue section for the gem in GitHub. I have done online searches about using this version of routing-filter but cannot find them. I think it posted last month.
Any help would be appreciated.
It looks like the problem has been corrected the problem with the locale not appearing in the URL. However I ended up using the native Rails code for routing since I had two applications in production that I had already rewritten in Rails 4.
I have a question here, and i hope you can point me int he right direction...
I need to "hide" a certain portion of my site to people accessing to it from a certain language (for example, english) and i wasnt it to be visible for default locales.
can this be done?
Any idea?
I am on Rails 3, ruby 1.9.2
I18n.locale returns the current locale.
if :en == I18n.locale
# ...
end
I am new to ruby & rails and have started building an application.
My goal is to build this in a way I can easily translate the contents of the rails app and display the website contents in a locale preferred by registered user.
Appreciate any inputs on some of the best practices or references to any documentation to read, to build a web application that can be easily translated?
Thanks,
Krish.
Check out the Rails Internationalization (I18n) API. It does everything you've described.
Also check out Globalize3, it became a standard for model translations. Very useful.
You can use ready_for_i18n plugin that convert your erb to desired form.It saves some time.
You'd definetely watch this talk: http://www.youtube.com/watch?v=CTu4iHWGDyE
Here are some tips or best practices I noted while working through Internationalization of a Rails app. (It's possible that some of them are now outdated).
Before I get into a list here's some clarification between localization and translation that was helpful to me:
Definitions:
App localization and model translations are separate concerns. Figure out which one of those (or both) it is, that you need.
The way I use them here:
App localization: Localization your app to a locale.
Model translation: Translation your model/data into a language.
Example: Give me the french translation (model translation) of the resource in my Spanish site (app localization)
I use Globalize for model translations.
Helpful Tips/ Best Practices:
Some of the following are clearly just helpful tips while working in the Context of Rails + Globalize, some of them might be more than that... possibly best practices.
I18n.locale refers to and sets the locale of the app. When using Globalize, Globalize.locale refers to and sets the locale for model/data translations.
Set both I18n.locale and Globalize.locale on every request. Since these variables are set in Thread, it will avoid some hard-to-replicate bugs.
Set Globalize.locale to I18n.locale right after setting I18n.locale. This allows for model translations to default to the locale of the app. (On my Spanish site, I expect data to be in Spanish, by default).
Change Globalize.locale (and notI18n.locale) to change model translation.
Reset I18n.locale and Globalize.locale after every test. In rspec
RSpec.configure do |config|
config.after(:each) do
I18n.locale = :en
Globalize.locale = :en
end
end
Either use subdomain, or subfolder in the url to refer to the locale of the app.
Use a language parameter to specify the language of the data.
When you work on your rails app, you will probably use default_url_options to use I18n.locale as the default locale parameter for your route / path helpers. However this doesn't work in tests. I picked up a solution from this github issue on rspec-rails.
I monkey patch ActionDispatch::Routing::RouteSet like so:
class ActionDispatch::Routing::RouteSet
def url_for_with_locale_fix(options={})
url_for_without_locale_fix(options.merge(:locale => I18n.locale))
end
alias_method_chain :url_for, :locale_fix
end
Set up a translate helper t as a wrapper around the I18n.t method
module I18nHelper
def t string, options = {}
I18n.t string, options
end
end
RSpec.configure do |config|
config.include I18nHelper
end
This is mini-pattern I use. You can check it out: http://developers-note.blogspot.com/2012/01/rails-i18n-good-practice.html
in my rails3 app, i'm using devise for authentication
now i'm trying to do i18n. for that i saw a devise.en.yml in config/locales, and thought all i got to do is make an devise.ro.yml to translate it in my other language. but if the devise.ro.yml is present, everything is in ro, no matter the language i use
for changing language, i use a locale param in my urls. for that i set up in the application controller something like this:
before_filter :set_locale
def set_locale
I18n.locale=params[:locale]
end
def default_url_options(options={})
{ :locale => I18n.locale }
end
everything in my app is translated ok, except the devise part
am I missing something here?
I can suppose you do in top of you devise.ro.yml ro instead of en ?
First you need to generate Devise views with: rails generate devise:views, then you can translate each of those to suite your locale needs.
devise.en.yml is only for flash messages.
Devise views do not use i18n by default, from now on you can use https://github.com/mcasimir/devise-i18n-views to add I18n support to Devise views and mails.
(see https://github.com/plataformatec/devise/pull/1989)
devise.ro.yml should work.
as a side-note:
I had similar problems, and used this trick to debug / find out where/what Rails is trying to lookup for translations:
http://unixgods.org/~tilo/Rails/which_l10n_strings_is_rails_trying_to_lookup.html