ä»æ¥ã¯Javaã®ã話ã§ãã
åé¡ã³ã¼ãã¼
- ã·ã¹ãã åºåç¨ã®åºå®ã¡ãã»ã¼ã¸ããå®æ°ã¯ã©ã¹ã«ä¸å çã«ãã¼ãã³ã¼ãã£ã³ã°ãã¦ããæ¢åã·ã¹ãã ããã
- ãã®å®æ°ã¯ã©ã¹ã«ã¯ãç¾ç¶ãæ¥æ¬èªã®åºå®ã¡ãã»ã¼ã¸ããè¨è¿°ããã¦ããªã
- ã·ã¹ãã ãã¼ã¸ã§ã³ã¢ããã®ä¸ç°ã¨ãã¦ãè±èªã¡ãã»ã¼ã¸ã®åºåï¼ã¤ã¾ãå¤è¨èªåï¼ã«å¯¾å¿ããå¿ è¦ãçãã
- ãã ããå®æ°ã¯ã©ã¹ãå©ç¨ããå´ï¼å¼ã³åºãå´ï¼ã®æ¢åããã°ã©ã ã¯ã極åå¤æ´ããããªã
ãã¤ã¹ãªä¾ç¤º
ç¾ç¶ã¯ãã·ã¹ãã ã§åºåãããã¡ãã»ã¼ã¸ã®æè¨ãå®æ°ã¯ã©ã¹ Message ã«éç´ããã¦ãã¾ãã
Message.java
package com.tercel_s.Util; /** * ã·ã¹ãã ã§ä½¿ç¨ãããã¡ãã»ã¼ã¸ãä¸å çã«å®ç¾©ãã¦ããå®æ°ã¯ã©ã¹ã§ã * @author Tercel */ public final class Message { public static final String MSG001 = "ããã«ã¡ã¯ä¸ç"; public static final String MSG002 = "è´å½çãªã¨ã©ã¼ãçºçãã¾ãã"; public static final String MSG003 = "ãã¡ã¤ã«ãåå¨ãã¾ãã"; // â¦â¦ããããæ¥æ¬èªã®ã¡ãã»ã¼ã¸ã延ã ã¨å®ç¾©ããã¦ãã }
ã¾ããã¡ãã»ã¼ã¸ãåºåããã¨ãã«ã¯ã以ä¸ã®ããã«ã¡ãã»ã¼ã¸ã¯ã©ã¹ã®ãã£ã¼ã«ãã«ç´æ¥ã¢ã¯ã»ã¹ãã¦ãã¾ãã
Main.java
package com.tercel_s.Api; import com.tercel_s.Util.Message; public class Main { public static void main(String[] args) { // ç¾ç¶ãå®æ°ã¯ã©ã¹ã«å®ç¾©ããã¦ãããã£ã¼ã«ãã«ç´ã¢ã¯ã»ã¹ãã形㧠// åºå®ã¡ãã»ã¼ã¸ãåå¾ã»è¡¨ç¤ºãã¦ãã System.out.println(Message.MSG001); // => ããã«ã¡ã¯ä¸ç System.out.println(Message.MSG002); // => è´å½çãªã¨ã©ã¼ãçºçãã¾ãã System.out.println(Message.MSG003); // => ãã¡ã¤ã«ãåå¨ãã¾ãã } }
å®æ°ã¯ã©ã¹ Message ã¯ãã·ã¹ãã å ã®ããããã¯ã©ã¹ããå©ç¨ããããå ±éé¨åï¼ã®ã¤ããï¼ã§ãã
è¨ãæããã°ãã©ãã ãã®ã¯ã©ã¹ã Message ã¯ã©ã¹ã«ã¢ã¯ã»ã¹ãã¦ãããããã®å½±é¿ç¯å²ã¯æªç¥æ°ã¨ãããã¨ã§ãã
ã¤ã¾ããªããã°ãé²ãããã«ããMessage ã¯ã©ã¹ä»¥å¤ã®æ¢åã½ã¼ã¹ãä¸åä¿®æ£ããã«ãç°å¢ã«å¿ãã¦é©åãªã¡ãã»ã¼ã¸ãåºåãããããã«ãããã¨ããã®ãä»åã®ãã¼ãã§ãã
System.out.println(Message.MSG001); // => ããã«ã¡ã¯ä¸ç â» æ¥æ¬èªç°å¢ã®å ´å // => Hello, World! â» è±èªç°å¢ã®å ´å
æããã¦å¯è½ã§ããããã
ä»æ¥ã¯ããã®åé¡ã解決ããããã«ç¨ããæ¹æ³ã3ã¹ãããã§ãç´¹ä»ãã¾ãã
ã¼ãã¯ããèãã
Step1. å®æ°ã¯ã©ã¹ãããåæåã¸
ã¾ãã¯æ¢åã³ã¼ãã®ãªãã¡ã¯ã¿ãªã³ã°ã§ããå®æ°ã¯ã©ã¹ Message ãã以ä¸ã®ãããªåæå (enum) ã«æ¸ãæãã¾ãã
Message.java
package com.tercel_s.Util; /** * ã·ã¹ãã ã§ä½¿ç¨ãããã¡ãã»ã¼ã¸ãä¸å çã«å®ç¾©ãã¦ããåæåã§ã * @author Tercel */ public enum Message { MSG001 { @Override public String toString() { return "ããã«ã¡ã¯ä¸ç"; } }, MSG002 { @Override public String toString() { return "è´å½çãªã¨ã©ã¼ãçºçãã¾ãã"; } }, MSG003 { @Override public String toString() { return "ãã¡ã¤ã«ãåå¨ãã¾ãã"; } } // â¦â¦ã¾ãã¯ãããããµãã«æ¸ãæãã }
Main.java å´ã¯ä½ãå¤æ´ããå¿ è¦ã¯ããã¾ããã
ãªãã¨ãããã¨ã§ãããã ï¼âãµã¶ã¨ããã®å£°ã§
ä»ã¾ã§ããã£ã¼ã«ãã®å®æ°å¤ããã®ã¾ã¾è¿å´ããèéã®å©ããªãä½ãã ã£ãããã°ã©ã ããå ã®ãªãã©ã¼ã ã«ãããè¿å´ããå¤ã toString() ã¡ã½ããã§å¶å¾¡ã§ããããã«ãªãã¾ããã
Step 2. åæåãå¤è¨èªã«å¯¾å¿ããã
ç¶ãã¦ãä¸ã¤ã®åæåã«å¯¾ãã¦è¨èªå¥ã«ç¨æããè¤æ°ã®ã¡ãã»ã¼ã¸ãç´ã¥ãã¦ã¿ã¾ãã
æ°ç toString() ã®ä»æãã¯ä»¥ä¸ã®éãã§ãã
- java.util.Locale ã«ã¦å®ç¾©ããã¦ããè¨èªã³ã¼ãã¨ãå½è©²è¨èªç¨ã®ã¡ãã»ã¼ã¸å®æ°ã®å¯¾å¿ã HashMap ã«æ ¼ç´ãã
- ãã®HashMapãããç¾å¨ã®ãã±ã¼ã«ã®è¨èªã³ã¼ãã«å¯¾å¿ããã¡ãã»ã¼ã¸ãåå¾ãã¦è¿å´ãã
Message.java
package com.tercel_s.Util; import java.util.HashMap; import java.util.Locale; /** * ã·ã¹ãã ã§ä½¿ç¨ãããã¡ãã»ã¼ã¸ãä¸å çã«å®ç¾©ãã¦ããåæä½ï¼äºãå½èªå¯¾å¿çï¼ã§ã * @author Tercel */ public enum Message { MSG001 { @Override public String toString() { return new HashMap<String, String>() {{ // ããã«ãæ¥æ¬èªç°å¢ç¨ã»è±èªç°å¢ç¨ã®ã¡ãã»ã¼ã¸ãè¨è¿°ãã¾ã put(Locale.JAPANESE.getLanguage(), "ããã«ã¡ã¯ä¸ç"); put(Locale.ENGLISH.getLanguage(), "Hello, World!"); }}.get(Locale.getDefault().getLanguage()); } }, MSG002 { @Override public String toString() { return new HashMap<String, String>() {{ // ããã«ãæ¥æ¬èªç°å¢ç¨ã»è±èªç°å¢ç¨ã®ã¡ãã»ã¼ã¸ãå®ç¾©ãã¾ã put(Locale.JAPANESE.getLanguage(), "è´å½çãªã¨ã©ã¼ãçºçãã¾ãã"); put(Locale.ENGLISH.getLanguage(), "Fatal error occured!"); }}.get(Locale.getDefault().getLanguage()); } }, MSG003 { @Override public String toString() { return new HashMap<String, String>() {{ // ããã«ãæ¥æ¬èªç°å¢ç¨ã»è±èªç°å¢ç¨ã®ã¡ãã»ã¼ã¸ãå®ç¾©ãã¾ã put(Locale.JAPANESE.getLanguage(), "ãã¡ã¤ã«ãåå¨ãã¾ãã"); put(Locale.ENGLISH.getLanguage(), "File not found!"); }}.get(Locale.getDefault().getLanguage()); } }; }
ããããã¨ãããã©ã«ããã±ã¼ã«ã®å 容ã«å¿ãã¦åºåãããã¡ãã»ã¼ã¸ãèªåçã«åãæ¿ããããã«ãªãã¾ãã
Main.java
package com.tercel_s.Api; import java.util.Locale; import com.tercel_s.Util.Message; public class Main { public static void main(String[] args) { Locale defaultLocale = Locale.getDefault(); // ããã©ã«ããã±ã¼ã«ãæ¥æ¬ã«è¨å® Locale.setDefault(Locale.JAPAN); System.out.println(Message.MSG001); // => ããã«ã¡ã¯ä¸ç System.out.println(Message.MSG002); // => è´å½çãªã¨ã©ã¼ãçºçãã¾ãã System.out.println(Message.MSG003); // => ãã¡ã¤ã«ãåå¨ãã¾ãã // ããã©ã«ããã±ã¼ã«ãã¢ã¡ãªã«ã«è¨å® Locale.setDefault(Locale.US); System.out.println(Message.MSG001); // => Hello, World! System.out.println(Message.MSG002); // => Fatal error occurred! System.out.println(Message.MSG003); // => File not found! // ããã©ã«ããã±ã¼ã«ããã¨ã«æ»ã Locale.setDefault(defaultLocale); } }
å®è¡çµæ
ããã«ã¡ã¯ä¸ç è´å½çãªã¨ã©ã¼ãçºçãã¾ãã ãã¡ã¤ã«ãåå¨ãã¾ãã Hello, World! Fatal error occured! File not found!
ããã§ä¸å¿ãå½åã®ç®çã¯éæã§ãã¾ããã
ããããã¯ããã«ä¸æ©é²ãã§ãJavaã§ãµãã¼ãããã¦ãããã¼ã«ã©ã¤ãºã®ä»çµã¿ã使ã£ã¦ã¿ããã¨æãã¾ãã
Step 3. ãªã½ã¼ã¹ãã³ãã«ã§ããã°ã©ã ã®è¦éããæ¹åãã
æå¾ã®ä»ä¸ãã«ãã¡ãã»ã¼ã¸å®ç¾©ããã±ã¼ã«å¥ã«ããããã£åãã¦ã¿ã¾ãããã
Step 2. ã§ã¯å ¨è¨èªç¨ã®ã¡ãã»ã¼ã¸ãä¸ã¤ã®ã½ã¼ã¹ã³ã¼ãå ã«ã¾ã¨ãã¦æ¸ããã¦ããã®ã§ãã¡ã³ããããªãã£ã¯è¯å¥½ã¨ã¯è¨ãã¾ããã§ããã
ãã¨ãã°è±èªçã®ã¡ãã»ã¼ã¸ã ããã¡ã³ããã³ã¹ãããã¨ããèãã¦ã¿ã¾ããããè±èªçã®ã¡ãã»ã¼ã¸é¨åã ããè¦ããã«ãé¢ããããä¸ã¤ã®ã½ã¼ã¹ãã¡ã¤ã«ã«è¤æ°ãã±ã¼ã«åãã®ãã¼ã¿ããã¹ã¦éç´ããã¦ããããé常ã«è¦éããæªããªã£ã¦ãã¾ãã¾ãã
ã¾ããæ¬æ¥ã¯å®æ°ãã¼ã¿ãä¿æããå½¹å²ã ã£ãã¯ãã® Message.java ã§ãããã½ã¼ã¹ãå¿ è¦ä»¥ä¸ã«è¥å¤§åãã¦ãã¾ã£ã¦ãã¾ãããåããããªã³ã¼ããç¹°ãè¿ãåºç¾ãã¦ãããã¨ãæ°ã«ãªãã¾ãã
ä¸è¨ã®åé¡ãè¸ã¾ãã以ä¸ã®ããã«ä¿®æ£ãã¦ã¿ã¾ãããã
- ã¡ãã»ã¼ã¸å®ç¾©ãJavaã®ã½ã¼ã¹ã§ã¯ãªãããã±ã¼ã«å¥ã®プロパティãã¡ã¤ã«ã«æ¸ã
- Javaå´ã¯ãç¾å¨ã®ãã±ã¼ã«ã«å¯¾ããé©åãªããããã£ãã¡ã¤ã«ããã¡ãã»ã¼ã¸ãæ¾ãããã«ãã
ãªããããã§æ°ãã«ç»å ´ããããããã£ã§ãããããã¤ãå®ããã°ãªããªãã«ã¼ã«ãããã¾ãããããããã¨ã¯ãResourceBundle (Java Platform SE 8)ããåç §ãã¦ãã ããã
- ããããã£ã®ãã¡ã¤ã«åã¯ã[åºåºå]_[è¨èªã³ã¼ã].propertiesãã¾ãã¯ã[åºåºå]_[è¨èªã³ã¼ã]_[å½åã³ã¼ã].propertiesããªã©ã®å½¢å¼ã«å¾ãã¾ãï¼ããã¾ã§ä¸ä¾ï¼
- ä¾1) HOGEHOGE_ja_JP.properties
- ä¾2) HOGEHOGE_en.properties
- ããããã£ãã¡ã¤ã«ã«ã¯ã1è¡ãã¤ã[ãã¼]:[å¤]ãã¾ãã¯ã[ãã¼]=[å¤]ãå½¢å¼ã«å¾ã£ã¦æ¸ãã¾ã
- ä¾1) MSG001:Hello, World!
- ãã ããè¡ã®æå¾ãã\ãã§çµããå ´åã¯ã次ã®è¡ãç¶ç¶è¡ã¨ã¿ãªããã¾ã
- ããããã£ãã¡ã¤ã«å
ã«ASCIIã³ã¼ãæåã®ã¿ã使ç¨ã§ãã¾ã
- æ¥æ¬èªãªã©ã使ç¨ãããå ´åã¯Unicodeåç
§ã\uâ¦ãã§æ¸ãã¾ã
- ä¾) MSG001:\u3053\u3093\u306B\u3061\u306F\u4E16\u754C
- æ¥æ¬èªãªã©ã使ç¨ãããå ´åã¯Unicodeåç
§ã\uâ¦ãã§æ¸ãã¾ã
ããã§ã¯ãå¾ ã¡ãããæ¬æ¥æçµçã®ã½ã¼ã¹ã³ã¼ãã§ãã
MessageList_ja_JP.properties
MSG001:\u3053\u3093\u306B\u3061\u306F\u4E16\u754C MSG002:\u81F4\u547D\u7684\u306A\u30A8\u30E9\u30FC\u304C\u767A\u751F\u3057\u307E\u3057\u305F MSG003:\u30D5\u30A1\u30A4\u30EB\u304C\u5B58\u5728\u3057\u307E\u305B\u3093
MessageList_en_US.properties
MSG001:Hello, World! MSG002:Fatal error occured! MSG003:File not found!
Message.java
package com.tercel_s.Util; import java.util.Locale; import java.util.ResourceBundle; /** * ã·ã¹ãã ã§ä½¿ç¨ãããã¡ãã»ã¼ã¸ãä¸å çã«å®ç¾©ãã¦ããåæä½ï¼äºãå½èªå¯¾å¿çï¼ã§ã * @author Tercel */ public enum Message { MSG001, MSG002, MSG003; @Override public String toString() { try { return ResourceBundle.getBundle("com.tercel_s.Util.MessageList", Locale.getDefault()).getString(name()); }catch(Exception ex) { ex.printStackTrace(); return null; } } }
Main.java
package com.tercel_s.Api; import java.util.Locale; import com.tercel_s.Util.Message; public class Main { public static void main(String[] args) { Locale defaultLocale = Locale.getDefault(); // ããã©ã«ããã±ã¼ã«ãæ¥æ¬èªã«è¨å® Locale.setDefault(Locale.JAPAN); System.out.println(Message.MSG001); // => ããã«ã¡ã¯ä¸ç System.out.println(Message.MSG002); // => è´å½çãªã¨ã©ã¼ãçºçãã¾ãã System.out.println(Message.MSG003); // => ãã¡ã¤ã«ãåå¨ãã¾ãã // ããã©ã«ããã±ã¼ã«ãè±èªã«è¨å® Locale.setDefault(Locale.US); System.out.println(Message.MSG001); // => Hello, World! System.out.println(Message.MSG002); // => Fatal error occurred! System.out.println(Message.MSG003); // => File not found! // ããã©ã«ããã±ã¼ã«ããã¨ã«æ»ã Locale.setDefault(defaultLocale); } }
å®è¡çµæ
ããã«ã¡ã¯ä¸ç è´å½çãªã¨ã©ã¼ãçºçãã¾ãã ãã¡ã¤ã«ãåå¨ãã¾ãã Hello, World! Fatal error occured! File not found!
ãã¼ãï¼ã§ããï¼
ã¾ã¨ã
- ç°å¢ä¾åã®å¤ãå®æ°ã«æ±ºãæã¡ãã¦ã¯ãããªãï¼å¾ã§ãããå°ããï¼
- ã¬ã¬ã·ã¼ãªå®æ°ã¯ã©ã¹ã¯ enum ã«ç½®ãæãã¦ã¿ãã¨æè»æ§ãå¢ããã¨ããã
- ãã±ã¼ã«ä¾åã®ã¡ãã»ã¼ã¸ã¯ããããã£ãã¡ã¤ã«ã«æ¸ããããããã
ã¨ããããã§ãé·ã ã¨ãèªã¿ããã ããããã¨ããããã¾ããã