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
Related
I am using devise_invitable gem in rails and I have configured a number of locales in my rails app. What I would like to do is to invite a user with a locale I would specify myself. I t could look something like this:
User.invite!(:email => "test#example.com", :locale => 'fr')
This would send an email with the 'fr' as locale , even when I18n.locale would be en.
Is this possible, even with a complete different syntax than the one I am using above ?
Digging, I found that devise_invitable uses the devise mailer, see source. I'm not 100% sure how I18n.t exactly works, but I suppose you know that. So use alias_method_chain to monkey-patch the translate method. To pass the other language, you can choose one:
modify the whole call stack to pass down the variable
use a pseudo-global variable via Thread.current[].
I propose to use 2., make your choice. Then use that variable to return the correct translation.
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
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
What I'd like to be able to do is:
in config/routes.rb
resources :posts
in config/locale/en.yml
en:
resources:
posts: "posts"
new: "new"
edit: "edit"
in config/locale/tr.yml
tr:
resources:
posts: "yazilar"
new: "yeni"
edit: "duzenle"
and get
I18n.locale = :en
edit_post_path(3) #=> /posts/3/edit
I18n.locale = :tr
edit_post_path(3) #=> /yazilar/3/duzenle
I'd also like Rails to match any of these routes anytime and pass the associated locale in the params hash such that when I navigate to /yazilar , the request should be routed to the posts#index action with the :tr locale in the params hash.
Any simple or complex way of doing that?
There is also the i18n_routing plugin by Guillaume Luccisano at
http://github.com/kwi/i18n_routing that solves most of those problems
however, it currently lacks translating the action names like ../new and ../edit. Guillaume says he will implement that "soon", though.
Update: Action name translating has been implemented in i18n_routing. Thanks Guillaume. :)
As the mentioned gems are no longer actively maintained, here are some more recent solutions:
gem route_translator
gem rails-translate-routes
both are successors of translate_routes
or a more powerfull (and complex) solution:
routing-filter
The translate_routes plugin by Raul Murciano provides some of the features you are asking for:
http://github.com/raul/translate_routes
It only works for Rails 2.3.x as far as I know though. But at least you can either get some ideas or even fork the plugin and make it work on Rails 3 yourself.
I'm using the auto_link method in Rails to turn urls in plain text to clickable links. It works pretty good most of the time.
but
google.com doesn't create a link
Is there something else out there that provides better coverage of urls? Or is there anyway I can easily modify the Rails code to include .coms and .net without a http or www start string?
auto_link uses AUTO_LINK_RE which you can override.
you can place a file in config/initializers
module ActionView::Helpers::TextHelper
silence_warnings do # you might need this to get rid of the warning
AUTO_LINK_RE = # your own definition here
end
end
It should work, but I never tried myself.