Displaying text in differnt locales on one page - grails

I need to display sentences in my GSP in differnt locales.
The following documentation states that g:message takes a locale param.
I can't find an example of this anywhere. Has anyone done this?
<p><g:message code="welcome.into.text1" locale="sv_SE"/></p>
<p><g:message code="welcome.into.text1" locale="en_US"/></p>
Obviosuly, I have messages_sv.properties & messages_en_US.properties.
Thanks

To specify locale explicitely use Locale object instead of String, e.g.:
<g:message code="welcome.into.text1" locale="${Locale.US}"/>

http://grails.org/doc/latest/guide/i18n.html
Grails how to change the current locale

Related

using 18n and I18n:Alchemy in rails in order to translate a text value according to the locale

In my application, there is a field credit_debit that has two string values: 'credit' and 'debit'.
These two terms should be shown in a localized form to the user, so that when the app is using an italian locale they should be translated into 'avere' and 'dare'.
I could write my own setter/getter method for the credit_debit field and handle this issue inside it, but maybe I could leverage the I18n architecture to do so in a way that is consistent with the rest of the localization. If it's possible, how can I implement this?
Can't see the problem if you're already using i18n localization.
Just use the field as a key for i18n (with a prefix if you like)
t("prefix_#{credit_debit}")
and just define the strings for both your locales.
So in your en locale:
prefix_credit: credit
prefix_debit: debit
And in your it locale:
prefix_credit: avere
prefix_debit: dare

Groovy gsp grails number format replace

I have a German customer, he wants some numbers on his page to be
4,3
instead of
4.3
as I cant set the Number format globally for the project or controller because i have javascript depending on the english format, I'm looking for a simple replace method in gsp something like
${number.replace(/./,",")}
but that causes
No signature of method: java.lang.Double.replace() is applicable for
argument types: (java.lang.String, java.lang.String)
any idea ? thanks in advance
Instead of doing string conversion stuff, do it like this:
${formatNumber(number: number, locale: Locale.GERMAN, format: '##0.00')}
take a look at
http://grails.org/doc/2.3.x/ref/Tags/formatNumber.html
You should be able to format the number properly using the locale.

How to localize no-script (iframe) reCAPTCHA?

I know that to localize the regular (JavaScript) reCAPTCHA I can provide lang attribute to Recaptcha.create method as specified in documentation I've tried to do the same for noscript version by passing lang in query string like this http://www.google.com/recaptcha/api/noscript?k=<key>&lang=ru, but I still got an English version.
Does anyone know how to localize the iframe version?
try using &hl=ru parameter. hope this help.

Hiding locale parameter in rails 3.1?

i want to implement the following behaviour:
if the users browser-language is e.g. EN, he should be redirected to a the url http://foo.bar/hello, if the browser-language is DE then to http://foo.bar/hallo.
so how do i need to set my routes to redirect the user to the right language (e.g. when an english user requests the DE route and vice versa) and how can i set a hidden locale parameter, so i can load the right view in the controller?
i want to use the same controller for both languages (one method per page), but localized views (foo.en.html.erb etc.)
thanks in advance!
I don't think that what you want to try to get is a good idea, and I will explain that here. I don't understand why you would choose a different approach from the ones that are provided by Rails out of the box, and explained in details in the "Internationalization Guide, Sections 2.3 and further".
Here are the arguments:
Rails provides at least 3 different ways to change the locale:
Getting it from parameters: http://my.example.com/books?locale=de
Getting it from the sub-domain: http://de.example.com/books
Client supplied application, like the accept-header
All have the advantage that the controller and action will be the same, which is what you normally want to have.
There are helper methods if you want to change your behavior depending on the locate: locale, ...
However, you may localize views as a whole if you want to do: see localized views. This will perhaps lead to a duplication in view code.
Or you use the translation and localization API as in I18n.t 'store.title' or I18n.l Time.now.
A hidden locale parameter has the disadvantage that it is not obvious (for the user) which locale is used.
If you need to translate your routes in addition to set the locale, you may have a look to the translate_routes gem.
The README explains how you can set the locale from your translation hello/hallo.

Rails i18n strings auto-lowercased?

I've noticed that in my new Rails 3.0 application all German i18n strings are converted to lowercase (except for the first letter).
When having a string like this:
de:
email: "E-Mail"
the output is always like "E-mail". Same story with all the other strings - uppercase letters within a sentence are auto-converted to lowercase.
Is this default behaviour that I have to disable, or is there any other problem? I have successfully set the locale correctly, as these strings to actually work.
Thanks for your help
Arne
There should be no modifications to the content you specify as part of the internationalization process. It sounds like something is calling humanize on the string before it is output. Some of the standard Rails form helper methods do this I believe. If you just output the translation using t('email') you should see 'E-Mail' correctly.
Update:
From your comments it seems like it is a label that is causing the problem. If you explicitly specify the text for the label rather than relying on the default behaviour you will get the translation exactly as you specify. So,
<%= f.label(:email, t('email')) %>
should generate the correct label from the translations.
However, it isn't ideal. I think you may also run into problems with the generated validation error messages.
Got the same issue. solved it by adding the _html suffix to the I18n translation key. it seems that using this suffix suppresses the humanize usage.

Resources