All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.reactor.util.SpringLocaleUtil Maven / Gradle / Ivy

The newest version!
package org.zodiac.reactor.util;

import java.util.Locale;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.springframework.context.MessageSource;
import org.zodiac.reactor.exception.I18nSupportException;
import org.zodiac.reactor.locale.UnsupportedMessageSource;

import reactor.core.publisher.Mono;

public abstract class SpringLocaleUtil extends FastThreadLocalLocaleUtil {

    protected SpringLocaleUtil() {
    }

    static MessageSource messageSource = UnsupportedMessageSource.instance();

    /**
     * Specify the message source, process the zone message in a reactive environment, and transform it into a new result.
     *
     * @param  The data type.
     * @param  The result type.
     * @param messageSource The {@link MessageSource}.
     * @param source The data.
     * @param message Message converter.
     * @param mapper Data mapper.
     * @param args The parameters that will be filled in the message.
     * @return The result.
     * @see java.text.MessageFormat
     */
    public static  Mono doWithReactive(MessageSource messageSource, S source, Function message,
        BiFunction mapper, Object... args) {
        return null == messageSource ? Mono.empty() : currentReactive().map(locale -> {
            String msg = message.apply(source);
            String newMsg = resolveMessage(messageSource, locale, msg, msg, args);
            return mapper.apply(source, newMsg);
        });
    }

    /**
     * Resolve messages in a reactive manner using the specified message source.
     *
     * @param messageSource The {@link MessageSource}.
     * @param code The message code.
     * @param args The parameters that will be filled in the message.
     * @return The resolved message.
     */
    public static Mono resolveMessageReactive(MessageSource messageSource, String code, Object... args) {
        return null == messageSource ? Mono.empty() : currentReactive().map(locale -> resolveMessage(messageSource, locale, code, code, args));
    }

    /**
     * Resolve the message using the specified message source.
     *
     * @param messageSource The {@link MessageSource}.
     * @param locale The {@link Locale}.
     * @param code The message code.
     * @param defaultMessage The default message.
     * @param args The parameters that will be filled in the message.
     * @return The resolved message.
     */
    public static String resolveMessage(MessageSource messageSource, Locale locale, String code, String defaultMessage,
        Object... args) {
        return null == messageSource ? null : messageSource.getMessage(code, args, defaultMessage, locale);
    }

    public static  Mono resolveThrowable(S source,
        BiFunction mapper) {
        return resolveThrowable(messageSource, source, mapper);
    }

    public static  Mono resolveThrowable(MessageSource messageSource, S source,
        BiFunction mapper) {
        return doWithReactive(messageSource, source, I18nSupportException::getI18nCode, mapper, source.getArgs());
    }

    public static  Mono resolveThrowable(S source, BiFunction mapper,
        Object... args) {
        return resolveThrowable(messageSource, source, mapper, args);
    }

    public static  Mono resolveThrowable(MessageSource messageSource, S source,
        BiFunction mapper, Object... args) {
        if (source instanceof I18nSupportException && args.length == 0) {
            I18nSupportException ex = ((I18nSupportException)source);
            return resolveThrowable(ex, (err, msg) -> mapper.apply(source, msg));
        }
        return doWithReactive(messageSource, source, Throwable::getMessage, mapper, args);
    }

    public static  Mono doWithReactive(S source, Function message, BiFunction mapper,
        Object... args) {
        return doWithReactive(messageSource, source, message, mapper, args);
    }

    public static Mono resolveMessageReactive(String code, Object... args) {
        return currentReactive().map(locale -> resolveMessage(messageSource, locale, code, code, args));
    }

    public static String resolveMessage(String code, Locale locale, String defaultMessage, Object... args) {
        return resolveMessage(messageSource, locale, code, defaultMessage, args);
    }

    public static String resolveMessage(String code, Object... args) {
        return resolveMessage(messageSource, current(), code, code, args);
    }

    public static String resolveMessage(String code, String defaultMessage, Object... args) {
        return resolveMessage(messageSource, current(), code, defaultMessage, args);
    }
}