APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.utils

pack-priv Class TextFormatter

extends Object
Class Inheritance
Imports
java.text.MessageFormat, java.util.Locale

TextFormatter is used by I18NBundle to perform argument replacement.
Author
davebaol

Field Summary

Modifier and TypeField and Description
private StringBuilder
private MessageFormat

Constructor Summary

AccessConstructor and Description
public
TextFormatter(Locale locale, boolean useMessageFormat)

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the formatted pattern
format
(String
the pattern
pattern
,
Object...
the arguments
args
)

Formats the given pattern replacing its placeholders with the actual arguments specified by args.

private String
private String

Returns:

the formatted pattern
simpleFormat
(String
the pattern
pattern
,
Object...
the arguments
args
)

Formats the given pattern replacing any placeholder of the form {0}, {1}, {2} and so on with the corresponding object from args converted to a string with toString(), so without taking into account the locale.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferback to summary
private StringBuilder buffer
messageFormatback to summary
private MessageFormat messageFormat

Constructor Detail

TextFormatterback to summary
public TextFormatter(Locale locale, boolean useMessageFormat)

Method Detail

formatback to summary
public String format(String pattern, Object... args)

Formats the given pattern replacing its placeholders with the actual arguments specified by args.

If this TextFormatter has been instantiated with TextFormatter(locale, true) MessageFormat is used to process the pattern, meaning that the actual arguments are properly localized with the locale of this TextFormatter.

On the contrary, if this TextFormatter has been instantiated with TextFormatter(locale, false) pattern's placeholders are expected to be in the simplified form {0}, {1}, {2} and so on and they will be replaced with the corresponding object from args converted to a string with toString(), so without taking into account the locale.

In both cases, there's only one simple escaping rule, i.e. a left curly bracket must be doubled if you want it to be part of your string.

It's worth noting that the rules for using single quotes within MessageFormat patterns have shown to be somewhat confusing. In particular, it isn't always obvious to localizers whether single quotes need to be doubled or not. For this very reason we decided to offer the simpler escaping rule above without limiting the expressive power of message format patterns. So, if you're used to MessageFormat's syntax, remember that with TextFormatter single quotes never need to be escaped!

Parameters
pattern:String

the pattern

args:Object[]

the arguments

Returns:String

the formatted pattern

Exceptions
IllegalArgumentException:
if the pattern is invalid
replaceEscapeCharsback to summary
private String replaceEscapeChars(String pattern)
simpleFormatback to summary
private String simpleFormat(String pattern, Object... args)

Formats the given pattern replacing any placeholder of the form {0}, {1}, {2} and so on with the corresponding object from args converted to a string with toString(), so without taking into account the locale.

This method only implements a small subset of the grammar supported by java.text.MessageFormat. Especially, placeholder are only made up of an index; neither the type nor the style are supported.

If nothing has been replaced this implementation returns the pattern itself.

Parameters
pattern:String

the pattern

args:Object[]

the arguments

Returns:String

the formatted pattern

Exceptions
IllegalArgumentException:
if the pattern is invalid