<commit_before>/**
 * Vosao CMS. Simple CMS for Google App Engine.
 * Copyright (C) 2009 Vosao development team
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * email: <EMAIL>
 */

package org.vosao.i18n;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.vosao.common.VosaoContext;

/**
 * Message bundle helper class for creatng localized messages from Java code.
 * 
 * @author <NAME>
 *
 */
public class Messages {

	public static final String JSTL_FMT_LOCALE_KEY = 
		"javax.servlet.jsp.jstl.fmt.locale.session";

	private static final Locale[] supportedLocales = {
		Locale.ENGLISH, 
		new Locale("ru")};
	
	private static Map<Locale, VosaoResourceBundle> bundles = 
			new HashMap<Locale, VosaoResourceBundle>();
	
	private static VosaoResourceBundle getBundle(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Locale sessionLocale = (Locale)session.getAttribute(JSTL_FMT_LOCALE_KEY);
		Locale locale = sessionLocale != null ? sessionLocale : 
			request.getLocale();
		return getBundle(locale);
	}
	
	private static VosaoResourceBundle getDefaultBundle() {
		return getBundle(Locale.ENGLISH);
	}

	private static VosaoResourceBundle getBundle(Locale locale) {
		if (!bundles.containsKey(locale)) {
			bundles.put(locale, new VosaoResourceBundle(locale));
		}
		return bundles.get(locale);
	}
	
	public static String get(String key, Object...objects) {
		VosaoContext ctx = VosaoContext.getInstance();
		String pattern = "not found";
		if (isLocaleSupported(ctx.getLocale())) {
			pattern = getBundle(ctx.getRequest()).getString(key);
		}
		else {
			pattern = getDefaultBundle().getString(key);
		}
		if (objects != null) {
			MessageFormat formatter = new MessageFormat("");
			formatter.setLocale(ctx.getLocale());
			formatter.applyPattern(pattern);
			pattern = formatter.format(objects);
		}
		return pattern;
	}
	
	/**
	 * Generate JavaScript JSON message bundle for JavaScript messages 
	 * localization.
	 * @return JS file.
	 */
	public static String getJSMessages() {
		VosaoContext ctx = VosaoContext.getInstance();
		Map<String, String> messages = new HashMap<String, String>();
		VosaoResourceBundle defaultBundle = getDefaultBundle();
		for (String key : defaultBundle.keySet()) {
			try {
				messages.put(key, defaultBundle.getString(key));
			}
			catch (MissingResourceException e) {
			}
		}
		VosaoResourceBundle bundle = getBundle(ctx.getRequest());
		for (String key : bundle.keySet()) {
			try {
				messages.put(key, bundle.getString(key));
			}
			catch (MissingResourceException e) {
			}
		}
		StringBuffer result = new StringBuffer();
		result.append("locale = '").append(ctx.getLocale().toString())
				.append("';\n");
		result.append("locale_language = '")
			.append(ctx.getLocale().getLanguage()).append("';\n");
		result.append("messages = {\n");
		int i = 0;
		for (String key : messages.keySet()) {
			if (i++ > 0) {
				result.append(",");
			}
			result.append(" '").append(key).append("' : '")
				.append(filterForJS(messages.get(key)))<commit_msg>Plugin localization.<commit_after>/**
 * Vosao CMS. Simple CMS for Google App Engine.
 * Copyright (C) 2009 Vosao development team
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * email: <EMAIL>
 */

package org.vosao.i18n;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.vosao.common.VosaoContext;

/**
 * Message bundle helper class for creatng localized messages from Java code.
 * 
 * @author <NAME>
 *
 */
public class Messages {

	public static final String JSTL_FMT_LOCALE_KEY = 
		"javax.servlet.jsp.jstl.fmt.locale.session";

	private static final Locale[] supportedLocales = {
		Locale.ENGLISH, 
		new Locale("ru")};
	
	private static Map<Locale, VosaoResourceBundle> bundles = 
			new HashMap<Locale, VosaoResourceBundle>();
	
	private static VosaoResourceBundle getBundle(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Locale sessionLocale = (Locale)session.getAttribute(JSTL_FMT_LOCALE_KEY);
		Locale locale = sessionLocale != null ? sessionLocale : 
			request.getLocale();
		return getBundle(locale);
	}
	
	private static VosaoResourceBundle getDefaultBundle() {
		return getBundle(Locale.ENGLISH);
	}

	private static VosaoResourceBundle getBundle(Locale locale) {
		if (!bundles.containsKey(locale)) {
			bundles.put(locale, new VosaoResourceBundle(locale));
		}
		return bundles.get(locale);
	}
	
	public static String get(String key, Object...objects) {
		VosaoContext ctx = VosaoContext.getInstance();
		String pattern = "not found";
		if (isLocaleSupported(ctx.getLocale())) {
			pattern = getBundle(ctx.getRequest()).getString(key);
		}
		else {
			pattern = getDefaultBundle().getString(key);
		}
		if (objects != null) {
			MessageFormat formatter = new MessageFormat("");
			formatter.setLocale(ctx.getLocale());
			formatter.applyPattern(pattern);
			pattern = formatter.format(objects);
		}
		return pattern;
	}
	
	/**
	 * Generate JavaScript JSON message bundle for JavaScript messages 
	 * localization.
	 * @return JS file.
	 */
	public static String getJSMessages() {
		VosaoContext ctx = VosaoContext.getInstance();
		Map<String, String> messages = new HashMap<String, String>();
		VosaoResourceBundle defaultBundle = getDefaultBundle();
		for (String key : Collections.list(defaultBundle.getKeys())) {
			try {
				messages.put(key, defaultBundle.getString(key));
			}
			catch (MissingResourceException e) {
			}
		}
		VosaoResourceBundle bundle = getBundle(ctx.getRequest());
		for (String key : Collections.list(bundle.getKeys())) {
			try {
				messages.put(key, bundle.getString(key));
			}
			catch (MissingResourceException e) {
			}
		}
		StringBuffer result = new StringBuffer();
		result.append("locale = '").append(ctx.getLocale().toString())
				.append("';\n");
		result.append("locale_language = '")
			.append(ctx.getLocale().getLanguage()).append("';\n");
		result.append("messages = {\n");
		int i = 0;
		for (String key : messages.keySet()) {
			if (i++ > 0) {
				result.append(",");
			}
			result.append(" '").append(key).append("' : '")
				.append(filterForJS(messages.get(key)))
{'fraction_non_alphanumeric': 0.11778144434572915, 'fraction_numerical': 0.005076786394212464, 'mean_word_length': 4.249833444370419, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2358832', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2289, 'n_words': 666}