<s:date name="mydate" format="dd/MM/yyyy HH:mm" /> works perfectly for fomatting my date to the french standard.
But It's hard-written in my jsp. So I have to build if clause to switch according to the locale.
Is there a way to put this format to a general property file?
I tried:
format.date = {0,date,dd/MM/yyyy}
format.time = {0,time,HH:mm}
put in my global .properties but it's not taken into account when, I try just a <s:date name="mydate"> or a <s:property value="mydate"/>.
Create localized date format inside your properties files, e.g. with struts.date.format key:
struts.date.format = dd.MM.yyyy
And use getText method to get this date format in <s:date> tag format attribute:
<s:date name="date" format="%{getText('struts.datetime.format')}"/>
Absolutely. Internationalization (i18n) is handled out-of-the-box in almost every framework out there, then it's better to spend time on understand how to make it works than to write some unneded, buggy custom solution.
With Struts2, you must ensure to have:
I18nInterceptor in your Interceptor Stack;
I18nInterceptor defined in struts.xml: <constant name="struts.custom.i18n.resources" value="global" />;
the localized, correctly named global.properties file for each locale you want to handle;
the request_locale` parameter (if you have not changed the language in your browser) when calling your Actions, giving the I18nInterceptor the ability to hijack the request to the correct resource;
an appropriate character encoding on your pages: <%# page contentType=”text/html;charset=UTF-8″ %>.
Take a look at the guides available on the web too:
http://www.mkyong.com/struts2/struts-2-i18n-or-localization-example/
http://www.2bloggers.com/2011/12/localization-i18n-in-struts2.html
http://www.roseindia.net/struts/struts/struts2.2.1/tags/i18ntag.html
http://www.roseindia.net/struts/strutsinternationalization.shtml
http://www.roseindia.net/tutorials/I18N/internationalison-code.shtml
Related
Is it possible to apply Struts 2 Internationalization (i18n) for changing language from English to Hindi on whole web pages of website by single click?
If it is possible then how can I resolve this problem?
As mentioned by #RomanC , it is possible to do that, you need to have the i18n interceptor in your package.
After that you need your jsp page submit a form with request_locale in it, or call an action with request_locale in its parameters . For example, if you want to do it with select you can use below:
<s:form id="langselect" action="locale-manager" namespace="/common">
<s:select name="request_locale" headerKey="-1"
headerValue="Language"
list="#{'en_US':'English', 'fa_IR':'فارسی','ar_SA':'العربية','zh_CN':'中国的'}"
onchange="forms['langselect'].submit()" />
</s:form>
You form action needs nothing to do about changing the locale at all, as the interceptor will do all the job for you.
Changing the locale will change all your 20 pages at once, of course as soon as you reload your pages. So if you want to do it with tab you need to reload your new pages (for example with ajax) or reload your total site to get localized jsp from struts.
The framework is internationalized.
You need to add the corresponding to each locale resource bundles for
the localized messages that you display via the struts tags. Use
text tag or getText() to retrieve a message in the UI.
The browser language is passed with the HTTP request and framework
create a locale corresponding to the browser settings. Switching
locale performed via passing a special parameter request_locale to
i18n interceptor that should be on your stack.
You can also configure this interceptor to accept user-defined
parameters.
Normally switching to a locale persist for the session of the user.
So, you don't have to pass a parameter each time on every request, but
this case is also supported if needed. See how could you achieve all
of the above using localization.
I have added Resource.fr-FR.resx to my project and have done the globalization setting in
web.config as follows.
<system.web>
<globalization culture="fr-FR" uiCulture="fr"/>
</system.web>
that is all you need according to http://msdn.microsoft.com/en-us/library/bz9tc508%28v=vs.80%29.aspx
but when I run my app it is still in english
I have checked "Thread.CurrentThread.CurrentUICulture" in Application_Start() and it says FR.
what am I missing?
the same thing has been posted in
MVC 3 Setting uiCulture does not work
but no answers.
Culture is not the same thing as Language. A "culture" is a set of formatting rules for dates/times, currency, calendaring, and a few other things (like text Title Casing rules).
Setting a culture will not automatically localize your application (where localization is when all of the human-readable strings are translated into another language, such as French). Localization and UI translation is a long, painful and expensive process. ASP.NET does not do it for you.
If you want to localize your application, you need to ensure that all human-readable strings are stored in a resource file (.resx in .NET) and you have created "satellite assemblies" that contain translated strings for other languages. You then need to ensure that ever string displayed to the user in your application uses the Resources APIs (or IDE-generated helper classes). This is painful because it means going from this in your *.aspx files...
<p>Hello, welcome. This is in English.</p>
...to this:
<p><%= Resources.WelcomeMessage %></p>
...or this (if you have too many strings to be managed by the Resources helper class):
<p><%= ResourceManager.GetString("HomePage_WelcomeMessage") %></p>
...doing this, of course, breaks any visual-designers for your website, for example.
How to change the format of java.util.Date object in struts 2. What I have tested, Struts 2 accepts m/d/yy. Is it possible to change the format?
And also how to change the default message "Invalid field value for field date"?
date tag in struts2 can only be used in output.
You can pass format to s:date tag
<s:date name="myDate" format="yyyy-MM-dd" />
To change the default error message, in your validation.xml you can add message parameter
<field name="myDate">
<field-validator type="date">
<message key="errors.myDateRequired" />
</field-validator>
</field>
Then in your ApplicationResources.properties file you will provide value for errors.myDateRequired
Update: As per comment
s:date is only a read only tag which means cannot be used to accept input from user.
For enabling textField to accept date in different format, couple of approaches:
Use struts2-jQuery plugin, it has a very nice date picker which support different date timeformat, showcase, DatePickerTag.
Custom javascript code to format user input.
Use jQuery (if allowed) and use jQuery datepicker
You can use custom converter to use text field for date, example
The date format Struts accepts depends on the user's locale. So for different locales you can have different date formats. This way you won't be able to support multiple date formats for the same locale but I don't see why you would need this anyway, it's only going to confuse your users. The only way to support different date formats for the same locale without javascript would be with a custom date converter as #mprabhat notes.
You could see the official doc about this:
Define the format for every language y the resource bundle. For instance, an appropriate en_US format definition extension could look like this:
format.date = {0,date,MM/dd/yy}
In parallel, you could add the following to your de_DE bundle:
format.date = {0,date,dd.MM.yyyy}
http://struts.apache.org/2.1.6/docs/formatting-dates-and-numbers.html
I'm struggling with a Struts2 date formatting issue. If I understand correctly, type conversion in Struts2 is locale aware, and any form fields/parameters that map to Date objects should be strings formatted in their Locale specific SHORT format; the default output for a Date object on the value stack is also output as the Locale specific SHORT format (unless overridden with custom formatting).
Although form fields have worked fine with dates, when using the <s:url> tag I can't seem to get the <s:param> tag to encode date parameters correctly. When I try something such as this
<s:url action="foo" >
<s:param name="endDateParam" value="#endDate"/>
</s:url>
the result is pretty obviously not the SHORT format:
/foo.action?endDateParam=Sat+Jan+14+00%3A00%3A00+EST+2012
I re-read the Struts2 documentation but they mostly discuss creating custom date formats in the i18n'ized properties files, which doesn't seem like the right solution.
Any help with this problem would be greatly appreciated.
You can send it like that :
<s:param name="dateFrom">
<s:date name="dateFrom" format="dd.MM.yyyy"/>
</s:param>
You've probably already solved this by formatting a string in the action the way you need it. This I would advise first, unless you are supper stickler for model/view separation or there isn't a one to one mapping between the action and the view in which case this zealousness my be justified.
Lets say you felt the formatting wasn't the business of the action in that case you could use OGNL to it's full effect:
Here is an example that displays the current date (it uses new to construct a new date but you can very easily just replace "new java.util.Date()" with "endDate". It was constructed this way so anyone can just paste it into their JSP without any action dependencies.
<p>
<s:property value="#java.text.DateFormat#getDateInstance(#java.text.DateFormat#SHORT, #java.util.Locale#CANADA).format(new java.util.Date())"/>
</p>
NOTE: requires OGNL static method access to be true. Easiest way to do that is to add the following to struts.xml:
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
Using OGNL to this level is a bit suspicious but it is easy to read and the intention is clearly view/presentation related. Although it isn't that easy to construct... The easiest way is to write everything as one line of java and then apply ognl syntax rules which you would find here:
http://commons.apache.org/ognl/language-guide.html
Also for quick reference:
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html
I am trying to parse a bunch of JSP files and find places with hardcoded strings. E.g.
<h:outputText value="I am hardcoded" styleClass="someClass" />
<my:customTag value="I am hardcoded too" />
Currently I am using jsoup to do so. It seems great as an HTML parser, however if I make changes to the document and write it out to a file all of the case sensitive JSF tags and attributes are changed to lowercase. Are there any Java libraries that can parse a JSP file, let me modify some attribute values, and let me write out the JSP?
Or better yet is there a way to tell jsoup not to change the casing of my elements and attributes?
It may not be the case for other people, but it turns out replacing all of the toLowerCase() calls in jsoup worked well enough for me.