• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests;
18 
19 import java.util.Date;
20 import java.util.Locale;
21 
22 import com.badlogic.gdx.Gdx;
23 import com.badlogic.gdx.files.FileHandle;
24 import com.badlogic.gdx.graphics.GL20;
25 import com.badlogic.gdx.graphics.g2d.BitmapFont;
26 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
27 import com.badlogic.gdx.math.MathUtils;
28 import com.badlogic.gdx.tests.utils.GdxTest;
29 import com.badlogic.gdx.utils.I18NBundle;
30 
31 /** Performs some tests with {@link I18NBundle} and prints the results on the screen.
32  *
33  * @author davebaol */
34 public class I18NMessageTest extends GdxTest {
35 
36 	String message = "";
37 	BitmapFont font;
38 	SpriteBatch batch;
39 	I18NBundle rb_root;
40 	I18NBundle rb_default;
41 	I18NBundle rb_en;
42 	I18NBundle rb_it;
43 	I18NBundle rb_unsupported;
44 	Date now = new Date();
45 
46 	@Override
create()47 	public void create () {
48 		font = new BitmapFont();
49 		batch = new SpriteBatch();
50 
51 		try {
52 			FileHandle bfh = Gdx.files.internal("data/i18n/message1");
53 			rb_root = I18NBundle.createBundle(bfh, new Locale("", "", "")); // Locale.ROOT doesn't exist in Android API level 8
54 			rb_default = I18NBundle.createBundle(bfh);
55 			rb_en = I18NBundle.createBundle(bfh, new Locale("en", "US"));
56 			rb_it = I18NBundle.createBundle(bfh, new Locale("it", "IT"));
57 			rb_unsupported = I18NBundle.createBundle(bfh, new Locale("unsupported"));
58 
59 			println("Default locale: " + Locale.getDefault());
60 
61 			println("\n\n---- Parent chain test ----");
62 			println(getMessage("root", rb_root));
63 			println(getMessage("default", rb_default));
64 			println(getMessage("en", rb_en));
65 			println(getMessage("it", rb_it));
66 			println(getMessage("unsupported", rb_unsupported));
67 
68 			println("\n\n---- Parametric message test ----");
69 			println(getParametricMessage("root", rb_root));
70 			println(getParametricMessage("default", rb_default));
71 			println(getParametricMessage("en", rb_en));
72 			println(getParametricMessage("it", rb_it));
73 			println(getParametricMessage("unsupported", rb_unsupported));
74 
75 			Gdx.app.log("", message);
76 
77 		} catch (Throwable t) {
78 			message = "FAILED: " + t.getMessage() + "\n";
79 			message += t.getClass();
80 			Gdx.app.error(I18NMessageTest.class.getSimpleName(), "Error", t);
81 		}
82 	}
83 
getMessage(String header, I18NBundle rb)84 	private String getMessage (String header, I18NBundle rb) {
85 		return header + " -> locale: " + rb.getLocale() + ", msg: \"" + rb.format("msg") + "\", rootMsg: \"" + rb.format("rootMsg")
86 			+ "\"";
87 	}
88 
getParametricMessage(String header, I18NBundle rb)89 	private String getParametricMessage (String header, I18NBundle rb) {
90 		return header + " -> " + rb.format("msgWithArgs", "LibGDX", MathUtils.PI, now);
91 	}
92 
println(String line)93 	private void println (String line) {
94 		message += line + "\n";
95 	}
96 
97 	@Override
render()98 	public void render () {
99 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
100 		batch.begin();
101 		font.draw(batch, message, 20, Gdx.graphics.getHeight() - 20);
102 		batch.end();
103 	}
104 
105 	@Override
dispose()106 	public void dispose () {
107 		batch.dispose();
108 		font.dispose();
109 	}
110 }
111