package com.brilliance.mda.runtime.mda.impl;

import com.brilliance.mda.runtime.mda.I18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class I18NImpl implements I18n {
    private static Logger log = LoggerFactory.getLogger(I18NImpl.class);

    public String getString(Locale locale, String resourceName, String resourceKey, int index) {
        return getString(locale, resourceName, (index == 0) ? resourceKey : (String.valueOf(resourceKey) + index));
    }

    public String getString(Locale locale, String resourceName, String resourceKey) {
        if (locale == null)
            locale = Locale.getDefault();
        try {
            ResourceBundle rb = ResourceBundle.getBundle("i18n/" + resourceName, locale);
            return rb.getString(resourceKey);
        } catch (Exception e) {
            return "???" + resourceKey + "???";
        }
    }

    public String getString(Locale locale, String i18nStr) {
        return getString(locale, i18nStr, 0);
    }

    public String getString(Locale locale, String i18nStr, int index) {
        if (i18nStr == null || !i18nStr.startsWith("/"))
            return i18nStr;
        String[] values = i18nStr.split("/");
        if (values.length != 3)
            return i18nStr;
        return getString(locale, values[1], values[2], index);
    }

}
