View Javadoc

1   /***
2    *     Ambient - A music player for the Android platform
3    Copyright (C) 2007 Martin Vysny
4    
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9    
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package sk.baka.ambient;
20  
21  import java.io.IOException;
22  import java.util.Formattable;
23  import java.util.Formatter;
24  import java.util.HashMap;
25  import java.util.IllegalFormatException;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  /***
30   * I18n support.
31   * 
32   * @author Martin Vysny
33   */
34  public final class I18n {
35  	private I18n() {
36  		throw new Error();
37  	}
38  
39  	/***
40  	 * Returns new "files" formatter. Output: "5 files"
41  	 * 
42  	 * @param filecount
43  	 *            count of files.
44  	 * @return the formatter, never <code>null</code>
45  	 */
46  	public static Formattable newFiles(int filecount) {
47  		return new AmountFormat(filecount, FILEFORMAT);
48  	}
49  
50  	private static interface CountFormatter {
51  		String format(int amount);
52  	}
53  
54  	private static class SkCountFormatter implements CountFormatter {
55  		private final String type5;
56  		private final String type1;
57  		private final String type2;
58  
59  		public SkCountFormatter(final String type1, final String type2,
60  				final String type5) {
61  			this.type1 = type1;
62  			this.type2 = type2;
63  			this.type5 = type5;
64  		}
65  
66  		public String format(int amount) {
67  			final StringBuilder b = new StringBuilder();
68  			b.append(amount);
69  			b.append(' ');
70  			if (amount == 1) {
71  				b.append(type1);
72  			} else if ((amount >= 2) && (amount <= 4)) {
73  				b.append(type2);
74  			} else {
75  				b.append(type5);
76  			}
77  			return b.toString();
78  		}
79  	}
80  
81  	private static class EnCountFormatter implements CountFormatter {
82  		private final String type1;
83  		private final String type2;
84  
85  		public EnCountFormatter(final String type1) {
86  			this.type1 = type1;
87  			type2 = type1 + "s";
88  		}
89  
90  		public EnCountFormatter(String type1, String type2) {
91  			this.type1 = type1;
92  			this.type2 = type2;
93  		}
94  
95  		public String format(int amount) {
96  			final StringBuilder b = new StringBuilder();
97  			b.append(amount);
98  			b.append(' ');
99  			b.append(amount != 1 ? type2 : type1);
100 			return b.toString();
101 		}
102 	}
103 
104 	private final static Map<String, CountFormatter> FILEFORMAT = new HashMap<String, CountFormatter>();
105 	static {
106 		FILEFORMAT
107 				.put("sk", new SkCountFormatter("súbor", "súbory", "súborov"));
108 		FILEFORMAT.put("en", new EnCountFormatter("file"));
109 	}
110 
111 	private static class AmountFormat implements Formattable {
112 		private final int amount;
113 		private final Map<String, CountFormatter> f;
114 
115 		public AmountFormat(int amount, Map<String, CountFormatter> f) {
116 			this.amount = amount;
117 			this.f = f;
118 		}
119 
120 		public void formatTo(Formatter formatter, int flags, int width,
121 				int precision) throws IllegalFormatException {
122 			final Locale l = formatter.locale();
123 			CountFormatter f = null;
124 			if (l != null) {
125 				f = this.f.get(l.getLanguage());
126 			}
127 			if (f == null) {
128 				f = this.f.get("en");
129 			}
130 			try {
131 				formatter.out().append(f.format(amount));
132 			} catch (IOException e) {
133 				throw new RuntimeException(e);
134 			}
135 		}
136 	}
137 
138 	/***
139 	 * Returns new "directories" formatter. Output: "5 directories"
140 	 * 
141 	 * @param count
142 	 *            count of directories.
143 	 * @return the formatter, never <code>null</code>
144 	 */
145 	public static Formattable newDir(int count) {
146 		return new AmountFormat(count, DIRFORMAT);
147 	}
148 
149 	private final static Map<String, CountFormatter> DIRFORMAT = new HashMap<String, CountFormatter>();
150 	static {
151 		DIRFORMAT.put("sk", new SkCountFormatter("adresár", "adresáre",
152 				"adresárov"));
153 		DIRFORMAT.put("en", new EnCountFormatter("directory", "directories"));
154 	}
155 
156 	/***
157 	 * Returns new "tracks" formatter. Output: "5 tracks"
158 	 * 
159 	 * @param count
160 	 *            count of tracks.
161 	 * @return the formatter, never <code>null</code>
162 	 */
163 	public static Formattable newTracks(int count) {
164 		return new AmountFormat(count, TRACKSFORMAT);
165 	}
166 
167 	/***
168 	 * Formats given formattable and returns the result.
169 	 * 
170 	 * @param f
171 	 *            the formattable to format. Must not be <code>null</code>.
172 	 * @return the string.
173 	 */
174 	public static String format(final Formattable f) {
175 		return new Formatter().format("%s", f).toString();
176 	}
177 
178 	private final static Map<String, CountFormatter> TRACKSFORMAT = new HashMap<String, CountFormatter>();
179 	static {
180 		TRACKSFORMAT.put("sk", new SkCountFormatter("skladba", "skladby",
181 				"skladieb"));
182 		TRACKSFORMAT.put("en", new EnCountFormatter("track"));
183 	}
184 
185 	/***
186 	 * Returns new "albums" formatter. Output: "5 albums"
187 	 * 
188 	 * @param count
189 	 *            count of albums.
190 	 * @return the formatter, never <code>null</code>
191 	 */
192 	public static Formattable newAlbums(int count) {
193 		return new AmountFormat(count, ALBUMSFORMAT);
194 	}
195 
196 	private final static Map<String, CountFormatter> ALBUMSFORMAT = new HashMap<String, CountFormatter>();
197 	static {
198 		ALBUMSFORMAT.put("sk", new SkCountFormatter("album", "albumy",
199 				"albumov"));
200 		ALBUMSFORMAT.put("en", new EnCountFormatter("album"));
201 	}
202 
203 	/***
204 	 * Returns new "artists" formatter. Output: "5 artists"
205 	 * 
206 	 * @param count
207 	 *            count of artists.
208 	 * @return the formatter, never <code>null</code>
209 	 */
210 	public static Formattable newArtists(int count) {
211 		return new AmountFormat(count, ARTISTSFORMAT);
212 	}
213 
214 	private final static Map<String, CountFormatter> ARTISTSFORMAT = new HashMap<String, CountFormatter>();
215 	static {
216 		ARTISTSFORMAT.put("sk", new SkCountFormatter("autor", "autori",
217 				"autorov"));
218 		ARTISTSFORMAT.put("en", new EnCountFormatter("artist"));
219 	}
220 
221 	/***
222 	 * Returns new "genres" formatter. Output: "5 genres"
223 	 * 
224 	 * @param count
225 	 *            count of genres.
226 	 * @return the formatter, never <code>null</code>
227 	 */
228 	public static Formattable newGenres(int count) {
229 		return new AmountFormat(count, GENREFORMAT);
230 	}
231 
232 	private final static Map<String, CountFormatter> GENREFORMAT = new HashMap<String, CountFormatter>();
233 	static {
234 		GENREFORMAT.put("sk", new SkCountFormatter("žáner", "žánre", "žánrov"));
235 		GENREFORMAT.put("en", new EnCountFormatter("genre"));
236 	}
237 }