Material localization support
[debian/openrocket] / core / src / net / sf / openrocket / l10n / L10N.java
1 package net.sf.openrocket.l10n;
2
3 import java.text.Normalizer;
4 import java.util.Locale;
5 import java.util.regex.Pattern;
6
7 import net.sf.openrocket.util.Chars;
8
9 /**
10  * Helper methods for localization needs.
11  * 
12  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
13  */
14 public final class L10N {
15         
16         private L10N() {
17                 // Prevent instantiation
18         }
19         
20         
21         /**
22          * Replace a text token by a replacement value.
23          * <p>
24          * A text token is a string portion that should be surrounded by
25          * braces, "{text}".
26          * 
27          * @param original              the original string.
28          * @param token                 the text token to replace.
29          * @param replacement   the replacement text.
30          * @return                              the modified string.
31          */
32         public static String replace(String original, String token, String replacement) {
33                 return Pattern.compile(token, Pattern.LITERAL).matcher(original).replaceAll(replacement);
34         }
35         
36         
37         /**
38          * Convert a language code into a Locale.
39          * 
40          * @param langcode      the language code (<code>null</code> ok).
41          * @return                      the corresponding locale (or <code>null</code> if the input was <code>null</code>)
42          */
43         public static Locale toLocale(String langcode) {
44                 if (langcode == null) {
45                         return null;
46                 }
47                 
48                 Locale l;
49                 String[] split = langcode.split("[_-]", 3);
50                 if (split.length == 1) {
51                         l = new Locale(split[0]);
52                 } else if (split.length == 2) {
53                         l = new Locale(split[0], split[1]);
54                 } else {
55                         l = new Locale(split[0], split[1], split[2]);
56                 }
57                 return l;
58         }
59         
60         
61         public static String normalize(String text) {
62                 text = Normalizer.normalize(text, Normalizer.Form.NFKD);
63                 text = text.toLowerCase();
64                 text = text.replaceAll("\\s+", " ");
65                 text = text.trim();
66                 
67                 StringBuilder sb = new StringBuilder(text.length());
68                 for (char c : text.toCharArray()) {
69                         if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
70                                 sb.append(c);
71                         } else if (c == ' ' || c == '/' || c == Chars.FRACTION) {
72                                 sb.append('_');
73                         }
74                 }
75                 text = sb.toString();
76                 
77                 text = text.replaceAll("^_+", "");
78                 text = text.replaceAll("_+$", "");
79                 
80                 return text;
81         }
82         
83 }