Search in sources :

Example 1 with LangPropsService

use of org.b3log.latke.service.LangPropsService in project solo by b3log.

the class Skins method fillLangs.

/**
     * Fills the specified data model with the current skink's (WebRoot/skins/${skinName}/lang/lang_xx_XX.properties)
     * and core language (WebRoot/WEB-INF/classes/lang_xx_XX.properties) configurations.
     *
     * @param localeString the specified locale string
     * @param currentSkinDirName the specified current skin directory name
     * @param dataModel the specified data model
     * @throws ServiceException service exception
     */
public static void fillLangs(final String localeString, final String currentSkinDirName, final Map<String, Object> dataModel) throws ServiceException {
    Stopwatchs.start("Fill Skin Langs");
    try {
        final String langName = currentSkinDirName + "." + localeString;
        Map<String, String> langs = LANG_MAP.get(langName);
        if (null == langs) {
            // Collect unused skin languages
            LANG_MAP.clear();
            LOGGER.log(Level.DEBUG, "Loading skin [dirName={0}, locale={1}]", new Object[] { currentSkinDirName, localeString });
            langs = new HashMap<String, String>();
            final String language = Locales.getLanguage(localeString);
            final String country = Locales.getCountry(localeString);
            final ServletContext servletContext = SoloServletListener.getServletContext();
            final InputStream inputStream = servletContext.getResourceAsStream("/skins/" + currentSkinDirName + "/lang/lang_" + language + '_' + country + ".properties");
            final Properties props = new Properties();
            props.load(inputStream);
            final Set<Object> keys = props.keySet();
            for (final Object key : keys) {
                langs.put((String) key, props.getProperty((String) key));
            }
            LANG_MAP.put(langName, langs);
            LOGGER.log(Level.DEBUG, "Loaded skin[dirName={0}, locale={1}, keyCount={2}]", new Object[] { currentSkinDirName, localeString, langs.size() });
        }
        // Fills the current skin's language configurations
        dataModel.putAll(langs);
        // Fills the core language configurations
        final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
        final LangPropsService langPropsService = beanManager.getReference(LangPropsServiceImpl.class);
        dataModel.putAll(langPropsService.getAll(Latkes.getLocale()));
    } catch (final IOException e) {
        LOGGER.log(Level.ERROR, "Fills skin langs failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) InputStream(java.io.InputStream) ServletContext(javax.servlet.ServletContext) IOException(java.io.IOException) Properties(java.util.Properties) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager) LangPropsService(org.b3log.latke.service.LangPropsService)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 ServletContext (javax.servlet.ServletContext)1 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)1 LangPropsService (org.b3log.latke.service.LangPropsService)1 ServiceException (org.b3log.latke.service.ServiceException)1