How to handle different locale value in spring 3.0 webflow - spring-webflow

I want to handle the different locale value, based on that I should display the error messages in jsp pages
how to handle different locale value in spring webflow with jsp ?
Below code snippet is, from one of my java controller file,
**msgContext.addMessage(new MessageBuilder().error().source("register")
.code("createAccountForm.emailAddress.alreadyexists").build());**
And the below is one of my jsp file, where I am displaying the above error message as follows,
**.......
<spring:hasBindErrors name="createAccountForm">
<c:forEach var="error" items="${errors.allErrors}">
<div style="font-weight:bold;color:#ff0000;"><c:out value="${error.defaultMessage}"/></div></c:forEach>
</spring:hasBindErrors>
........**
Now, I want to add 2 more property files for error messages for different locale values.
In this scenario how can I add the locale value ? [I will get the locale value in the above java file], so that based on the different locale value, I should display the error messages in my above jsp file ?

You simply create the appropriately named properties files, e.g. messages_en.properties for English, and have something configured or coded to set the current Locale.
See Working with Messages.

Related

ASP.NET MVC bundles causing errors in W3C HTML validation

I do use ASP.NET MVC 5 and I include my JavaScript files with bundles. ASP.NET then generates a file name with a hash value at the end of the file. Between the filename and the hash value is a '=' character. When I validate my HTML code at W3C I get the following error:
= in an unquoted attribute value. Probable causes: Attributes running together or a URL query string in an unquoted attribute value.
I am not sure if there is an option to fix this error. Does anyone have any idea how to fix this bug?

NLog Web - AspNet-User-Identity without domain

I'm currently using the NLog.Web package for writing my .Net logs in my application.
After reading the NLog.Web I've noticed that unlike the ${windows-identity} layout renderer, the ${aspnet-user-identity} layout renderer got no domain parameter for it.
For example, if I want to log the current running windows identity, it logs out: domain\user, but when specifying domain=false, it logs only user.
How do I implement this kind of ability with the ${aspnet-user-identity}? Because when I configured ${aspnet-user-identity:domain=false} it didn't work.
The WindowsIdentity.Name, used in NLog, will always give the full name, including domain.
The logon name is in the form DOMAIN\USERNAME.
https://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.name(v=vs.110).aspx
I think you need a custom layout renderer, and split it by-hand on the /.
Something like this: (maybe also add soms checks for outOfIndex)
using NLog.LayoutRenderers;
....
//register ${my-aspnet-user-identity}
LayoutRenderer.Register("my-aspnet-user-identity",
(logEvent) => HttpContext.Current?.User?.Identity?.Name?.Split('/')[1]);
Register it as soon as possible.
I found a different way to solve this issue #Julian
in the NLog.config file, i created a variable:
<variable name="aspnetIdentity" value="${replace:searchFor=^\\w+\\\\:replaceWith=:regex=true:inner=${aspnet-user-identity}}" />
As defined in the variable, the regex searches for at least one word (at the start) and finally searching for a backslash.
the other backslashes are written to escape special characters and also double backslashing. Finally, what was found (it's the domain name) will be replaced with an empty string and therefore I got only the username and not the Domain\Username
Thanks for the help #Julian

How to handle I18n when texts are not in .properties files but in the jsp page itself

I'm building a multi-languages application with Spring MVC.
So far I handled the multi-languages system with the Spring class ReloadableResourceBundleMessageSource and .properties files. It was easy since texts were very short.
Now, I have to translate the body of the page and I can't rely on .properties files.
I have an Italian version of the page and an english version of the page. My doubt is: how should I handle it?
I thought that after the #Controller return the page name, for example "index", I should have a filter that check the application Locale and then add to the page name a suffix. So, the filter must turn "index" into "it/index" or "en/index".
IS it a good way to solve the issue?
Thank you.
Here's a suggestion with one drawback: I didn't test it with .jsp but .vm. The idea might still work.
As not to break the i18n mechanism put a message key, say parseContent in every language.property file. Now, make a view for every locale and name them, say parse_en_US.vm, parse_de_DE.vm and so on. These files must only contain what you wouldn't want to be in the language.property files.
Example of an entry in messages_en_US.porperties might be parseContent = parse_en_US.vm
An now use #springMessage('parseContent') to get the right view name depending on the present locale. This view you parse as a sub-view and problem solved.
For .vm it looks like this:
#set($view = "#springMessage('parseContent')")
#parse($view)
Same number of .vm files, but no need to invent sth new.

How to customize/localize Assert messages in Symfony2?

When you use #Assert\NotBlank constraint and the given field is empty, then you get the error:
This value should not be blank
I would like to change this message application-wide, without changing Symfony2 source code. How to accomplish that?
Cutomizing validation error messages is quite simple, but can seem tricky at first.
Default locale
First of all you should change the default locale of your application. In versions 2.0.x the correct value to change is framework.session.default_locale. For future reference, starting from 2.1.0 it'll be framework.default_locale. Consult the docs for correct syntax.
A locale should consist of your language and region and is defined as language_REGION (list of languages, list of countries). The locale used in Germany for german would be de_DE for example.
Validation messages
Validation messages are hard coded in their respective constraint classes.
Translating validation messages
Symfony uses Twig to render all the validation messages. The process itself is complicated and falls out of the scope of this question, but the important part is that each constraint message is sent through a translation filter, which depending on the user's locale (default_locale by default) translates the messages to the proper language.
To change any of the translations, simply copy the validation translation file from vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/translations/validators.{lang}.xlf to app/Resources/translations/validators.{lang}.xlf where {lang} is the language part of your default locale.
Having done the former, simply change the default messages to what ever you see more fit. If the language you need support for doesn't exist, copy any translation file to the same directory and modify that file instead.
To read more about how translation works in Symfony2, visit the official documentation on translation.
Additionally to the instructions by gilden, you have to make sure the framework.translator block in config/config.yml is uncommented (it's commented by default nowadays). If you don't do that, you'll still end up with the original English messages.
If anyone has doubts in 2021, here's all I had to do to switch "Error" (eng) constraint message to "Erreur" (fr):
Go to your config/packages/translation.yaml file
Edit it by replacing "en" to "fr" (or your language) :
Test it on your localhost :
Note :
here is the Symfony 5.2 doc I used to help me out
All I did was set a Default_locale (!! not detect where the user comes from !!)

Character encoding issue with Spring MVC and HTML form

I'm working with spring mvc. I've set up a web form that has two simple text inputs. On controller, I use #ModelAttribute to let spring build the bean from the web form.
The problem comes when user puts on those text fields specials characters, like 酒店 and this kind of stuff, spring doesn't read it as utf-8, and they become the usual bad-encoded string.
I've checked web.xml and there's the utf-8 encoding filter, all pages are marqued as utf-8 and browser is sending right charset headers. Any idea on what's going on?
You may want to check this out:
http://forum.springsource.org/showthread.php?81858-ResponseBody-and-UTF-8
The short of it is that if you are using annotated methods then the messageconverter being used has a default character set. You can change this setting in your web.xml by setting the supported media types.
However, if your service doesn't like that media type, you may get a 406 error. You can create your own string message converter and set the default encoding, but there is no easy way with the built in HttpStringMessageConverter.
Alternately you can re-encode a string to a different character set:
String newresponse = new String(responseString.getBytes("ISO-8859-1"), "UTF-8");
You may also want to check out the related question here: How to get UTF-8 working in Java webapps?
the solution is simple by add produces = "text/plain;charset=UTF-8" to request mapping you can force spring mvc to encode the returned text.

Resources