Reputation: 337
I have the existing web app (Spring MVC). I need to make i18n for all the contents.
The requirement for managing the language is to use language code directly in the URL. So for existing URL:
http://example.com/product1
I will need to create N virtual URLs like
http://example.com/EN/product1
http://example.com/FR/product1
I'm investigating the different option to achieve this. One dirty solution I found is to use the interceptor which will check the language code in the URL, removing the language code, to make a valid URL and set the Redirect. But it's so ugly...
What is the best approach to implement this?
Upvotes: 0
Views: 1827
Reputation: 18662
I believe all the possible ways to implement this will be ugly. That's just because the requirement does not make any sense (you're implementing the language switcher anti-pattern, aren't you?).
So much for my rant, the easiest way would be to use "classic" Spring MessageSources and capture the locale in your controller. Since I have no idea how you implemented your controllers, what view technology you're using and so on, let's say:
@RequestMapping("/")
public class SomeController {
@RequestMapping(value="{lang}/product1" method=RequestMethod.GET)
public getProduct(@PathVariable String lang) {
Locale locale = Locale.forLanguageTag(lang);
LocaleContextHolder.setLocale(locale);
return "product1";
}
}
This will set the locale and a view will be using them to resolve messages (provided it's internationalized).
The only problem with this example is, I haven't tested it and I am not quite sure it will work :) It's likely that you would have to re-organize the parameters, or to make matters worse use one controller for everything (which would be a terrible solution).
Upvotes: 1
Reputation: 13858
There will be multiple ways to do this and they will depend somewhat on your application scale.
Personally I can quickly think of
Assign multiple URL-mappings to the Spring mvc-dispatcher:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/EN/*</url-pattern>
<url-pattern>/FR/*</url-pattern>
<url-pattern>/GR/*</url-pattern>
</servlet-mapping>
If you have RAM to spare, you could simply deploy the application multiple times - once for each language context you want to support. This would allow replicating heavy-used languages on multiple servers and have low-volume ones otherwise.
Upvotes: 1