1 | package fr.mael.jiwigo.transverse.util; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FilenameFilter; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.List; |
---|
7 | import java.util.Locale; |
---|
8 | import java.util.ResourceBundle; |
---|
9 | import java.util.Set; |
---|
10 | import java.util.TreeSet; |
---|
11 | |
---|
12 | /** |
---|
13 | Copyright (c) 2010, Mael |
---|
14 | All rights reserved. |
---|
15 | |
---|
16 | Redistribution and use in source and binary forms, with or without |
---|
17 | modification, are permitted provided that the following conditions are met: |
---|
18 | * Redistributions of source code must retain the above copyright |
---|
19 | notice, this list of conditions and the following disclaimer. |
---|
20 | * Redistributions in binary form must reproduce the above copyright |
---|
21 | notice, this list of conditions and the following disclaimer in the |
---|
22 | documentation and/or other materials provided with the distribution. |
---|
23 | * Neither the name of jiwigo nor the |
---|
24 | names of its contributors may be used to endorse or promote products |
---|
25 | derived from this software without specific prior written permission. |
---|
26 | |
---|
27 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
28 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
29 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
30 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
31 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
32 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
33 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
34 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
35 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
36 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
37 | |
---|
38 | * @author mael |
---|
39 | * management of the messages for the translations |
---|
40 | */ |
---|
41 | public class Messages { |
---|
42 | |
---|
43 | public static Locale usedLocale = Locale.getDefault(); |
---|
44 | |
---|
45 | /** |
---|
46 | * returns a message |
---|
47 | * @param key the key of the message |
---|
48 | * @return the message |
---|
49 | */ |
---|
50 | public static String getMessage(String key) { |
---|
51 | return ResourceBundle.getBundle("fr.mael.jiwigo.trad.messages", usedLocale).getString(key); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Gets available translations |
---|
56 | * @return the list of available translations |
---|
57 | */ |
---|
58 | public static List<Locale> getAvailableBundles() { |
---|
59 | ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
---|
60 | final String bundlepackage = "fr.mael.jiwigo.trad"; |
---|
61 | final String bundlename = "messages"; |
---|
62 | |
---|
63 | File root = new File(loader.getResource(bundlepackage.replace('.', '/')).getFile()); |
---|
64 | File[] files = root.listFiles(new FilenameFilter() { |
---|
65 | public boolean accept(File dir, String name) { |
---|
66 | return name.matches("^" + bundlename + "(_\\w{2}(_\\w{2})?)?\\.properties$"); |
---|
67 | } |
---|
68 | }); |
---|
69 | |
---|
70 | Set<String> languages = new TreeSet<String>(); |
---|
71 | for (File file : files) { |
---|
72 | languages.add(file.getName().replaceAll("^" + bundlename + "(_)?|\\.properties$", "")); |
---|
73 | } |
---|
74 | |
---|
75 | List<Locale> availableLocales = new ArrayList<Locale>(); |
---|
76 | for (Locale locale : Locale.getAvailableLocales()) { |
---|
77 | for (String s : languages) { |
---|
78 | if (s.equals(locale.getLanguage())) { |
---|
79 | availableLocales.add(locale); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | return availableLocales; |
---|
84 | } |
---|
85 | |
---|
86 | } |
---|