问题
I am using Struts2 Internationalization to support English & Arabic.
I want to know that is it possible to dynamically or automatically change the position of label which comes before any input (text, date, etc.) when the locale changes?
E.g.
LTR -> English -> First Name - "Input Text Box"
RTL -> Arabic -> "Input Text Box" - أول اسم
Any possible solution for this?
BR SC
回答1:
Either (ab)use java.awt.ComponentOrientation
String direction = ComponentOrientation.getOrientation(locale).isLeftToRight() ? "ltr" : "rtl";
(its source code is pretty trivial though, there are only 4 languages which are RTL, here's an extract of relevance)
public static ComponentOrientation getOrientation(Locale locale) {
String lang = locale.getLanguage();
if ("iw".equals(lang) || "ar".equals(lang) || "fa".equals(lang) || "ur".equals(lang)) {
return RIGHT_TO_LEFT;
} else {
return LEFT_TO_RIGHT;
}
}
Or put it in a recourcebundle yourself.
this.direction = ltr
And read it as follows
String direction = bundle.getString("this.direction");
Either way, you can make use of this to change the direction accordingly.
<html dir="${direction}">
which would end up as <html dir="ltr">
or <html dir="rtl">
in generated HTML.
回答2:
Not as far as I know.
Messages can be localised and have arguments to handle currencies symbols and the like, but I have not seen any ready-made available framework that can handle left-to-right and right-to-left ordering of page.
My guess is that you have to serve pages, parts of pages using tiles or write the if else statements by your self.
来源:https://stackoverflow.com/questions/4659263/struts2-internationalization-java