| 1 | package ch.poole.openinghoursparser; |
| 2 | |
| 3 | import java.text.MessageFormat; |
| 4 | import java.util.Locale; |
| 5 | import java.util.ResourceBundle; |
| 6 | |
| 7 | /** |
| 8 | * Translation support for {@link OpeningHoursParser} |
| 9 | * @author JOSM team, Simon Legner |
| 10 | */ |
| 11 | public class I18n { |
| 12 | |
| 13 | private static ResourceBundle messages; |
| 14 | |
| 15 | static { |
| 16 | setLocale(Locale.ROOT); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Private constructor to stop instantiation |
| 21 | */ |
| 22 | private I18n() { |
| 23 | // private |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Sets the locale used for translations. |
| 28 | * @param locale the locale |
| 29 | * @see ResourceBundle#getBundle(java.lang.String, java.util.Locale) |
| 30 | */ |
| 31 | public static synchronized void setLocale(Locale locale) { |
| 32 | messages = ResourceBundle.getBundle("ch.poole.openinghoursparser.Messages", locale); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns the translation for the given translation {@code key} and the supplied {@code arguments}. |
| 37 | * @param key the translation key to {@linkplain ResourceBundle#getString fetch} the translation |
| 38 | * @param arguments the translation arguments which are used {@linkplain MessageFormat#format for formatting} |
| 39 | * @return the translated string |
| 40 | */ |
| 41 | public static String tr(String key, Object... arguments) { |
| 42 | return MessageFormat.format(messages.getString(key), arguments); |
| 43 | } |
| 44 | } |