package com.cf.imes.framework.common.util.i18n.core.util;

import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * 国际化资源包工具类
 *
 * @author Gqr
 * @since 2025/11/21 11:16
 */
@Slf4j
public class ResourceBundleUtils {

    /**
     * 获取国际化资源值
     *
     * @param baseName       资源包位置
     * @param key            i18n key
     * @param defaultMessage 没有获取到资源描述返回的缺省值
     * @param locale         语种
     * @param args           占位符数组
     * @return
     */
    public static String getMessage(String baseName, String key, String defaultMessage, Locale locale, Object... args) {
        try {
            System.out.println("baseName:" + baseName + ",key:" + key + ",defaultMessage:" + defaultMessage + ",locale:" + locale);
            ResourceBundle messageEnum = ResourceBundle.getBundle(baseName, locale);
            if (messageEnum != null) {
                String i18nMessage = messageEnum.getString(key);
                return MessageFormat.format(i18nMessage, args);
            }
        } catch (Exception e) {
            log.error("[ResourceBundleUtils][getMessage]异常", e);
        }
        return defaultMessage;
    }
}
