NullPointer getting internationalization text at LocalizedTextUtil - java

I have 3 language files idioma_CA.properties idioma_EN.properties idioma_ES.properties in the package "idiomas" and one default idioma.properties.
struts.properties has this properties:
hibernatePlugin.configurationType=annotation
struts.custom.i18n.resources = idiomas/idioma
So everytime I try to change the language struts2 ignores me and get always the text from the default properties at JSP's.
But using "getText" at execute method of my Action I get that NullPointerException whatever locale i have at request_locale.
Can someone tell me what am i doing wrong?
Thank you very much

I solved the problem.
The localization letters after "idioma_" must be lowercase like "idioma_ca.properties"

Related

Using the java.ext.dirs property causing currency to display incorrectly

I'm trying to implement my own locales through creating an extension and using the "java.ext.dirs" property, however I can't seem to figure out why the following is happening.
First without the property using the following code:
Locale swedishLocale = new Locale("sv", "SE");
NumberFormat swedishFormat = NumberFormat.getCurrencyInstance(swedishLocale);
System.out.println(swedishFormat.format(23));
Output:
23,00 kr
I then tried using the property "java.ext.dirs" to add my own extension and noticed that the currency output on an unrelated locale had changed to:
SEK 23.00
To test things out I tried specifying the "java.ext.dirs" property pointing to a completely empty directory, still the output had changed from the default:
SEK 23.00
Should changing the property be causing Java to behave this way (Affecting unrelated Locales), even if the property is pointing to an empty directory or am I missing something here?
Thanks.

Tapestry 5.4 core.properties override

I have a tapestry 5.4 project, and I want to override one element of the default core.properties file.
I tried to add to login_en.properties and login_hu.properties a new line (core-default-error-banner=...) but it do not override it.
Is there any way to overwrite it?
Thanks for the answers in advance.
If you intend to overwrite the header line of the Errors component, just specify your own message id in the page/component template like this:
<t:errors banner="message:your-translated-error-msg-id" />
To support other locales, just translate the built-in message catalog (property) files yourself: To have Tapestry use these new files, just put them in the corresponding package-named directory within your own app (for example, src/main/resources/org/apache/tapestry5/core.properties). More informtation can be found from Tapestry site.

Thymeleaf add custom data attribute with message resource value

I have a requirement where I need to insert the value to custom data tag using thymeleaf. The code for doing it using
data-th-attr="${data-custom=#messages.msg('test')}"
as well as
th:attr="data-custom=${#messages.msg('test')}"
I am unable to get the value in both the cases.
ultimately the parsing should be like data-custom="test"
here test is key for the value test in a properties file
By using the
data-th-attr="data-custom=#{test}"
or By using
th:attr="data-custom=#{test}"
helped me out, here test is the key for the value in message resource the issue was with the intellij IDEA IDE, it was having a bug that was showing me an unnecessary error.
Use th:attr="data-custom=#{key.for.message}" , this should work.
then after parsing the Expression,
data-custom="value.for.message"

How to set the Locale Value based on the country the user belongs to

Currently my J2EE Application supports these below countries
MessagesBundle_en_GB.properties (United Kingdom )
MessagesBundle_en_US.properties (United States )
MessagesBundle_it_IT.properties (Italy )
MessagesBundle_pt_BR.properties (Brazil )
MessagesBundle_sv_SE .properties (Sweden)
So i made a properties files for all these countries above and defined the Key value pairs in it .
I am using Resource Bundle for this concept .
And the way i will be accessing the key name is this way
bundle.getString("userName"));
bundle.getString("Mobile"));
Now my question is ,
How can i set the Locale value inside the JSP Page , because the user might belong any of the country as mentioned above
Please let me know , thank you very much
// This one is hardcoded , how can i set this dynamically ??
ResourceBundle bundle = ResourceBundle.getBundle("MessagesBundle", Locale.UK);
use ResourceBundle.getBundle(BUNDLE_NAME).getString(key); to access the Strings.
when updating the Default Locale e.g. via Locale.setDefault(<REQUIRED_LOCALE>); clear the Resourcebundle cache: ResourceBundle.clearCache();
the next call of ResourceBundle.getBundle(BUNDLE_NAME).getString(key); should the return the localized String of the chosen Locale.
The simplest way to do it is to implement HttpFilter. Call it for example LocaleHttpFilter. It should be mapped to/*` in your web.xml, so it will be called every time the request arrives to your application.
The filter will discover your request and decide what should be the current locale. It may base its decision on URL parameters, HTTP headers, GeoIP lookup etc. Once it decided about the locale it should call:
Locale.setDefault(locale)
Then you can use
ResourceBundle.getBundle("MessageBundle").getString("hello");
at any place in your code. This line will return value of string "hello" according to the current locale that was set into the filter.
First thing first: you may use ResourceBundle.getBundle(Locale) in your back-end code. However, you should never use this in the JSP Page directly. You should use JSTL instead. Now, let's get into details.
There are two reasons why it is not necessary the good idea to use ResourceBundle directly. One is related to this:
<%
try {
ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
} catch (MissingResourceException mre) {
// LOG THIS!
}
%>
This looks pretty ugly, doesn't it? That's because you have to beware of MissingResourceException that will be thrown if there is no bundle for base name you are looking for. To make matters worse, the same exception might be thrown if there is no key in the given scenario:
rb.getString("key");
So you also need to take this into account:
<%
try {
rb.getString("key");
} catch (MissingResourceException mre) {
// LOG THIS!
}
%>
How does it look?
Of course you can derive from ResourceBundle and override these methods so they won't throw an exception, but this is substantially more work than just this:
<fmt:setLocale value="fr_CA" scope="session"/>
<fmt:bundle basename="com.taglib.weblog.Greeting">
<fmt:message key="com.taglib.weblog.Greeting.greeting">
This is the reason you should use JSTL with JSP. Read more about how to use JSTL for i18n in this article.
Now, your original question was about language negotiation (W3C term), or Locale detection if you prefer. How to do that in JSP application?
The easiest and most typical scenario is to read the contents of HTTP's Accept-Language header. In Java Servlet world that means calling ServletRequest's getLocale() or getLocales() method and assign to variable in HttpSession object, which is accessible from JSP page. If you wonder how to access HttpSession on the servlet side, there is a getSession() method.
That works if you have direct access to Servlet. If you don't you need to create (or assign existing) Locale filter which will do all that for you. As you may imagine, this is fairly common scenario. That's the reason people already written (long time ago) necessary classes. You can find in few frameworks, let me notably mention Spring Framework.
I know it might sound strange, but if you are looking for simple solution, learning and using common web framework (i.e. Spring MVC) is better that re-inventing the wheel. I know that learning curve might be a bit steep, but it is worth it.

I18n - JSF variable value translation

I am using Bundle Internationalization in my project. I have initialized bundle via
<f:loadBundle basename="ui.all.bundles.AppResources_en" var="msg"/>
When i need to translate some text, i am using a key to resourceBundle, to get a value of it, for example: #{msg.someText}. But, now i want to translate text, which key is a value of another variable. For example:
I have variable String textToTransl. It`s value is status_booked. In my AppResources is defined, that status_booked means "It is booked!", so, when i am pointing it to #{msg.textToTransl} i need to see "It is booked!"
How can i make it work?
Use:
value="#{msg[currentDoc.textToTransl]}"
ui.all.bundles.AppResources
Don't include the locale (_en) in the resource name. The resource bundle code should resolve the locale-specific bundle based on the request locale.

Categories