<commit_before>/*
 * DefaultTranslate.java
 *
 * Copyright (c) 2014. <NAME> <graham_howden1 at yahoo.co.uk>.
 *
 * This file is part of PluginFramework.
 *
 * PluginFramework 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 3 of the License, or
 * (at your option) any later version.
 *
 * PluginFramework 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 PluginFramework.  If not, see <http ://www.gnu.org/licenses/>.
 */

package com.publicuhc.pluginframework.translate;

import com.google.inject.Inject;
import com.publicuhc.pluginframework.configuration.Configurator;
import org.apache.commons.lang.LocaleUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginLogger;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class DefaultTranslate implements Translate {

    private final TranslateReflection locales;
    private final YamlControl controller;

    private Locale commandBlockLocale = Locale.ENGLISH;
    private Locale remoteConsoleLocale = Locale.ENGLISH;
    private Locale consoleLocale = Locale.ENGLISH;
    private Locale broadcastLocale = Locale.ENGLISH;

    @Inject
    protected DefaultTranslate(TranslateReflection locales, YamlControl controller, PluginLogger logger)
    {
        this.locales = locales;
        this.controller = controller;
    }

    @Inject(optional = true)
    protected void setConfigurator(Configurator configurator)
    {
        FileConfiguration config = configurator.getConfig("locales");

        commandBlockLocale = LocaleUtils.toLocale(config.getString("commandBlock", "en_US"));
        remoteConsoleLocale = LocaleUtils.toLocale(config.getString("remoteConsole", "en_US"));
        consoleLocale = LocaleUtils.toLocale(config.getString("console", "en_US"));
        broadcastLocale = LocaleUtils.toLocale(config.getString("broadcast", "en_US"));
    }

    protected ResourceBundle getConfigForLocale(Locale locale)
    {
        return YamlResourceBundle.getBundle("translations.lang", locale, controller);
    }

    @Override
    public String translate(String key, CommandSender sender, Object... params)
    {
        return translate(key, getLocaleForSender(sender), params);<commit_msg>readd the optional check on the locales.yml file<commit_after>/*
 * DefaultTranslate.java
 *
 * Copyright (c) 2014. <NAME> <<EMAIL>_<EMAIL>den1 at yahoo.co.uk>.
 *
 * This file is part of PluginFramework.
 *
 * PluginFramework 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 3 of the License, or
 * (at your option) any later version.
 *
 * PluginFramework 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 PluginFramework.  If not, see <http ://www.gnu.org/licenses/>.
 */

package com.publicuhc.pluginframework.translate;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.publicuhc.pluginframework.configuration.Configurator;
import org.apache.commons.lang.LocaleUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginLogger;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Level;

public class DefaultTranslate implements Translate {

    private final TranslateReflection locales;
    private final YamlControl controller;

    private Locale commandBlockLocale = Locale.ENGLISH;
    private Locale remoteConsoleLocale = Locale.ENGLISH;
    private Locale consoleLocale = Locale.ENGLISH;
    private Locale broadcastLocale = Locale.ENGLISH;

    @Inject
    protected DefaultTranslate(TranslateReflection locales, YamlControl controller, PluginLogger logger)
    {
        this.locales = locales;
        this.controller = controller;
    }

    @Inject(optional = true)
    protected void setConfigurator(Configurator configurator, PluginLogger logger)
    {
        Optional<FileConfiguration> configurationOptional = configurator.getConfig("locales");

        if(configurationOptional.isPresent()) {
            FileConfiguration config = configurationOptional.get();

            commandBlockLocale = LocaleUtils.toLocale(config.getString("commandBlock", "en_US"));
            remoteConsoleLocale = LocaleUtils.toLocale(config.getString("remoteConsole", "en_US"));
            consoleLocale = LocaleUtils.toLocale(config.getString("console", "en_US"));
            broadcastLocale = LocaleUtils.toLocale(config.getString("broadcast", "en_US"));
        } else {
            logger.log(Level.SEVERE, "Failed to load the locales.yml config file, assuming en_US as defaults for all");
        }
    }

    protected ResourceBundle getConfigForLocale(Locale locale)
    {
        return YamlResourceBundle.getBundle("translations.lang", locale, controller);
    }

    @Override
    public String translate(String key, CommandSender sender, Object... params)
    {
        return translate(key, getLocaleForSender(sender), params);
{'fraction_non_alphanumeric': 0.08456354300385109, 'fraction_numerical': 0.0019255455712451862, 'mean_word_length': 4.291171477079796, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '535830', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1630, 'n_words': 481}