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

com.litongjava.tio.boot.i18n.Res Maven / Gradle / Ivy

There is a newer version: 1.8.6
Show newest version
package com.litongjava.tio.boot.i18n;

import java.text.MessageFormat;
import java.util.ResourceBundle;
import com.jfinal.kit.StrKit;

/**
 * Res is used to get the message value from the ResourceBundle of the related Locale.
 */
public class Res {

  private final ResourceBundle resourceBundle;

  public Res(String baseName, String locale) {
    if (StrKit.isBlank(baseName)) {
      throw new IllegalArgumentException("baseName can not be blank");
    }
    if (StrKit.isBlank(locale)) {
      throw new IllegalArgumentException("locale can not be blank, the format like this: zh_CN or en_US");
    }

    this.resourceBundle = ResourceBundle.getBundle(baseName, I18n.toLocale(locale));
  }

  /**
   * Get the message value from ResourceBundle of the related Locale.
   * @param key message key
   * @return message value
   */
  public String get(String key) {
    return resourceBundle.getString(key);
  }

  /**
   * Get the message value from ResourceBundle by the key then format with the arguments.
   * Example:
* In resource file : msg=Hello {0}, today is{1}.
* In java code : res.format("msg", "james", new Date());
* In freemarker template : ${_res.format("msg", "james", new Date())}
* The result is : Hello james, today is 2015-04-14. */ public String format(String key, Object... arguments) { return MessageFormat.format(resourceBundle.getString(key), arguments); } public ResourceBundle getResourceBundle() { return resourceBundle; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy