Comments (4)
Hi @armandsar,
I tried to reproduce without success. I put that piece of code in a controller and it worked as expected:
def my_action
I18n.locale = :fr
puts _("Assigned")
I18n.locale = :en
puts _("Assigned")
I18n.with_locale(:fr) do
puts _("Assigned")
end
I18n.with_locale(:en) do
puts _("Assigned")
end
end
The output in the console was :
Assigné
Assigned
Assigné
Assigned
I tried in development with config.action_controller.perform_caching
as true
and false
(we never know ^^). It was in a Rails 4 application.
What exactly is your stack so I could try to reproduce with the same one. Does it break also in development environment or only in production?
from rails.
I just tested the same example on a Rails 5.0.6 application and it worked too.
from rails.
Simplest bit of code i got to replicate this:
class TestController < ActionController::Base
before_action :set_default_locale
def index
a = []
a << [_('Dashboard'), I18n.locale]
I18n.locale = 'lv'
a << [_('Dashboard'), I18n.locale]
render json: a
end
private
def set_default_locale
I18n.locale = 'en'
GetText.locale = 'en'
end
end
Remove "GetText.locale = 'en'" line, reload page, translation still wrong.
Restart the server without gettext line and all is fine.
I now do realize that there is no need to set locale for GetText directly and I didn't really notice this until now, as this was implemented by another developer.
Still the behavior seems strange, given that we do call "I18n.locale = 'lv'" in controller action later..
I guess this can be closed :)
from rails.
I found out why it wasn't working: https://github.com/ruby-gettext/gettext/blob/master/lib/gettext.rb#L261-L282
GetText.locale=
is the same as set_locale
and fix the locale once for all the application (see the comments).
Gettext.current_locale=
is the same as set_current_locale
and is more flexible (many locales in the application).
This comment says all: # Note that if #set_locale is set, this value is ignored.
I tried your example with `current_locale=" and it's working. But as you noticed, Translation gem takes care of that for you so you don't have to worry about configuring the GetText locale: https://github.com/translation/rails/blob/master/lib/translation_io/railtie.rb#L21
from rails.
Related Issues (20)
- Special characters in translation HOT 2
- Isn't it possible to only use gettext? HOT 2
- Deprecation warning for #has_rdoc (Rubygems) HOT 2
- Do not add methods on Object HOT 3
- Empty hash in localization.en.yml
- Strings not get synced
- Unclear how to use custom locales in project HOT 5
- Crash on "rake translation:init" HOT 3
- getText method not translating text as supposed to after install and intialization HOT 7
- Deprecation warning on Rails 6 HOT 4
- Need a suggestion on how to keep the source pluralization clean HOT 2
- Error while running `translations:sync`: ArgumentError: comparison of Pathname with String failed HOT 3
- When disabling gettext in the initialiser gettext directory and files are still created HOT 3
- gettext strings missing after use of case statement in erb HOT 20
- A way to exclude translation keys HOT 3
- Ruby 3.1 compatibility (Psych 4 breaking change) HOT 7
- Match I18n fallbacks in GetText HOT 2
- ignore specific files HOT 4
- Error when starting Pushion Passenger HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rails.