cn.royalcms.facades.gettext.RC_Gettext Maven / Gradle / Ivy
package cn.royalcms.facades.gettext;
import cn.royalcms.component.gettext.GettextResource;
import org.xnap.commons.i18n.I18n;
import java.text.MessageFormat;
import java.util.Locale;
public class RC_Gettext {
private I18n i18n;
private volatile static RC_Gettext singleton;
public static RC_Gettext getSingleton() {
synchronized (RC_Gettext.class) {
if (singleton == null) {
singleton = new RC_Gettext();
}
}
return singleton;
}
private RC_Gettext() {
}
public I18n getCurrentGettext() {
return i18n;
}
/**
* @param basename my.package.Message
* @param locale zh_CN
*/
public void setCurrentGettext(String basename, Locale locale) {
i18n = new I18n(basename, locale, GettextResource.class.getClassLoader());
}
/**
* Returns text
translated into the currently selected
* language. Every user-visible string in the program must be wrapped into
* this function.
*
* @param text text to translate
*
* @since 0.9
* @return the translation
*/
public static String tr(String text) {
return getSingleton().i18n.tr(text);
}
/**
* Returns text
translated into the currently selected
* language.
*
* Occurrences of {number} placeholders in text are replaced by
* objects
.
*
* Invokes
* {@link MessageFormat#format(java.lang.String, java.lang.Object[])}.
*
* @param text text to translate
* @param objects arguments to MessageFormat.format()
*
* @since 0.9
* @return the translated text
*/
public static String tr(String text, Object[] objects) {
return getSingleton().i18n.tr(text, objects);
}
/**
* Convenience method that invokes {@link #tr(String, Object[])}.
*
* @param text the key string to be translated.
* @param o1 object args to be formatted and substituted.
*
* @since 0.9
* @return String
*/
public static String tr(String text, Object o1) {
return getSingleton().i18n.tr(text, o1);
}
/**
* Convenience method that invokes {@link #tr(String, Object[])}.
*
* @param text the key string to be translated.
* @param o1 object args to be formatted and substituted.
* @param o2 object args to be formatted and substituted.
*
* @since 0.9
* @return String
*/
public static String tr(String text, Object o1, Object o2) {
return getSingleton().i18n.tr(text, o1, o2);
}
/**
* Convenience method that invokes {@link #tr(String, Object[])}.
*
* @param text the key string to be translated.
* @param o1 object args to be formatted and substituted.
* @param o2 object args to be formatted and substituted.
* @param o3 object args to be formatted and substituted.
*
* @since 0.9
* @return String
*/
public static String tr(String text, Object o1, Object o2, Object o3) {
return getSingleton().i18n.tr(text, o1, o2, o3);
}
/**
* Convenience method that invokes {@link #tr(String, Object[])}.
*
* @param text the key string to be translated.
* @param o1 object args to be formatted and substituted.
* @param o2 object args to be formatted and substituted.
* @param o3 object args to be formatted and substituted.
* @param o4 object args to be formatted and substituted.
*
* @since 0.9
* @return String
*/
public static String tr(String text, Object o1, Object o2, Object o3, Object o4) {
return getSingleton().i18n.tr(text, o1, o2, o3, o4);
}
/**
* Returns the plural form for n
of the translation of
* text
.
*
* @param text the key string to be translated.
* @param pluralText the plural form of text
.
* @param n value that determines the plural form
*
* @since 0.9
* @return the translated text
*/
public static String trn(String text, String pluralText, long n) {
return getSingleton().i18n.trn(text, pluralText, n);
}
/**
* Returns the plural form for n
of the translation of
* text
.
*
* @param text the key string to be translated.
* @param pluralText the plural form of text
.
* @param n value that determines the plural form
* @param objects object args to be formatted and substituted.
*
* @since 0.9
* @return the translated text
*/
public static String trn(String text, String pluralText, long n, Object[] objects) {
return getSingleton().i18n.trn(text, pluralText, n, objects);
}
/**
* Overloaded method that invokes
* {@link #trn(String, String, long, Object[])} passing Object
* arguments as an array.
*
* @param text the key string to be translated.
* @param pluralText the plural form of text
.
* @param n value that determines the plural form
* @param o1 object args to be formatted and substituted.
*
* @since 0.9
* @return String
*/
public static String trn(String text, String pluralText, long n, Object o1) {
return getSingleton().i18n.trn(text, pluralText, n, o1);
}
/**
* Overloaded method that invokes
* {@link #trn(String, String, long, Object[])} passing Object
* arguments as an array.
*
* @param text the key string to be translated.
* @param pluralText the plural form of text
.
* @param n value that determines the plural form
* @param o1 object args to be formatted and substituted.
* @param o2 object args to be formatted and substituted.
*
* @since 0.9
* @return String
*/
public static String trn(String text, String pluralText, long n, Object o1, Object o2) {
return getSingleton().i18n.trn(text, pluralText, n, o1, o2);
}
/**
* Overloaded method that invokes
* {@link #trn(String, String, long, Object[])} passing Object
* arguments as an array.
*
* @param text the key string to be translated.
* @param pluralText the plural form of text
.
* @param n value that determines the plural form
* @param o1 object args to be formatted and substituted.
* @param o2 object args to be formatted and substituted.
* @param o3 object args to be formatted and substituted.
*
* @since 0.9
* @return String
*/
public static String trn(String text, String pluralText, long n, Object o1, Object o2, Object o3) {
return getSingleton().i18n.trn(text, pluralText, n, o1, o2, o3);
}
/**
* Overloaded method that invokes
* {@link #trn(String, String, long, Object[])} passing Object
* arguments as an array.
*
* @param text the key string to be translated.
* @param pluralText the plural form of text
.
* @param n value that determines the plural form
* @param o1 object args to be formatted and substituted.
* @param o2 object args to be formatted and substituted.
* @param o3 object args to be formatted and substituted.
* @param o4 object args to be formatted and substituted.
*
* @since 0.9
* @return String
*/
public static String trn(String text, String pluralText, long n, Object o1, Object o2, Object o3, Object o4) {
return getSingleton().i18n.trn(text, pluralText, n, o1, o2, o3, o4);
}
}