package com.ruoyi.common.utils;

import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import com.ruoyi.common.utils.spring.SpringUtils;

import java.util.Locale;

/**
 * 获取i18n资源文件
 *
 * @author ruoyi
 */
public class MessageUtils
{
    /**
     * 根据消息键和参数 获取消息 委托给spring messageSource
     *
     * @param code 消息键
     * @param args 参数
     * @return 获取国际化翻译值
     */
    public static String message(String code, Object... args) {
        try {
            MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
            Locale locale = LocaleContextHolder.getLocale();
            String msg = messageSource.getMessage(code, args, locale);
            return msg;
        } catch (Exception e){
            return "";
        }
    }

    public static String message(String code, String lang, Object... args) {
        try {
            MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
            Locale locale = new Locale(lang);
            String msg = messageSource.getMessage(code, args, locale);
            return msg;
        } catch (Exception e){
            return "";
        }
    }

}
