• 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 I18NSimpleMessageTest 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 		I18NBundle.setSimpleFormatter(true);
52 
53 		try {
54 			FileHandle bfh = Gdx.files.internal("data/i18n/message2");
55 			rb_root = I18NBundle.createBundle(bfh, new Locale("", "", "")); // Locale.ROOT doesn't exist in Android API level 8
56 			rb_default = I18NBundle.createBundle(bfh);
57 			rb_en = I18NBundle.createBundle(bfh, new Locale("en", "US"));
58 			rb_it = I18NBundle.createBundle(bfh, new Locale("it", "IT"));
59 			rb_unsupported = I18NBundle.createBundle(bfh, new Locale("unsupported"));
60 
61 			println("Default locale: " + Locale.getDefault());
62 
63 			println("\n\n---- Parent chain test ----");
64 			println(getMessage("root", rb_root));
65 			println(getMessage("default", rb_default));
66 			println(getMessage("en", rb_en));
67 			println(getMessage("it", rb_it));
68 			println(getMessage("unsupported", rb_unsupported));
69 
70 			println("\n\n---- Parametric message test ----");
71 			println(getParametricMessage("root", rb_root));
72 			println(getParametricMessage("default", rb_default));
73 			println(getParametricMessage("en", rb_en));
74 			println(getParametricMessage("it", rb_it));
75 			println(getParametricMessage("unsupported", rb_unsupported));
76 
77 			Gdx.app.log("", message);
78 
79 		} catch (Throwable t) {
80 			message = "FAILED: " + t.getMessage() + "\n";
81 			message += t.getClass();
82 			Gdx.app.error(I18NSimpleMessageTest.class.getSimpleName(), "Error", t);
83 		}
84 	}
85 
getMessage(String header, I18NBundle rb)86 	private String getMessage (String header, I18NBundle rb) {
87 		return header + " -> locale: " + rb.getLocale() + ", msg: \"" + rb.format("msg") + "\", rootMsg: \"" + rb.format("rootMsg")
88 			+ "\"";
89 	}
90 
getParametricMessage(String header, I18NBundle rb)91 	private String getParametricMessage (String header, I18NBundle rb) {
92 		return header + " -> " + rb.format("msgWithArgs", "LibGDX", MathUtils.PI, now);
93 	}
94 
println(String line)95 	private void println (String line) {
96 		message += line + "\n";
97 	}
98 
99 	@Override
render()100 	public void render () {
101 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
102 		batch.begin();
103 		font.draw(batch, message, 20, Gdx.graphics.getHeight() - 20);
104 		batch.end();
105 	}
106 
107 	@Override
dispose()108 	public void dispose () {
109 		batch.dispose();
110 		font.dispose();
111 	}
112 }
113