/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2011.
 *
 * ASTRID Research
 * Headquaters: Csapo utca 42. Debrecen H-4029 HUNGARY
 * Phone: +36-52-533-173        Fax: +36-52-533-174
 * Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DebGen Biobank Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.localization.service;

import hu.astrid.debgen.server.startup.ApplicationParameters;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * The localization Service
 * @author inagy
 */
public class LocalizationService {

    /**
     * Returns the localized pair of the message (key)
     * @param message The key of the specified exception message
     * @return The localized message
     */
    public String getMessage(String message) {
        try {
            return getString(message.replaceAll(" ", "_").toUpperCase());
        } catch (Exception e) {
            return message;
        }
    }

    /**
     * The default constructor
     */
    public LocalizationService() {
        this.setLocale(ApplicationParameters.getLanguageCode());
    }

    /**
     * Returns the default ResourceBundle
     * @return
     */
    public ResourceBundle getDefaultBundle() {
        return ResourceBundle.getBundle("hu/astrid/debgen/localization/languagefiles/gui_languages");
    }

    /**
     * Returns a string from the bundle using the key
     * @param key The key
     * @return The value pair of the key
     */
    public String getString(String key) {
        return getDefaultBundle().getString(key);
    }

    /**
     * Sets the Locale using the specified language code
     * @param languageCode The ISO3 code of the language
     */
    public void setLocale(String languageCode) {
        for (Locale l : Locale.getAvailableLocales()) {
            if (l.getISO3Language().equalsIgnoreCase(languageCode)) {
                Locale.setDefault(l);
            }
        }
    }

    public String getMessage(String key, String languageISOCode) {
        return ResourceBundle.getBundle("hu/astrid/debgen/localization/languagefiles/gui_languages", this.getLocale(languageISOCode)).getString(key.toUpperCase().replaceAll(" ", "_"));
    }

    public Locale getLocale(String languageISOCode) {
        for (Locale l : Locale.getAvailableLocales()) {
            if (l.getISO3Language().equalsIgnoreCase(languageISOCode)) {
                return l;
            }
        }
        return Locale.getDefault();
    }
}
