Java Code Examples for java.text.MessageFormat#applyPattern()

The following examples show how to use java.text.MessageFormat#applyPattern() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityPropertiesServiceSimple.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public String getPropertyMessage(String code, Object[] args, Locale locale) {
    if (code == null) {
        throw new IllegalArgumentException("code (key) cannot be null when looking up messages");
    }
    String message = null;
    String template = bundle.getString(code);
    if (template != null) {
        if (args != null && args.length > 0) {
            MessageFormat formatter = new MessageFormat("");
            if (locale == null) {
                locale = Locale.getDefault();
            }
            formatter.setLocale(locale);
            formatter.applyPattern(template);
            message = formatter.format(args);
        } else {
            message = template;
        }
    }
    return message;
}
 
Example 2
Source File: UpdateDialogController.java    From updatefx with MIT License 6 votes vote down vote up
private void initialize() {
	URL changelog = release.getApplication().getChangelog();
	
	if (changelog != null) {
		WebEngine engine = changeView.getEngine();
		String finalURL = String.format("%s?from=%d&to=%d", changelog, currentReleaseID, release.getId());
		engine.load(finalURL);
	} else {
		changeView.setVisible(false);
		changeView.setManaged(false);
	}
	
   Object[] messageArguments = { release.getApplication().getName(), currentVersion, release.getVersion() };
   MessageFormat formatter = new MessageFormat("");
   formatter.setLocale(resources.getLocale());
   
	if (release.getLicenseVersion() != currentLicenseVersion) {
		formatter.applyPattern(resources.getString("infotext.paidupgrade"));
	} else {
		formatter.applyPattern(resources.getString("infotext.freeupgrade"));			
	}
	
	infoLabel.setText(formatter.format(messageArguments));
	infoLabel.autosize();
}
 
Example 3
Source File: LocalizedMessage.java    From ripple-lib-java with ISC License 6 votes vote down vote up
protected String formatWithTimeZone(
        String template,
        Object[] arguments, 
        Locale locale,
        TimeZone timezone) 
{
    MessageFormat mf = new MessageFormat(" ");
    mf.setLocale(locale);
    mf.applyPattern(template);
    if (!timezone.equals(TimeZone.getDefault())) 
    {
        Format[] formats = mf.getFormats();
        for (int i = 0; i < formats.length; i++) 
        {
            if (formats[i] instanceof DateFormat) 
            {
                DateFormat temp = (DateFormat) formats[i];
                temp.setTimeZone(timezone);
                mf.setFormat(i,temp);
            }
        }
    }
    return mf.format(arguments);
}
 
Example 4
Source File: OverwriteDesign.java    From ldparteditor with MIT License 6 votes vote down vote up
/**
 * Create contents of the dialog.
 *
 * @param parent
 */
@Override
protected Control createDialogArea(Composite parent) {
    Composite cmp_Container = (Composite) super.createDialogArea(parent);
    GridLayout gridLayout = (GridLayout) cmp_Container.getLayout();
    gridLayout.verticalSpacing = 10;
    gridLayout.horizontalSpacing = 10;

    Label lbl_overwrite = new Label(cmp_Container, SWT.NONE);

    Object[] messageArguments = {whichFile};
    MessageFormat formatter = new MessageFormat(""); //$NON-NLS-1$
    formatter.setLocale(MyLanguage.LOCALE);
    formatter.applyPattern(I18n.DIALOG_Replace);
    lbl_overwrite.setText(formatter.format(messageArguments));

    return cmp_Container;
}
 
Example 5
Source File: LocalizedMessage.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
protected String formatWithTimeZone(
        String template,
        Object[] arguments, 
        Locale locale,
        TimeZone timezone) 
{
    MessageFormat mf = new MessageFormat(" ");
    mf.setLocale(locale);
    mf.applyPattern(template);
    if (!timezone.equals(TimeZone.getDefault())) 
    {
        Format[] formats = mf.getFormats();
        for (int i = 0; i < formats.length; i++) 
        {
            if (formats[i] instanceof DateFormat) 
            {
                DateFormat temp = (DateFormat) formats[i];
                temp.setTimeZone(timezone);
                mf.setFormat(i,temp);
            }
        }
    }
    return mf.format(arguments);
}
 
Example 6
Source File: EntityPropertiesServiceSimple.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public String getPropertyMessage(String code, Object[] args, Locale locale) {
    if (code == null) {
        throw new IllegalArgumentException("code (key) cannot be null when looking up messages");
    }
    String message = null;
    String template = bundle.getString(code);
    if (template != null) {
        if (args != null && args.length > 0) {
            MessageFormat formatter = new MessageFormat("");
            if (locale == null) {
                locale = Locale.getDefault();
            }
            formatter.setLocale(locale);
            formatter.applyPattern(template);
            message = formatter.format(args);
        } else {
            message = template;
        }
    }
    return message;
}
 
Example 7
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Replace MessageFormat(String, Locale) constructor (not available until JDK 1.4).
 * @param pattern string
 * @param locale Locale
 * @return MessageFormat
 */
private MessageFormat createMessageFormat(String pattern, Locale locale) {
    MessageFormat result = new MessageFormat(pattern);
    if (locale != null) {
        result.setLocale(locale);
        result.applyPattern(pattern);
    }
    return result;
}
 
Example 8
Source File: HeadingFilter.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void setInitialContext(InitialRenderContext context)
{
	super.setInitialContext(context);
	String outputTemplate = outputMessages.getString(getLocaleKey()
			+ ".print");
	formatter = new MessageFormat("");
	formatter.applyPattern(outputTemplate);
}
 
Example 9
Source File: UrlFilter.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void setInitialContext(InitialRenderContext context)
{
	super.setInitialContext(context);
	String outputTemplate = outputMessages.getString(getLocaleKey()
			+ ".print");
	formatter = new MessageFormat("");
	formatter.applyPattern(outputTemplate);
}
 
Example 10
Source File: MessageFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_setLocaleLjava_util_Locale() {
  MessageFormat format = new MessageFormat("date {0,date}");
  format.setLocale(Locale.CHINA);
  assertEquals("Wrong locale1", Locale.CHINA, format.getLocale());
  format.applyPattern("{1,date}");
  assertEquals("Wrong locale3", DateFormat.getDateInstance(DateFormat.DEFAULT,
                                                           Locale.CHINA), format.getFormats()[0]);
}
 
Example 11
Source File: MessageFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_equalsLjava_lang_Object() {
    MessageFormat format1 = new MessageFormat("{0}");
    MessageFormat format2 = new MessageFormat("{1}");
    assertTrue("Should not be equal", !format1.equals(format2));
    format2.applyPattern("{0}");
    assertTrue("Should be equal", format1.equals(format2));
    SimpleDateFormat date = (SimpleDateFormat) DateFormat.getTimeInstance();
    format1.setFormat(0, DateFormat.getTimeInstance());
    format2.setFormat(0, new SimpleDateFormat(date.toPattern()));
    assertTrue("Should be equal2", format1.equals(format2));
}
 
Example 12
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Replace MessageFormat(String, Locale) constructor (not available until JDK 1.4).
 * @param pattern string
 * @param locale Locale
 * @return MessageFormat
 */
private MessageFormat createMessageFormat(String pattern, Locale locale) {
    MessageFormat result = new MessageFormat(pattern);
    if (locale != null) {
        result.setLocale(locale);
        result.applyPattern(pattern);
    }
    return result;
}
 
Example 13
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Replace MessageFormat(String, Locale) constructor (not available until JDK 1.4).
 * @param pattern string
 * @param locale Locale
 * @return MessageFormat
 */
private MessageFormat createMessageFormat(final String pattern, final Locale locale) {
    final MessageFormat result = new MessageFormat(pattern);
    if (locale != null) {
        result.setLocale(locale);
        result.applyPattern(pattern);
    }
    return result;
}
 
Example 14
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Replace MessageFormat(String, Locale) constructor (not available until JDK 1.4).
 * @param pattern string
 * @param locale Locale
 * @return MessageFormat
 */
private MessageFormat createMessageFormat(String pattern, Locale locale) {
    MessageFormat result = new MessageFormat(pattern);
    if (locale != null) {
        result.setLocale(locale);
        result.applyPattern(pattern);
    }
    return result;
}
 
Example 15
Source File: MessageFormatDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        int planet = 7;
        String event = "a disturbance in the Force";

        String messageFormatPattern = "At {1,time,long} on {1,date,full}, there was {2} on planet {0,number,integer}.";
        MessageFormat messageFormat = new MessageFormat(messageFormatPattern);
        String result = messageFormat.format(new Object[]{planet, new Date(), event});
        System.out.println(result);

        // 重置 MessageFormatPattern
        // applyPattern
        messageFormatPattern = "This is a text : {0}, {1}, {2}";
        messageFormat.applyPattern(messageFormatPattern);
        result = messageFormat.format(new Object[]{"Hello,World", "666"});
        System.out.println(result);

        // 重置 Locale
        messageFormat.setLocale(Locale.ENGLISH);
        messageFormatPattern = "At {1,time,long} on {1,date,full}, there was {2} on planet {0,number,integer}.";
        messageFormat.applyPattern(messageFormatPattern);
        result = messageFormat.format(new Object[]{planet, new Date(), event});
        System.out.println(result);

        // 重置 Format
        // 根据参数索引来设置 Pattern
        messageFormat.setFormat(1,new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"));
        result = messageFormat.format(new Object[]{planet, new Date(), event});
        System.out.println(result);
    }
 
Example 16
Source File: ExtendedMessageFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Replace MessageFormat(String, Locale) constructor (not available until JDK 1.4).
 * @param pattern string
 * @param locale Locale
 * @return MessageFormat
 */
private MessageFormat createMessageFormat(String pattern, Locale locale) {
    MessageFormat result = new MessageFormat(pattern);
    if (locale != null) {
        result.setLocale(locale);
        result.applyPattern(pattern);
    }
    return result;
}
 
Example 17
Source File: HeadingFilter.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void setInitialContext(InitialRenderContext context)
{
	super.setInitialContext(context);
	String outputTemplate = outputMessages.getString(getLocaleKey()
			+ ".print");
	formatter = new MessageFormat("");
	formatter.applyPattern(outputTemplate);
}
 
Example 18
Source File: UrlFilter.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void setInitialContext(InitialRenderContext context)
{
	super.setInitialContext(context);
	String outputTemplate = outputMessages.getString(getLocaleKey()
			+ ".print");
	formatter = new MessageFormat("");
	formatter.applyPattern(outputTemplate);
}
 
Example 19
Source File: JREvaluator.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 
 */
private MessageFormat getMessageFormat(String pattern)
{
	MessageFormat messageFormat = new MessageFormat("");
	messageFormat.setLocale((Locale)locale.getValue());
	messageFormat.applyPattern(pattern);
	return messageFormat;
}
 
Example 20
Source File: Messages.java    From javalite with Apache License 2.0 5 votes vote down vote up
private static String getMessage(String key, Locale locale, Object... params){
    MessageFormat mf = new MessageFormat("");
    try{
        if(locale == null){
            mf.applyPattern(ResourceBundle.getBundle(BUNDLE).getString(key));
        }else{
            mf.applyPattern(ResourceBundle.getBundle(BUNDLE, locale).getString(key));
        }
    }catch(Exception e){
        mf.applyPattern(key);
    }
    return mf.format(params);
}