2014-07-07 53 views
0

我正在开发一个jsp web应用程序,它不是多语言的,但现在他们希望它能够根据浏览器语言环境更改用户界面语言。Java AOP或i18n的注释

因此,为标签和其他固定元素实现i18n很容易,但是我们做了一个仪表板框架,它有一些标准报告,标题,列标题和其他元素也被固定,但是从DB中检索到,并且不是所有的人都会被翻译。

我正在考虑用一些符号来解决这个问题,以指出哪个字段值是可翻译的元素。例如,也许使用一些#{resource.bundle.key.element}表示法。

那么,仪表板实体使用JPA。所以,我想知道是否有可能使用资源包进行i18n翻译,并且在从数据库检索值之后执行区域设置转换,而不执行侵入式编码,可能使用注释或AOP实现。

这样的事情可能吗?或者我可以使用另一种方法?

回答

0

国际化是一个交叉问题,因此是AOP的理想选择。使用AspectJ,你可以做这样的事情(简单,简单的例子,只是作为一个概念证明):

驱动程序:

package de.scrum_master.app; 

public class Application { 
    public enum Language { EN, DE, FR }; 

    public final Language language; 

    public Application(Language language) { 
     this.language = language; 
    } 

    public static void main(String[] args) { 
     for (Language language : Language.values()) 
      new Application(language).printStuff(); 
    } 

    private void printStuff() { 
     System.out.println("Hello world"); 
     System.out.println("This error message will not be translated."); 
     System.out.println("one"); 
     System.out.println("two"); 
     System.out.println("three"); 
     System.out.println("This error message will also not be translated."); 
     System.out.println("Goodbye"); 
     System.out.println(); 
    } 
} 

翻译方面:

请注意:静态翻译的东西应该存储在属性文件中。我知道这样很丑。

package de.scrum_master.aspect; 

import java.util.HashMap; 
import java.util.Map; 

import de.scrum_master.app.Application; 
import de.scrum_master.app.Application.Language; 

public aspect TranslationAspect { 
    private static final Map<String, Map<Language, String>> dictionary = new HashMap<>(); 

    static { 
     Map<Language, String> translations = new HashMap<>(); 
     translations.put(Language.EN, "Hello world"); 
     translations.put(Language.DE, "Hallo Welt"); 
     translations.put(Language.FR, "Bonjour tout le monde"); 
     dictionary.put("Hello world", translations); 
     translations = new HashMap<>(); 
     translations.put(Language.EN, "Goodbye"); 
     translations.put(Language.DE, "Auf Wiedersehen"); 
     translations.put(Language.FR, "Au revoir"); 
     dictionary.put("Goodbye", translations); 
     translations = new HashMap<>(); 
     translations.put(Language.EN, "one"); 
     translations.put(Language.DE, "eins"); 
     translations.put(Language.FR, "un"); 
     dictionary.put("one", translations); 
     translations = new HashMap<>(); 
     translations.put(Language.EN, "two"); 
     translations.put(Language.DE, "zwei"); 
     translations.put(Language.FR, "deux"); 
     dictionary.put("two", translations); 
     translations = new HashMap<>(); 
     translations.put(Language.EN, "three"); 
     translations.put(Language.DE, "drei"); 
     translations.put(Language.FR, "trois"); 
     dictionary.put("three", translations); 
    } 

    void around(Application application, String text) : 
     call(* *.println(String)) && this(application) && args(text) 
    { 
     proceed(
      application, 
      dictionary.get(text) == null ? text : dictionary.get(text).get(application.language) 
     ); 
    } 
} 

控制台输出:

Hello world 
This error message will not be translated. 
one 
two 
three 
This error message will also not be translated. 
Goodbye 

Hallo Welt 
This error message will not be translated. 
eins 
zwei 
drei 
This error message will also not be translated. 
Auf Wiedersehen 

Bonjour tout le monde 
This error message will not be translated. 
un 
deux 
trois 
This error message will also not be translated. 
Au revoir 

享受!