From dncastilho at gmail.com Thu Dec 18 08:16:52 2008 From: dncastilho at gmail.com (Danilo Castilho) Date: Thu, 18 Dec 2008 11:16:52 -0200 Subject: [gettext-u-en] manipulating languages on the fly Message-ID: <26f1ede40812180516x2d4b6009y1576276e4a4a4b7b@mail.gmail.com> Hi all, I've an interesting problem to solve. In my website, an user can trigger an action that will mail another user, that can have a different language. In consequence, I have to translate the email using a different locale. One way to do that is to use the " set_locale 'langcode' " method, as the gettext tutorial suggests. The mailer uses a rhtml as the mail template, so here is an example: ==== mail.rhtml ===== <%set_locale(@receiving_user.lang)%> <%=_('Hello World')%> ================= But if I do that, every user that is browsing that instance of my rails server is going to see the screen translated in the @receiving_user.lang - starting on the next screen refresh. I've found a (not perfect) workaround. If I set the locale to nil in the end of the mail rhtml this problem won't happen, since all the users will continue to use the language in their session/cookie (:lang parameter). ==== mail.rhtml ===== <%set_locale(@receiving_user.lang)%> <%=_('Hello World')%> <%set_locale nil%> ================= That works fine in my current non-thread-safe-rails2.1.2 environment. But that is not an acceptable solution, since we are walking towards the thread-safe state in rails 2.2 and ruby 1.9 / jruby. Does anyone have any suggestion (a better solution)? Also, I assume the next gettext release will be compatible with rails 2.2 and, therefore, will be thread-safe. In other words, it will not have this problem. Is that a good assumption? regards, Danilo. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mutoh at highway.ne.jp Thu Dec 18 10:37:10 2008 From: mutoh at highway.ne.jp (Masao Mutoh) Date: Fri, 19 Dec 2008 00:37:10 +0900 Subject: [gettext-u-en] manipulating languages on the fly In-Reply-To: <26f1ede40812180516x2d4b6009y1576276e4a4a4b7b@mail.gmail.com> References: <26f1ede40812180516x2d4b6009y1576276e4a4a4b7b@mail.gmail.com> Message-ID: <20081219003710.1fcc075d.mutoh@highway.ne.jp> Hi, This is a bug. As you think, a request should have a new locale. --- rails.rb.org 2008-12-19 00:30:31.000000000 +0900 +++ rails.rb 2008-12-19 00:31:11.000000000 +0900 @@ -96,6 +96,7 @@ def init_gettext_main(cgi) #:nodoc: cgi.params["lang"] = [params["lang"]] if params["lang"] set_cgi(cgi) + set_locale(nil) set_locale_all(nil) end The last line (set_locale_all) may be needless. Next gettext(on Rails2.2) will work well with multi-thread (a thread has a locale). On Thu, 18 Dec 2008 11:16:52 -0200 "Danilo Castilho" wrote: > Hi all, > I've an interesting problem to solve. In my website, an user can trigger an > action that will mail another user, that can have a different language. In > consequence, I have to translate the email using a different locale. One way > to do that is to use the " set_locale 'langcode' " method, as the gettext > tutorial suggests. The mailer uses a rhtml as the mail template, so here is > an example: > > > ==== mail.rhtml ===== > <%set_locale(@receiving_user.lang)%> > <%=_('Hello World')%> > > ================= > > > But if I do that, every user that is browsing that instance of my rails > server is going to see the screen translated in the @receiving_user.lang - > starting on the next screen refresh. I've found a (not perfect) workaround. > If I set the locale to nil in the end of the mail rhtml this problem won't > happen, since all the users will continue to use the language in their > session/cookie (:lang parameter). > > ==== mail.rhtml ===== > <%set_locale(@receiving_user.lang)%> > <%=_('Hello World')%> > <%set_locale nil%> > ================= > > That works fine in my current non-thread-safe-rails2.1.2 environment. But > that is not an acceptable solution, since we are walking towards the > thread-safe state in rails 2.2 and ruby 1.9 / jruby. Does anyone have any > suggestion (a better solution)? > > Also, I assume the next gettext release will be compatible with rails 2.2 > and, therefore, will be thread-safe. In other words, it will not have this > problem. Is that a good assumption? > > regards, > Danilo. > -- .:% Masao Mutoh