mirror of https://github.com/deavmi/JavaOCP
Localization
- Using `MessageFormat` with parameterized messages
This commit is contained in:
parent
cf275fb5f0
commit
8bce513a2a
|
@ -67,6 +67,19 @@
|
|||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>ocp.local.Localization.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
@ -76,6 +89,11 @@
|
|||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</plugins>
|
||||
</reporting>
|
||||
</project>
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.util.List;
|
|||
import java.text.DateFormat;
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.MessageFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
|
@ -305,5 +306,37 @@ public class App
|
|||
customFormatter2 = new SimpleDateFormat("MMMM MMMM", symbols);
|
||||
System.out.println("Date&Time formatted with custom formatter for language '"+currentLocale.getLanguage()+"': "+customFormatter2.format(today));
|
||||
|
||||
|
||||
/**
|
||||
* Going back to resource bundles.
|
||||
*
|
||||
* We can also have parameterized messages,
|
||||
* of which can have normal parameters we pass
|
||||
* it (indexed from 0 onwards) but also certain
|
||||
* of those parameterized parts can be formatted
|
||||
* according to a type like a date and that
|
||||
* can be formatted according to the locale too
|
||||
*
|
||||
* We first create a {@link MessageFormat}, set its
|
||||
* locale, and then we pass it the resource
|
||||
* in the bundle. Lastly, we call format which
|
||||
* replaces the items and if locale-dependent
|
||||
* things like dates need formatting then the locale
|
||||
* passed in `msgFormat.setLocale(Locale)` will be used
|
||||
*/
|
||||
greetingBundle = ResourceBundle.getBundle("GreetingsBundle", currentLocale);
|
||||
MessageFormat msgFormat = new MessageFormat("");
|
||||
msgFormat.setLocale(currentLocale);
|
||||
|
||||
/**
|
||||
* We must pass the string to format
|
||||
*/
|
||||
msgFormat.applyPattern(greetingBundle.getString("message"));
|
||||
|
||||
/**
|
||||
* Now we can format the message and have it plop
|
||||
* the arguments into it we pass below.
|
||||
*/
|
||||
System.out.println(msgFormat.format(new Object[] {69, today}));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue