jp.co.ctc_g.jfw.core.resource.Rs.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.ctc_g.jfw.core.resource.Rs.java

Source

/*
 * Copyright (c) 2013 ITOCHU Techno-Solutions Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.ctc_g.jfw.core.resource;

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

import jp.co.ctc_g.jfw.core.internal.InternalMessages;
import jp.co.ctc_g.jfw.core.util.Maps;
import jp.co.ctc_g.jfw.core.util.Strings;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * <p>
 * ????J-Framework?????????
 * </p>
 *
 * <h4></h4>
 * <p>
 * ??? {@link org.springframework.context.support.ResourceBundleMessageSource } ??????
 * </p>
 *
 * <h4></h4>
 * <p>
 * ?????????? ????????? ?????
 * {@link Locale}???? ???Web??????????????????
 * ????????????????????
 * </p>
 *
 * @author ITOCHU Techno-Solutions Corporation.
 */
public final class Rs {

    private static final Log L = LogFactory.getLog(Rs.class);
    private static final ResourceBundle R = InternalMessages.getBundle(Rs.class);

    private Rs() {
    }

    /**
     * ????????
     * ????????<strong>????????</strong>
     *
     * @param key
     *            ??
     * @return ??
     */
    public static String find(String key) {
        return find(key, (Locale) null);
    }

    /**
     * ????????????
     * ????????defaultValue????????</strong>
     *
     * @param key
     *            ??
     * @param defaultValue
     *            ?
     * @return ??
     */
    public static String find(String key, String defaultValue) {
        try {
            MessageSource source = MessageSourceLocator.get();
            if (Strings.isEmpty(key))
                return "";
            Locale userLocale = LocaleContextHolder.getLocale();
            Locale currentLocale = userLocale != null ? userLocale : Locale.getDefault();
            String value = source.getMessage(key, null, currentLocale);
            if (Strings.isEmpty(value)) {
                return key;
            } else {
                return value;
            }
        } catch (NoSuchMessageException e) {
            return defaultValue;
        }
    }

    /**
     * ????????????
     * ????????<strong>????????</strong>
     *
     * @param key
     *            ??
     * @param locale
     *            ??
     * @return ??
     */
    public static String find(String key, Locale locale) {
        try {
            MessageSource source = MessageSourceLocator.get();

            if (Strings.isEmpty(key))
                return "";

            Locale currentLocale = locale;
            if (currentLocale == null) {
                Locale userLocale = LocaleContextHolder.getLocale();
                currentLocale = userLocale != null ? userLocale : Locale.getDefault();
            }
            String value = source.getMessage(key, null, currentLocale);
            if (Strings.isEmpty(value)) {
                return key;
            } else {
                return value;
            }
        } catch (NoSuchMessageException e) {
            if (L.isDebugEnabled()) {
                String message = Strings.substitute(R.getString("I-RESOURCE#0001"), Maps.hash("key", key));
                L.debug(message);
            }
            return key;
        }
    }

    /**
     * ?????
     * ??????????????
     * @param context {@link MessageSourceResolvable}
     * @return ??
     */
    public static String find(MessageSourceResolvable context) {
        return find(context, (Locale) null);
    }

    /**
     * ?????????
     * ??????????????
     * @param context {@link MessageSourceResolvable}
     * @param locale 
     * @return ??
     */
    public static String find(MessageSourceResolvable context, Locale locale) {
        try {
            MessageSource source = MessageSourceLocator.get();
            Locale currentLocale = locale;
            if (currentLocale == null) {
                Locale userLocale = LocaleContextHolder.getLocale();
                currentLocale = userLocale != null ? userLocale : Locale.getDefault();
            }
            return source.getMessage(context, currentLocale);
        } catch (NoSuchMessageException e) {
            return context.getDefaultMessage();
        }
    }

}