我想設置一個翻譯服務,它將使用GNU gettext。基本思路是:https://thedarkgod.wordpress.com/2009/01/18/java-webapp-localization-through-gettext/java spring mvc @service方法:nullpointerexception |新手和翻譯
但我希望它能夠作爲服務來實現。對於一些奇怪的原因,我想有這個類:因爲它沒有真正的工作代碼的
import webapp.service.TranslationService;
import org.springframework.context.i18n.LocaleContextHolder;
/**
* AppStrings<p>
* <p/>
* DOC-TODO:
*/
@Service("applicationStrings")
public class ApplicationStrings {
@Autowired private TranslationService translationService;
public String CART_SUBTYPE = "Cart";
public ApplicationStrings(){
Locale locale = LocaleContextHolder.getLocale();
//translationService.initLocale();
this.updateLocale();
}
public void updateLocale(){
Locale locale = LocaleContextHolder.getLocale();
translationService.updateLocale(locale);
this.setLocale(locale);
}
public void setLocale(Locale locale){
//this.CART_SUBTYPE = translationService._("Cart");
this.CART_SUBTYPE = "CART_DEF - Check ApplicationStrings";
}
}
一部分是評論...但它可能會泄露我的目標。將確實有問題的服務類看起來是這樣的:
package webapp.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import gnu.gettext.GettextResource;
import java.util.ResourceBundle;
import java.util.Locale;
import java.text.MessageFormat;
import java.util.Hashtable;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
/**
* From: https://thedarkgod.wordpress.com/2009/01/18/java-webapp-localization-through-gettext/
*
*/
@Service("translationService")
public class TranslationService {
private final Logger logger = LoggerFactory.getLogger(getClass());
private static Hashtable<Locale, ResourceBundle> trht = new Hashtable<Locale, ResourceBundle>();
private ResourceBundle myResources = null;
public TranslationService() {
Locale locale = LocaleContextHolder.getLocale();
if (locale == null){
logger.warn("Setting a default locale!");
locale = Locale.ENGLISH;
}
this.updateLocale(locale);
}
public TranslationService (Locale locale) {
this.updateLocale(locale);
}
public void initLocale() {
Locale locale = LocaleContextHolder.getLocale();
this.updateLocale(locale);
}
public void updateLocale(Locale locale)
{
synchronized (trht)
{
if (!trht.contains (locale))
{
try
{
myResources = GettextResource.getBundle("translation", locale);
}
catch (Exception e)
{
logger.error("UPDATE: Exception.");
e.printStackTrace();
}
trht.put ((Locale) locale.clone(), myResources);
}
else
myResources = trht.get (locale);
}
}
public String _(String s)
{
if (myResources == null) return s;
return GettextResource.gettext (myResources, s);
}
public String N_(String singular, String plural, long n)
{
if (myResources == null) return (n == 1 ? singular : plural);
return GettextResource.ngettext (myResources, singular,
plural, n);
}
public String format (String s, Object ... args)
{
return MessageFormat.format (_(s), args);
}
public String formatN (String singular, String plural,
long n, Object ... args)
{
return MessageFormat.format (N_(singular, plural, n), args);
}
}
這些類甚至沒有在我的應用程序使用了一點,但當然春天將實例他們和錯誤看起來是這樣的:
java.lang.NullPointerException
at webapp.constant.ApplicationStrings.updateLocale(ApplicationStrings.java:34)
at webapp.constant.ApplicationStrings.<init>(ApplicationStrings.java:29)
請請注意,所有其他@service都在工作,所以我認爲它不是一些xml配置的問題,爲此我在stackoverflow上發現了幾個問題(和答案),再次其他服務正在工作,因此配置應該沒問題。
我想這是我的新手的做法,可能會錯過一些簡單的關鍵字..甚至概念..
感謝和歡呼聲
與答案在一起,因爲這可能是gettext的一般方法和事實上與這個問題有關,我希望對實際方法提出幾點意見。
另外我完全不確定「sychronized」部分:這可能是問題嗎?
做了一個非常快速的測試...似乎沒有工作,但真的很快......明天去試試。感謝和歡呼.. F – mariotti
工程,只需要在春天的環境中製作可見/活動的PostConstuct! :) – mariotti