1 package net.sf.openrocket.l10n;
3 import java.text.Normalizer;
4 import java.util.Locale;
5 import java.util.regex.Pattern;
7 import net.sf.openrocket.util.Chars;
10 * Helper methods for localization needs.
12 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14 public final class L10N {
17 // Prevent instantiation
22 * Replace a text token by a replacement value.
24 * A text token is a string portion that should be surrounded by
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.
32 public static String replace(String original, String token, String replacement) {
33 return Pattern.compile(token, Pattern.LITERAL).matcher(original).replaceAll(replacement);
38 * Convert a language code into a Locale.
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>)
43 public static Locale toLocale(String langcode) {
44 if (langcode == null) {
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]);
55 l = new Locale(split[0], split[1], split[2]);
61 public static String normalize(String text) {
62 text = Normalizer.normalize(text, Normalizer.Form.NFKD);
63 text = text.toLowerCase();
64 text = text.replaceAll("\\s+", " ");
67 StringBuilder sb = new StringBuilder(text.length());
68 for (char c : text.toCharArray()) {
69 if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
71 } else if (c == ' ' || c == '/' || c == Chars.FRACTION) {
77 text = text.replaceAll("^_+", "");
78 text = text.replaceAll("_+$", "");