2009-10-01 18:46:46 +02:00
|
|
|
/*
|
2011-10-28 08:17:54 +02:00
|
|
|
* This file is part of LibrePlan
|
2009-10-01 18:46:46 +02:00
|
|
|
*
|
2010-07-19 09:36:44 +02:00
|
|
|
* Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e
|
2010-07-19 09:47:20 +02:00
|
|
|
* Desenvolvemento Tecnolóxico de Galicia
|
2011-01-19 18:00:09 +01:00
|
|
|
* Copyright (C) 2010-2011 Igalia, S.L.
|
2011-07-19 09:26:26 +02:00
|
|
|
* Copyright (C) 2011 ComtecSF, S.L.
|
2009-10-01 18:46:46 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-10-28 08:17:54 +02:00
|
|
|
package org.libreplan.web;
|
2009-08-13 14:13:27 +02:00
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
2011-07-19 09:26:26 +02:00
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
|
2011-10-28 08:17:54 +02:00
|
|
|
import org.libreplan.business.users.entities.User;
|
2009-08-13 14:13:27 +02:00
|
|
|
import org.xnap.commons.i18n.I18n;
|
|
|
|
|
import org.xnap.commons.i18n.I18nFactory;
|
|
|
|
|
import org.zkoss.util.Locales;
|
2011-07-19 09:26:26 +02:00
|
|
|
import org.zkoss.web.servlet.Charsets;
|
2011-07-20 08:30:48 +02:00
|
|
|
import org.zkoss.zk.ui.Execution;
|
2011-07-19 09:26:26 +02:00
|
|
|
import org.zkoss.zk.ui.Executions;
|
2009-08-13 14:13:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public class I18nHelper {
|
|
|
|
|
|
2011-02-04 13:18:10 +01:00
|
|
|
private static Locale defaultLang = Locale.ENGLISH;
|
2009-11-03 22:59:57 +01:00
|
|
|
|
2016-04-28 14:42:42 +03:00
|
|
|
private static HashMap<Locale, I18n> localesCache = new HashMap<>();
|
2009-08-13 14:13:27 +02:00
|
|
|
|
2016-10-31 16:49:57 +02:00
|
|
|
private I18nHelper() {
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-13 14:13:27 +02:00
|
|
|
public static I18n getI18n() {
|
2011-07-20 08:30:48 +02:00
|
|
|
setPreferredLocale();
|
2011-02-04 13:18:10 +01:00
|
|
|
|
|
|
|
|
Locale locale = Locales.getCurrent();
|
2011-07-19 09:26:26 +02:00
|
|
|
|
2016-04-28 14:42:42 +03:00
|
|
|
if ( localesCache.keySet().contains(locale) ) {
|
2011-02-04 13:18:10 +01:00
|
|
|
return localesCache.get(locale);
|
2009-08-13 14:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-28 14:42:42 +03:00
|
|
|
I18n i18n = I18nFactory.getI18n(I18nHelper.class, locale, org.xnap.commons.i18n.I18nFactory.FALLBACK);
|
2011-02-04 13:18:10 +01:00
|
|
|
|
|
|
|
|
// The language returned is not the same as the requested by the user
|
2016-04-28 14:42:42 +03:00
|
|
|
if ( !locale.getLanguage().equals(i18n.getResources().getLocale().getLanguage()) ) {
|
2011-02-04 13:18:10 +01:00
|
|
|
// Force it to be default language
|
|
|
|
|
i18n = getDefaultI18n();
|
|
|
|
|
}
|
2009-08-13 14:13:27 +02:00
|
|
|
localesCache.put(Locales.getCurrent(), i18n);
|
|
|
|
|
|
|
|
|
|
return i18n;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-20 08:30:48 +02:00
|
|
|
private static void setPreferredLocale() {
|
|
|
|
|
Execution execution = Executions.getCurrent();
|
2016-04-28 14:42:42 +03:00
|
|
|
if ( execution != null ) {
|
2011-10-11 13:45:05 +02:00
|
|
|
Locale userLocale = getUserLocale();
|
2016-04-28 14:42:42 +03:00
|
|
|
Charsets.setPreferredLocale((HttpSession) execution.getSession().getNativeSession(), userLocale);
|
|
|
|
|
if ( userLocale != null ) {
|
2011-10-11 13:45:05 +02:00
|
|
|
Locales.setThreadLocal(userLocale);
|
|
|
|
|
}
|
2011-07-20 08:30:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-19 09:26:26 +02:00
|
|
|
private static Locale getUserLocale() {
|
|
|
|
|
User user = UserUtil.getUserFromSession();
|
2016-04-28 14:42:42 +03:00
|
|
|
return (user != null) ? user.getApplicationLanguage().getLocale() : null;
|
2011-07-19 09:26:26 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-04 13:18:10 +01:00
|
|
|
private static I18n getDefaultI18n() {
|
|
|
|
|
I18n i18n = localesCache.get(defaultLang);
|
2016-04-28 14:42:42 +03:00
|
|
|
|
|
|
|
|
if ( i18n == null ) {
|
|
|
|
|
i18n = I18nFactory.getI18n(I18nHelper.class, defaultLang, org.xnap.commons.i18n.I18nFactory.FALLBACK);
|
2011-02-04 13:18:10 +01:00
|
|
|
}
|
2016-04-28 14:42:42 +03:00
|
|
|
|
2011-02-04 13:18:10 +01:00
|
|
|
return i18n;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-26 17:20:38 +03:00
|
|
|
/**
|
2016-10-31 16:49:57 +02:00
|
|
|
* TODO It should be changed since JDK9
|
|
|
|
|
*
|
2016-10-26 17:20:38 +03:00
|
|
|
* Use of '_' as an identifier might not be supported in releases after Java SE 8.
|
|
|
|
|
*
|
|
|
|
|
* @param str
|
|
|
|
|
* @return Text depends on locale
|
|
|
|
|
*/
|
2009-08-13 14:13:27 +02:00
|
|
|
public static String _(String str) {
|
|
|
|
|
return getI18n().tr(str);
|
|
|
|
|
}
|
2009-08-19 10:35:29 +02:00
|
|
|
|
|
|
|
|
public static String _(String text, Object o1) {
|
|
|
|
|
return getI18n().tr(text, o1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String _(String text, Object o1, Object o2) {
|
|
|
|
|
return getI18n().tr(text, o1, o2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String _(String text, Object o1, Object o2, Object o3) {
|
|
|
|
|
return getI18n().tr(text, o1, o2, o3);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 14:42:42 +03:00
|
|
|
public static String _(String text, Object o1, Object o2, Object o3, Object o4) {
|
2009-08-19 10:35:29 +02:00
|
|
|
return getI18n().tr(text, o1, o2, o3, o4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String _(String text, Object[] objects) {
|
|
|
|
|
return getI18n().tr(text, objects);
|
|
|
|
|
}
|
2009-11-03 23:12:02 +01:00
|
|
|
|
2009-08-19 10:35:29 +02:00
|
|
|
}
|