I have Thymeleaf template:
<p th:text="#{home.welcome}">Welcome to our grocery store!</p>
And I have several language bundles such as home_en.properties, home_fr.properties, etc.
Thymeleaf must take messages from appropriate bundle according to locale.
Where can I set this locale?
Retrieving locale from session, cookies or url query requires manual mapping of language and message bundle. Maybe there are automatic ways to map user's browser default language on message bundle?
Related
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
I am using Grails 2.0.3 in my project. I would like to implement internationalization to my application. As far as I read from this documentation I understand Grails have an out box support for internationalization. However I would like to override browsers Accept-Header setting and would like to set users language preference.
First I've created a filter in order to catch requests and check the language preferences. But it did not help. In filter I can get localized messages but when page is rendered I am getting English page. Here is the code that I use for setting locale.
def locale = new Locale("es", "ES")
java.util.Locale.setDefault(locale)
Then I've created custom LocaleResolver and injected that in spring configuration as localeResolver. Again in filter I can see localized messages however in pages still no luck?
Is there a way to override or bypass browsers setting in Grails i18n support?
The default LocaleResolver of Grails is SessionLocaleResolver. If you want to always use es_ES you can change this to FixedLocaleResolver.
beans {
localeResolver(FixedLocaleResolver) {
locale = new Locale("es", "ES")
}
}
If you want to restrict to a set of locales, then you will need a filter, and use the SessionLocaleResolver#setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) method.
I have a Grails web application running on a german localized machine.
How does Grails determine the language to use? From the webbrowser? From the systems setting? Is this changeable with altering the language used for the ui (eg with this one: http://grails.org/plugin/lang-selector)? If not, how is it changeable?
Reason is that (seldom) i have users that access the webapp via (english) terminalserver. And there is some strange behaviour with number input (comma, dot, ...)
From http://grails.org/doc/latest/guide/i18n.html
By default the user locale is detected from the incoming
Accept-Language header. However, you can provide users the capability
to switch locales by simply passing a parameter called lang to Grails
as a request parameter:
/book/list?lang=es
Grails will automatically switch the user's locale and store it in a
cookie so subsequent requests will have the new header.
Also you can configure the default locale as follows. Place
beans = {
localeResolver(SessionLocaleResolver) {
defaultLocale = new Locale("ru", "RU")
java.util.Locale.setDefault(defaultLocale)
}
}
in the resources.groovvy file
I am building a multilingual website with Symfon 1.4/Doctrine, having English language defined as primary.
Content translation in other languages are added gradually, and there will very often be situations when translation of the content requested will be unavailable in requested language. I want to display the requested content in English in those cases.
Is this achievable at global level, e.g. for all the i18n content?
As pointed by Grad van Horck, this works fine by default for interface translation.
What I need is the same functionality for content stored in DB (models having "actAs i18n" behavior).
The default way allows perfectly for this. Just make sure all your texts are in English by default, and translate them where needed. So just do <?php echo __('Hello'); ?>, and then translate it if you want. If a translation can't be found, it just falls back at it's 'original'.
To do the same in your database, you'd probably be best of copying the i18n behaviour (Template/Listener) and add your own bit of logic to fall back on English.
It's possible to set the default culture on sfDoctrineRecord (see http://trac.symfony-project.org/ticket/5458)
sfDoctrineRecord::setDefaultCulture('nl'); // default = 'en'
This changes the i18n fallback when the translation isn't available in the database.
hi all,
i want to make localization feature in a website written in struts 2. as far as i know, the standard way of doing so is using get in the following manner:
http://.../namespace/action_name?request_locale=<locale code>
however, my boss doesn't like such hairy url. instead, i'm required to write it in the following manner:
http://.../namespace/a_param/<locale code>/another_param...
i tried to change the action mapping in my struts.xml into something like
<action name="*/*..." ... >
<param name="locale">{2}</param>
...
</action>
it doesn't work
after i changed it into
<action name="*/*..." ...>
<param name="request_locale">{2}</param>
...
</action>
it doesn't work either T_T
by the way, i know there is trick of putting ActionContext.getContext().setLocale(new Locale(...)); in action which basically change the locale for that instance. however, it seems that the effect will only be transient (in contrast, i18n saves the chosen locale in session, which basically makes it quite persistent.
so, how to change the locale by embedding the locale code in the url?
Your help is highly appreciated =D
I have not done much with locals but i18n should automatically determine the correct local from the browser via the headers, there is no need for anything to be in the url. As long as there is a language bundle for the particular locale it will try to pull properties from that file.
This page shows an example of using basic i18n (only looked at it for a moment, personally I always start at http://struts.apache.org/2.x/ but the tutorial/guides are a bit dry.
Why do you need to refer to anything in the url at all concerning language? Personally if the user did want to override the default locale I would provide some form of control (menu) to do so. Then I would set a variable in session then I would create an interceptor which would call setLocale on the action using the local parameter on the session (if there is a value set of course). This way there would not be any need to embed parameters into individual pages and the local is out of the url all together.
There is a way to do what you want with the url... Something to do with conventions and slashes in allowing slashes in the action name I think. I'll post back if I remember. But I think the above is generally a better approach anyways.
Edit: Taking into consideration what you are trying to accomplish I can see two very different solutions.
1) You can use a proxy, the incoming URL www.example.com/en/ and www.example.com/fr/ can be mapped to different web applications or even the same web application but the url is re-written into a form that suites your application. Tools that can do this include: iptables, apache mod_rewrite, squid... and a multitude of others. This type of solution is more valuable if you handle multiple ip addressses/urls/applications on one server.
2) You can set the struts2 property struts.enable.SlashesInActionNames then using wildcards you can do something like:
<action name="*/*">
<result>/WEB-INF/content/{1}/{2}.jsp</result>
<action>
You can also pass parameters to actions each asterisk found in the action name becomes {1}, {2}, etc. It sounds like you might need this feature. If someone else knows it escapes me at the moment how you would capture parts of the url like this with struts2-conventions-plugin so the action can make use of them I would find that interesting.
#Quaternion
basically the intention is that, the website has several national "sub-website". based on user's ip address, he/she will be redirected to the national "sub-website". it's like when you open www.google.com, you may be redirected to www.google.com.country_domain.
each national "sub-website" has several languages, with 1 default language. it's just like when you open google israel website, by default you will see a website written in hebrew language, although you can override this default choice by choosing it to be in english.
in my planned website, following isreal website and hebrew language example, it is supposed to be like this:
the user is in israel
he is opening www.abcdef.com
the server is recognizing that the client is in israel. there are 3 languages can be chosen for the israel "sub-website": hebrew, arabian, english. the default one is hebrew, but client can override this choice
the user is then redirected to www.abcdef.com/il/he ("il" stands for israel country and "he" stands for hebrew language)
but the user is apparently a british tourist with no knowledge on hebrew or arabian language. so he/she chose english language
he/she will be redirected to www.abcdef.com/il/en ("en" stands for english language)
the next time that client opens www.abcdef.com in israel again (assuming the cookies & sessions are still around), he/she will be redirected to www.abcdef.com/il/en
thx fr your help =D
Definitely I would leave the responsibility to handle the Locale to an interceptor.
Here is a tutorial to Create an Interceptor.
This interceptor can be placed in a common stack shared by all (or most of) incoming requests and it will assign the locale
ActionContext.getContext().setLocale(locale);
with the proper logic that could take into account:
query-string parameters
stored user preferences
cookies
session
browser preferences (are in the request)