878b3c41c3b5e64868045fc3e99d1585f3f9c889
[debian/openrocket] / core / src / net / sf / openrocket / l10n / L10N.java
1 package net.sf.openrocket.l10n;
2
3 import java.util.Locale;
4 import java.util.regex.Pattern;
5
6 /**
7  * Helper methods for localization needs.
8  * 
9  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
10  */
11 public final class L10N {
12         
13         private L10N() {
14                 // Prevent instantiation
15         }
16         
17         
18         /**
19          * Replace a text token by a replacement value.
20          * <p>
21          * A text token is a string portion that should be surrounded by
22          * braces, "{text}".
23          * 
24          * @param original              the original string.
25          * @param token                 the text token to replace.
26          * @param replacement   the replacement text.
27          * @return                              the modified string.
28          */
29         public static String replace(String original, String token, String replacement) {
30                 return Pattern.compile(token, Pattern.LITERAL).matcher(original).replaceAll(replacement);
31         }
32         
33         
34         /**
35          * Convert a language code into a Locale.
36          * 
37          * @param langcode      the language code (<code>null</code> ok).
38          * @return                      the corresponding locale (or <code>null</code> if the input was <code>null</code>)
39          */
40         public static Locale toLocale(String langcode) {
41                 if (langcode == null) {
42                         return null;
43                 }
44                 
45                 Locale l;
46                 String[] split = langcode.split("[_-]", 3);
47                 if (split.length == 1) {
48                         l = new Locale(split[0]);
49                 } else if (split.length == 2) {
50                         l = new Locale(split[0], split[1]);
51                 } else {
52                         l = new Locale(split[0], split[1], split[2]);
53                 }
54                 return l;
55         }
56         
57 }