2012-07-15 4 views
0

내 응용 프로그램에는 english와 german에 대해 nameEn 및 nameDe가있는 엔터티가 있습니다. 그러나 영어 만 사용됩니다. 사용 가능한 엔티티가 너무 많기 때문에 선택한 언어 항목을 반환 할 수있는 제네릭 클래스를 갖기를 원했지만 여러 항목에 대해 내 접근 방식이 작동하지 않았습니다.db의 다중 언어 지원 데이터

@Entity 
@Table(name="employee")  
public class Employee implements java.io.Serializable { 
    // Fields 
    private Integer id; 
    private String nameEn;  
    private String nameDe;  

    //Getter, Setter Methods 
} 

@Entity 
@Table(name="address") 
public class Address implements 
    java.io.Serializable { 

private String descriptionEn; 
private String descriptionDe; 
} 

public interface ILabelText { 
    String getNameEn(); 
    String getNameDe(); 
    String getDescriptionEn(); 
    String getDescriptionDe(); 
} 

public abstract class LabelText implements ILabelText, Serializable { 

private static final long serialVersionUID = 1L; 

protected String descriptionEn; 
protected String descriptionDe; 


private Logger log = Logger.getLogger(LabelText.class); 
String language = FacesContext.getCurrentInstance().getViewRoot().getLocale().getLanguage(); 


public String getDescription() {   
    log.info("Language Selected is " + language); 
    if (language.equals("De")) { 
     return getDescriptionDe(); 
    } else { 
     return getDescriptionEn(); 
    } 
} 

    public String getName() {  
    log.info("Language Selected is " + language); 
    if (language.equals("De")) { 
     return getNameDe(); 
    } else { 
     return getNameEn(); 
    } 
} 
} 


//In Xhtml, based on selected locale, display value accordingly 
<h:outputText value="#{emp.getName()}" /> 
<h:outputText value="#{add.getDescription()}" /> 
+1

당신의 스레드에 매우 친숙한 것을 읽어주십시오 - http://stackoverflow.com/questions/19295/database-backed-i18n-for-java-web-app –

답변

1

당신이

@Entity 
public class Lang implements Serializable 
{ 
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private long id; 

    @NotNull 
    private String key; 

    @NotNull 
    private String translation; 
} 

같은 엔티티 Lang을 만들고 JSF에서 올바른 언어를 액세스 할 수있는 그런 다음이

@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER) 
@MapKey(name = "key") 
protected Map<String, Lang> name; 

처럼 Address에서 사용할 수 있습니다 :

<h:outputText value="#{emp.name[userLocale].translation}" /> 

표현식 userLocale은 언어 키 (en, , ...)로 해결되거나 하드 코드 될 수 있습니다. #{emp.name['en'].translation}.

0

당신이 번역있는 테이블을 만들 더 쉽게 :

예컨대 : 사람 -> 당신의 사람의 모든 PersonTranslations

사람들 | id PersonTranslations | 장소; person_id;

은 다음 Person 클래스에 당신이 그 PersonTranslation.find (1과 같은 몇 가지를 (이것은 person_id로 키를 사용하여 PersonTranslation 검색되며, 로케일) 술어

Person.description의 모든 속성에 대한 언어를 설정 'en');

enter image description here