package com.xxx.base.springframework.util;

import com.xxx.base.springframework.config.PathLocaleResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author huzhiwen
 * @Date 2020/10/21 13:28
 **/
@Component
@Slf4j
public class MessageUtil {

    private static MessageSource messageSource;

    private static PathLocaleResolver pathLocaleResolver;

    public MessageUtil(MessageSource messageSource, PathLocaleResolver pathLocaleResolver) {
        this.messageSource = messageSource;
        this.pathLocaleResolver = pathLocaleResolver;
    }

    /**
     * 获取单个国际化翻译值
     */
    public static String get(String msgKey) {
        try {
            return messageSource.getMessage(msgKey, null, pathLocaleResolver.getLocal());
        } catch (Exception e) {
            log.error("国际化发生异常", e);
            return msgKey;
        }
    }
}
