質問編集履歴

1

皆さんの回答のpropertiesファイルを作ってやってみました

2015/11/18 15:33

投稿

suu
suu

スコア13

test CHANGED
File without changes
test CHANGED
@@ -111,3 +111,119 @@
111
111
  android studioにはエディタで弄れるローカライズ方法が備わっていると聞いたのですが
112
112
 
113
113
  javaではどうするのだろうと気になったので質問しました
114
+
115
+
116
+
117
+
118
+
119
+ >>>>>>>>
120
+
121
+ みなさんの言っていたPropertiesで書いてみました
122
+
123
+ try分の例外の書き方がわからなかったのでテキトウですがなんとか表示されました
124
+
125
+ あと文字化けに苦労しましたがnative2asciiで出来ました
126
+
127
+ 自分の参考にコードを載せときます
128
+
129
+
130
+
131
+ jp.properties
132
+
133
+ ```
134
+
135
+ asa=おはよう
136
+
137
+ hiru=こんにちは
138
+
139
+ ban=さよなら
140
+
141
+ ```
142
+
143
+
144
+
145
+ en.properties
146
+
147
+ ```
148
+
149
+ asa = morning
150
+
151
+ hiru = hello
152
+
153
+ ban = bye
154
+
155
+ ```
156
+
157
+
158
+
159
+
160
+
161
+ ```
162
+
163
+ import java.util.Locale;
164
+
165
+ import java.util.Properties;
166
+
167
+ import java.io.FileInputStream;
168
+
169
+
170
+
171
+ public class LocaleDisplay_Properties {
172
+
173
+ public static void main(String[] args) {
174
+
175
+
176
+
177
+ Locale local = Locale.getDefault();
178
+
179
+ String language = local.getLanguage();
180
+
181
+
182
+
183
+ String country;
184
+
185
+
186
+
187
+ if( "ja".equals(language) ){
188
+
189
+ country = "jp.properties";
190
+
191
+ }else{
192
+
193
+ country = "english.properties";
194
+
195
+ }
196
+
197
+
198
+
199
+ Properties properties = new Properties();
200
+
201
+
202
+
203
+ try {
204
+
205
+ FileInputStream myConfFileIn = new FileInputStream(country);
206
+
207
+ properties.load(myConfFileIn);
208
+
209
+ } catch (Exception ex) {
210
+
211
+ System.out.println("error");
212
+
213
+ }
214
+
215
+
216
+
217
+ System.out.println(properties.getProperty("asa"));
218
+
219
+ System.out.println(properties.getProperty("hiru"));
220
+
221
+ System.out.println(properties.getProperty("ban"));
222
+
223
+
224
+
225
+ }
226
+
227
+ }
228
+
229
+ ```