All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.anotheria.anosite.localization.LocalizationMap Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
package net.anotheria.anosite.localization;

import net.anotheria.anoplass.api.APICallContext;
import net.anotheria.anosite.gen.asresourcedata.data.LocalizationBundle;
import net.anotheria.util.StringUtils;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Storage for localization in different environment.
* Will be rewritten with new Localization Framework. * * @author denis */ public class LocalizationMap { /** * Logger. */ private static final Logger LOGGER = LoggerFactory.getLogger(LocalizationMap.class); /** * The name under which the localization map is stored in the call context * to allow access by the variable processors. */ public static final String CALL_CONTEXT_SCOPE_NAME = LocalizationMap.class.getName(); private Map localizationBundles = new HashMap(); private LocalizationMap() { } private String getPrivateKey(LocalizationEnvironment environment, String key) { return environment.name() + "_" + key.trim(); } public String getMessage(String key) { String message = localizationBundles.get(getPrivateKey(LocalizationEnvironment.ACTION, key)); if (message != null) return message; message = localizationBundles.get(getPrivateKey(LocalizationEnvironment.BOX, key)); if (message != null) return message; message = localizationBundles.get(getPrivateKey(LocalizationEnvironment.PAGE, key)); if (message != null) return message; message = localizationBundles.get(getPrivateKey(LocalizationEnvironment.TEMPLATE, key)); if (message != null) return message; message = localizationBundles.get(getPrivateKey(LocalizationEnvironment.SITE, key)); if (message != null) return message; return localizationBundles.get(getPrivateKey(LocalizationEnvironment.RESOURCES, key)); } public void addLocalization(LocalizationEnvironment scope, Map localization) { for (Entry loc : localization.entrySet()) localizationBundles.put(getPrivateKey(scope, loc.getKey()), loc.getValue()); } public void addLocalizationBundles(LocalizationEnvironment scope, List bundles){ for (LocalizationBundle bundle : bundles) addLocalizationBundle(scope, bundle); } public void addLocalizationBundle(LocalizationEnvironment scope, LocalizationBundle bundle){ String toParse = bundle.getMessages(); if(toParse == null){ LOGGER.error("LocalizationBundle " + bundle.getId() + ": message is null!"); return; } toParse = StringUtils.removeChar(toParse, '\r'); String[] lines = StringUtils.tokenize(toParse, '\n'); for (String l : lines) { if(StringUtils.isEmpty(l) || l.trim().startsWith("#")) continue; String[] message = null; if (l.contains("\\=")) { String escapedString = l.replace("\\=", "="); message = StringUtils.tokenize(escapedString, '='); if (message.length != 2){ LOGGER.warn("Invalid format of LocalizationBundle with id " + bundle.getId() + " in line: <" + l + ">. Expected line format: "); continue; } message[1] = message[1].replace("=", "="); } else { message = StringUtils.tokenize(l, '='); if (message.length != 2){ LOGGER.warn("Invalid format of LocalizationBundle with id " + bundle.getId() + " in line: <" + l + ">. Expected line format: "); continue; } } localizationBundles.put(getPrivateKey(scope, message[0]), message[1]); } } public static LocalizationMap getCurrentLocalizationMap() { LocalizationMap loc = (LocalizationMap) APICallContext.getCallContext().getAttribute(LocalizationMap.CALL_CONTEXT_SCOPE_NAME); if (loc != null) return loc; loc = new LocalizationMap(); APICallContext.getCallContext().setAttribute(LocalizationMap.CALL_CONTEXT_SCOPE_NAME, loc); return loc; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy