Ver3.0 ๐ ์๋ฐ ๊ตญ์ ํ์ ResourceBundle์ ํ์ฉํ ๋ค๊ตญ์ด ์ง์ ์๋ฒฝ ๊ฐ์ด๋

๐ ์๋ฐ ๊ตญ์ ํ์ ResourceBundle์ ํ์ฉํ ๋ค๊ตญ์ด ์ง์ ์๋ฒฝ ๊ฐ์ด๋
๊ธ๋ก๋ฒ ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ํ์ ๊ธฐ์ ์ ์น๊ตฌ์ฒ๋ผ ์ฝ๊ฒ ๋ฐฐ์๋ณด์! ๐
๐ฏ ์ ๋ค๊ตญ์ด ์ง์์ด ํ์ํ ๊น?
์๋ ! ์ค๋์ ์๋ฐ๋ก ๊ธ๋ก๋ฒ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค ๋ ๊ผญ ํ์ํ ๊ตญ์ ํ(Internationalization, i18n)์ ์ง์ญํ(Localization, l10n)์ ๋ํด ์ด์ผ๊ธฐํด๋ณผ๊ฒ. ๐
์์ฆ ์ธ์์์ ์ํํธ์จ์ด๋ฅผ ๋ง๋ ๋ค๋ ๊ฑด ๋จ์ํ ํ ๋๋ผ, ํ ์ธ์ด๋ง์ ์ํ ๊ฒ ์๋์ผ. ์ ์ธ๊ณ ์ฌ๋๋ค์ด ์ฌ์ฉํ ์ ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค์ด์ผ ํ๋ ์๋๊ฐ ์๊ฑฐ๋ ! ๐ ์๋ฅผ ๋ค์ด, ๋๊ฐ ๋ง๋ ๋ฉ์ง ์ฑ์ด ํ๊ตญ์์๋ง ์ฐ์ด๋ ๊ฒ ์๋๋ผ ๋ฏธ๊ตญ, ์ผ๋ณธ, ์ค๊ตญ, ์ ๋ฝ ๋ฑ ์ ์ธ๊ณ์์ ์ฌ์ฉ๋๋ค๋ฉด? ์ ๋ง ๋ฉ์ง์ง ์์?
๊ตญ์ ํ(i18n)๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ํน์ ์ธ์ด๋ ์ง์ญ์ ์ข ์๋์ง ์๋๋ก ์ค๊ณํ๋ ๊ณผ์ ์ด์ผ. ์ฝ๊ฒ ๋งํ๋ฉด, ์ฝ๋ ์์ "์๋ ํ์ธ์"๋ผ๊ณ ํ๋์ฝ๋ฉํ๋ ๊ฒ ์๋๋ผ, ๋์ค์ ์ธ์ ๋ ๋ค๋ฅธ ์ธ์ด๋ก ๋ฐ๊ฟ ์ ์๊ฒ ๊ตฌ์กฐ๋ฅผ ๋ง๋๋ ๊ฑฐ์ง. ๐๏ธ
์ง์ญํ(l10n)๋ ์ค์ ๋ก ํน์ ์ธ์ด์ ๋ฌธํ๊ถ์ ๋ง๊ฒ ์ฝํ ์ธ ๋ฅผ ๋ฒ์ญํ๊ณ ์กฐ์ ํ๋ ์์ ์ด์ผ. ๋จ์ํ ์ธ์ด๋ง ๋ฐ๊พธ๋ ๊ฒ ์๋๋ผ ๋ ์ง ํ์, ํตํ ๋จ์, ์ซ์ ํ๊ธฐ๋ฒ๊น์ง ๊ฐ ๋๋ผ์ ๊ด์ต์ ๋ง์ถฐ์ผ ํด. ๐ ๐ฐ
๐ง ResourceBundle์ด ๋ญ๊ธธ๋?
์๋ฐ์์ ๋ค๊ตญ์ด ์ง์์ ํต์ฌ์ ๋ฐ๋ก ResourceBundle์ด์ผ! ์ด ์น๊ตฌ๋ ์๋ฐ๊ฐ ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณตํ๋ ๊ฐ๋ ฅํ ๋๊ตฌ๋ก, ์ธ์ด๋ณ๋ก ๋ค๋ฅธ ํ ์คํธ๋ฅผ ๊ด๋ฆฌํ ์ ์๊ฒ ํด์ค. ๐
ResourceBundle์ ์๋ ์๋ฆฌ๋ ์๊ฐ๋ณด๋ค ๊ฐ๋จํด. ๊ฐ ์ธ์ด๋ณ๋ก properties ํ์ผ์ ๋ง๋ค๊ณ , ๊ทธ ์์ ํค-๊ฐ ์์ผ๋ก ๋ฒ์ญ๋ ํ ์คํธ๋ฅผ ์ ์ฅํ๋ ๊ฑฐ์ผ. ๊ทธ๋ฌ๋ฉด ์๋ฐ ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ฌ์ฉ์์ ๋ก์ผ์ผ(Locale)์ ๋ฐ๋ผ ์๋์ผ๋ก ์ ์ ํ ํ์ผ์ ์ ํํด์ ํ ์คํธ๋ฅผ ๊ฐ์ ธ์ค์ง! ๐ฏ
๐ฆ ResourceBundle์ ์ฃผ์ ํน์ง
1๏ธโฃ ์๋ ํด๋ฐฑ(Fallback) ๋ฉ์ปค๋์ฆ
๋ง์ฝ ํน์ ์ธ์ด์ ๋ฆฌ์์ค ํ์ผ์ด ์์ผ๋ฉด, ์๋์ผ๋ก ๊ธฐ๋ณธ ์ธ์ด๋ก ๋์๊ฐ. ์๋ฅผ ๋ค์ด messages_fr_CA.properties(์บ๋๋ค ํ๋์ค์ด)๊ฐ ์์ผ๋ฉด messages_fr.properties(ํ๋์ค์ด)๋ฅผ ์ฐพ๊ณ , ๊ทธ๊ฒ๋ ์์ผ๋ฉด messages.properties(๊ธฐ๋ณธ)๋ฅผ ์ฌ์ฉํด.
2๏ธโฃ ๊ณ์ธต์ ๊ตฌ์กฐ
์ธ์ด(language), ๊ตญ๊ฐ(country), ๋ณํ(variant) ์์ผ๋ก ์ธ๋ฐํ๊ฒ ์ง์ญํํ ์ ์์ด. ๊ฐ์ ์์ด๋ผ๋ ๋ฏธ๊ตญ ์์ด(en_US)์ ์๊ตญ ์์ด(en_GB)๋ฅผ ๊ตฌ๋ถํ ์ ์์ง!
3๏ธโฃ ์บ์ฑ ๊ธฐ๋ฅ
ํ ๋ฒ ๋ก๋๋ ResourceBundle์ ๋ฉ๋ชจ๋ฆฌ์ ์บ์๋์ด์ ์ฑ๋ฅ์ด ์ข์. ๋งค๋ฒ ํ์ผ์ ์ฝ์ ํ์๊ฐ ์๊ฑฐ๋ ! โก
4๏ธโฃ ์ ๋์ฝ๋ ์ง์
properties ํ์ผ์ ๊ธฐ๋ณธ์ ์ผ๋ก ISO-8859-1 ์ธ์ฝ๋ฉ์ ์ฌ์ฉํ์ง๋ง, ์ ๋์ฝ๋ ์ด์ค์ผ์ดํ ์ํ์ค(\uXXXX)๋ฅผ ํตํด ๋ชจ๋ ์ธ์ด๋ฅผ ํํํ ์ ์์ด.
ResourceBundle์๋ ํฌ๊ฒ ๋ ๊ฐ์ง ํ์ ์ด ์์ด:
PropertyResourceBundle: ๊ฐ์ฅ ํํ๊ฒ ์ฌ์ฉ๋๋ ํ์ ์ผ๋ก, .properties ํ์ผ์ ๊ธฐ๋ฐ์ผ๋ก ํด. ํ ์คํธ ํ์ผ์ด๋ผ ํธ์งํ๊ธฐ ์ฝ๊ณ , ๊ฐ๋ฐ์๊ฐ ์๋ ๋ฒ์ญ๊ฐ๋ ์ฝ๊ฒ ์์ ํ ์ ์์ด. ๐
ListResourceBundle: ์๋ฐ ํด๋์ค๋ก ์ง์ ๊ตฌํํ๋ ๋ฐฉ์์ด์ผ. properties ํ์ผ๋ณด๋ค ์ ์ฐํ๊ณ , ๋ฌธ์์ด์ด ์๋ ๊ฐ์ฒด๋ ์ ์ฅํ ์ ์์ด. ํ์ง๋ง ๋ฒ์ญํ ๋๋ง๋ค ์ปดํ์ผํด์ผ ํ๋ค๋ ๋จ์ ์ด ์์ง. ๐จ
๐ ์ค์ ! ResourceBundle ์ฌ์ฉํ๊ธฐ
์, ์ด์ ์ค์ ๋ก ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ์ฝ๋๋ก ์ดํด๋ณผ๊น? ๋จผ์ ํ๋ก์ ํธ ๊ตฌ์กฐ๋ถํฐ ์ก์๋ณด์! ๐๏ธ
src/
โโโ main/
โโโ resources/
โโโ messages.properties (๊ธฐ๋ณธ, ์์ด)
โโโ messages_ko.properties (ํ๊ตญ์ด)
โโโ messages_ja.properties (์ผ๋ณธ์ด)
โโโ messages_zh.properties (์ค๊ตญ์ด)
๐ Step 1: Properties ํ์ผ ๋ง๋ค๊ธฐ
๋จผ์ ๊ฐ ์ธ์ด๋ณ๋ก properties ํ์ผ์ ๋ง๋ค์ด์ผ ํด. ํ์ผ ์ด๋ฆ์ ๋ฐ๋์ basename_์ธ์ด์ฝ๋_๊ตญ๊ฐ์ฝ๋.properties ํ์์ ๋ฐ๋ผ์ผ ํด!
greeting=Hello, {0}!
welcome.message=Welcome to our application
button.submit=Submit
button.cancel=Cancel
error.required=This field is required
error.invalid.email=Please enter a valid email address
menu.home=Home
menu.about=About
menu.contact=Contact
farewell=Goodbye, see you soon!
greeting=์๋
ํ์ธ์, {0}๋!
welcome.message=์ฐ๋ฆฌ ์ ํ๋ฆฌ์ผ์ด์
์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค
button.submit=์ ์ถ
button.cancel=์ทจ์
error.required=์ด ํญ๋ชฉ์ ํ์์
๋๋ค
error.invalid.email=์ฌ๋ฐ๋ฅธ ์ด๋ฉ์ผ ์ฃผ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์
menu.home=ํ
menu.about=์๊ฐ
menu.contact=์ฐ๋ฝ์ฒ
farewell=์๋
ํ ๊ฐ์ธ์, ๋ ๋ง๋์!
greeting=ใใใซใกใฏใ{0}ใใ๏ผ
welcome.message=็งใใกใฎใขใใชใฑใผใทใงใณใธใใใใ
button.submit=้ไฟก
button.cancel=ใญใฃใณใปใซ
error.required=ใใฎ้
็ฎใฏๅฟ
้ ใงใ
error.invalid.email=ๆๅนใชใกใผใซใขใใฌในใๅ
ฅๅใใฆใใ ใใ
menu.home=ใใผใ
menu.about=ใซใคใใฆ
menu.contact=ใๅใๅใใ
farewell=ใใใใชใใใพใใไผใใใพใใใ๏ผ
๐ป Step 2: ์๋ฐ ์ฝ๋์์ ์ฌ์ฉํ๊ธฐ
์ด์ ์๋ฐ ์ฝ๋์์ ResourceBundle์ ๋ก๋ํ๊ณ ์ฌ์ฉํด๋ณด์!
import java.util.Locale;
import java.util.ResourceBundle;
public class InternationalizationDemo {
public static void main(String[] args) {
// ํ๊ตญ์ด ๋ก์ผ์ผ๋ก ResourceBundle ๋ก๋
Locale koreanLocale = new Locale("ko", "KR");
ResourceBundle koreanBundle = ResourceBundle.getBundle("messages", koreanLocale);
// ์์ด ๋ก์ผ์ผ๋ก ResourceBundle ๋ก๋
Locale englishLocale = new Locale("en", "US");
ResourceBundle englishBundle = ResourceBundle.getBundle("messages", englishLocale);
// ์ผ๋ณธ์ด ๋ก์ผ์ผ๋ก ResourceBundle ๋ก๋
Locale japaneseLocale = new Locale("ja", "JP");
ResourceBundle japaneseBundle = ResourceBundle.getBundle("messages", japaneseLocale);
// ํ๊ตญ์ด ์ถ๋ ฅ
System.out.println("=== ํ๊ตญ์ด ===");
System.out.println(koreanBundle.getString("welcome.message"));
System.out.println(koreanBundle.getString("button.submit"));
// ์์ด ์ถ๋ ฅ
System.out.println("\n=== English ===");
System.out.println(englishBundle.getString("welcome.message"));
System.out.println(englishBundle.getString("button.submit"));
// ์ผ๋ณธ์ด ์ถ๋ ฅ
System.out.println("\n=== ๆฅๆฌ่ช ===");
System.out.println(japaneseBundle.getString("welcome.message"));
System.out.println(japaneseBundle.getString("button.submit"));
}
}
์ ์ฝ๋๋ฅผ ์คํํ๋ฉด ๊ฐ ์ธ์ด๋ก ๋ฉ์์ง๊ฐ ์ถ๋ ฅ๋๋ ๊ฑธ ๋ณผ ์ ์์ด! ์ ๊ธฐํ์ง? ๐
๐จ Step 3: ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๋ฉ์์ง ์ฒ๋ฆฌํ๊ธฐ
์ค์ ์ ํ๋ฆฌ์ผ์ด์ ์์๋ ๋์ ์ธ ๊ฐ์ ๋ฉ์์ง์ ํฌํจํด์ผ ํ ๋๊ฐ ๋ง์. ์๋ฅผ ๋ค์ด "์๋ ํ์ธ์, ํ๊ธธ๋๋!"์ฒ๋ผ ์ฌ์ฉ์ ์ด๋ฆ์ ๋ฃ์ด์ผ ํ๋ ๊ฒฝ์ฐ ๋ง์ด์ผ. ์ด๋ด ๋๋ MessageFormat ํด๋์ค๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ฉด ๋ผ!
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
public class MessageFormatDemo {
public static void main(String[] args) {
// ํ๊ตญ์ด ๋ก์ผ์ผ
Locale koreanLocale = new Locale("ko", "KR");
ResourceBundle bundle = ResourceBundle.getBundle("messages", koreanLocale);
// ์ฌ์ฉ์ ์ด๋ฆ์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ
String userName = "ํ๊ธธ๋";
String greetingPattern = bundle.getString("greeting");
String greeting = MessageFormat.format(greetingPattern, userName);
System.out.println(greeting); // ์ถ๋ ฅ: ์๋
ํ์ธ์, ํ๊ธธ๋๋!
// ์์ด ๋ก์ผ์ผ๋ก ๋ณ๊ฒฝ
Locale englishLocale = new Locale("en", "US");
bundle = ResourceBundle.getBundle("messages", englishLocale);
userName = "John";
greetingPattern = bundle.getString("greeting");
greeting = MessageFormat.format(greetingPattern, userName);
System.out.println(greeting); // ์ถ๋ ฅ: Hello, John!
}
}
MessageFormat์ ์ฌ๋ฌ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ ์ฒ๋ฆฌํ ์ ์์ด. properties ํ์ผ์ {0}, {1}, {2} ๊ฐ์ ํ๋ ์ด์คํ๋๋ฅผ ๋ฃ๊ณ , ์์๋๋ก ๊ฐ์ ์ ๋ฌํ๋ฉด ๋ผ! ๐
order.confirmation=์ฃผ๋ฌธ์ด ์๋ฃ๋์์ต๋๋ค. ์ฃผ๋ฌธ๋ฒํธ: {0}, ๊ธ์ก: {1}์, ๋ฐฐ์ก์์ ์ผ: {2}
๊ทธ๋ฆฌ๊ณ ์๋ฐ ์ฝ๋์์:
String message = MessageFormat.format(
bundle.getString("order.confirmation"),
"ORD-12345",
"50000",
"2024-01-15"
);
๐ Locale ํด๋์ค ์์ ์ ๋ณต
ResourceBundle๊ณผ ํจ๊ป ๋ฐ๋์ ์์์ผ ํ ๊ฒ ๋ฐ๋ก Locale ํด๋์ค์ผ. Locale์ ํน์ ์ง์ญ์ด๋ ์ธ์ด๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด๋ก, ๊ตญ์ ํ์ ํต์ฌ ๊ฐ๋ ์ด์ง! ๐บ๏ธ
๐ฏ Locale ์์ฑ ๋ฐฉ๋ฒ
Locale ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ด:
// ๋ฐฉ๋ฒ 1: ์์ฑ์ ์ฌ์ฉ
Locale locale1 = new Locale("ko"); // ์ธ์ด๋ง
Locale locale2 = new Locale("ko", "KR"); // ์ธ์ด + ๊ตญ๊ฐ
Locale locale3 = new Locale("ko", "KR", "seoul"); // ์ธ์ด + ๊ตญ๊ฐ + ๋ณํ
// ๋ฐฉ๋ฒ 2: ์์ ์ฌ์ฉ (์์ฃผ ์ฐ์ด๋ ๋ก์ผ์ผ)
Locale locale4 = Locale.KOREA;
Locale locale5 = Locale.US;
Locale locale6 = Locale.JAPAN;
Locale locale7 = Locale.CHINA;
// ๋ฐฉ๋ฒ 3: Locale.Builder ์ฌ์ฉ (Java 7+, ๊ถ์ฅ)
Locale locale8 = new Locale.Builder()
.setLanguage("ko")
.setRegion("KR")
.setVariant("seoul")
.build();
// ๋ฐฉ๋ฒ 4: forLanguageTag ์ฌ์ฉ (BCP 47 ํ์ค)
Locale locale9 = Locale.forLanguageTag("ko-KR");
Locale locale10 = Locale.forLanguageTag("en-US");
// ์์คํ
๊ธฐ๋ณธ ๋ก์ผ์ผ ๊ฐ์ ธ์ค๊ธฐ
Locale defaultLocale = Locale.getDefault();
System.out.println("์์คํ
๊ธฐ๋ณธ ๋ก์ผ์ผ: " + defaultLocale);
// ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ ๋ก์ผ์ผ ํ์ธ
Locale[] availableLocales = Locale.getAvailableLocales();
System.out.println("์ฌ์ฉ ๊ฐ๋ฅํ ๋ก์ผ์ผ ์: " + availableLocales.length);
Locale.Builder๋ forLanguageTag ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ์ข์. ๋ ๋ช
ํํ๊ณ ํ์ค์ ๋ฐ๋ฅด๊ฑฐ๋ !
๐ Locale ์ ๋ณด ํ์ฉํ๊ธฐ
Locale ๊ฐ์ฒด์์ ๋ค์ํ ์ ๋ณด๋ฅผ ์ถ์ถํ ์ ์์ด:
public class LocaleInfoDemo {
public static void main(String[] args) {
Locale locale = new Locale("ko", "KR");
// ๊ธฐ๋ณธ ์ ๋ณด
System.out.println("์ธ์ด ์ฝ๋: " + locale.getLanguage()); // ko
System.out.println("๊ตญ๊ฐ ์ฝ๋: " + locale.getCountry()); // KR
System.out.println("๋ณํ: " + locale.getVariant()); // (๋น์ด์์)
// ํ์ ์ด๋ฆ (ํ์ฌ ๋ก์ผ์ผ ๊ธฐ์ค)
System.out.println("์ธ์ด ์ด๋ฆ: " + locale.getDisplayLanguage()); // ํ๊ตญ์ด
System.out.println("๊ตญ๊ฐ ์ด๋ฆ: " + locale.getDisplayCountry()); // ๋ํ๋ฏผ๊ตญ
System.out.println("์ ์ฒด ์ด๋ฆ: " + locale.getDisplayName()); // ํ๊ตญ์ด (๋ํ๋ฏผ๊ตญ)
// ๋ค๋ฅธ ๋ก์ผ์ผ ๊ธฐ์ค์ผ๋ก ํ์
System.out.println("\n์์ด๋ก ํ์:");
System.out.println("์ธ์ด: " + locale.getDisplayLanguage(Locale.ENGLISH)); // Korean
System.out.println("๊ตญ๊ฐ: " + locale.getDisplayCountry(Locale.ENGLISH)); // South Korea
// ISO ์ฝ๋
System.out.println("\nISO ์ฝ๋:");
System.out.println("ISO3 ์ธ์ด: " + locale.getISO3Language()); // kor
System.out.println("ISO3 ๊ตญ๊ฐ: " + locale.getISO3Country()); // KOR
}
}
๐จ ๊ณ ๊ธ ๊ธฐ๋ฅ: ListResourceBundle ํ์ฉํ๊ธฐ
์์์ properties ํ์ผ์ ์ฌ์ฉํ๋ PropertyResourceBundle์ ๋ดค๋๋ฐ, ์ด๋ฒ์ ์๋ฐ ํด๋์ค๋ก ์ง์ ๊ตฌํํ๋ ListResourceBundle์ ์์๋ณผ๊ฒ! ๐
ListResourceBundle์ properties ํ์ผ๋ณด๋ค ๋ ๋ณต์กํ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ์ ์์ด. ๋ฌธ์์ด๋ฟ๋ง ์๋๋ผ ๋ฐฐ์ด, ๋ฆฌ์คํธ, ์ฌ์ง์ด ์ปค์คํ ๊ฐ์ฒด๋ ๊ฐ๋ฅํ์ง! ๐
๐ ListResourceBundle ๊ตฌํ ์์
import java.util.ListResourceBundle;
// ์์ด ๋ฆฌ์์ค
public class MyResources extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][] {
// ๋ฌธ์์ด
{"greeting", "Hello!"},
{"farewell", "Goodbye!"},
// ๋ฐฐ์ด
{"weekdays", new String[] {
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"
}},
// ์ซ์
{"maxUsers", 1000},
// ๋ณต์กํ ๊ฐ์ฒด
{"colors", new String[] {"Red", "Green", "Blue"}},
// ์ค์ฒฉ ๋ฐฐ์ด
{"matrix", new int[][] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}}
};
}
}
import java.util.ListResourceBundle;
// ํ๊ตญ์ด ๋ฆฌ์์ค
public class MyResources_ko extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][] {
{"greeting", "์๋
ํ์ธ์!"},
{"farewell", "์๋
ํ ๊ฐ์ธ์!"},
{"weekdays", new String[] {
"์์์ผ", "ํ์์ผ", "์์์ผ",
"๋ชฉ์์ผ", "๊ธ์์ผ", "ํ ์์ผ", "์ผ์์ผ"
}},
{"maxUsers", 1000},
{"colors", new String[] {"๋นจ๊ฐ", "์ด๋ก", "ํ๋"}},
{"matrix", new int[][] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}}
};
}
}
๐ป ListResourceBundle ์ฌ์ฉํ๊ธฐ
import java.util.Locale;
import java.util.ResourceBundle;
public class ListResourceBundleDemo {
public static void main(String[] args) {
// ํ๊ตญ์ด ๋ก์ผ์ผ
Locale locale = new Locale("ko");
ResourceBundle bundle = ResourceBundle.getBundle("MyResources", locale);
// ๋ฌธ์์ด ๊ฐ์ ธ์ค๊ธฐ
System.out.println(bundle.getString("greeting"));
// ๋ฐฐ์ด ๊ฐ์ ธ์ค๊ธฐ
String[] weekdays = (String[]) bundle.getObject("weekdays");
System.out.println("์์ผ๋ค:");
for (String day : weekdays) {
System.out.println(" - " + day);
}
// ์ซ์ ๊ฐ์ ธ์ค๊ธฐ
int maxUsers = (Integer) bundle.getObject("maxUsers");
System.out.println("์ต๋ ์ฌ์ฉ์ ์: " + maxUsers);
// 2์ฐจ์ ๋ฐฐ์ด ๊ฐ์ ธ์ค๊ธฐ
int[][] matrix = (int[][]) bundle.getObject("matrix");
System.out.println("ํ๋ ฌ:");
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}
- ํ ์คํธ ํ์ผ์ด๋ผ ํธ์ง์ด ์ฌ์
- ์ปดํ์ผ ์์ด ์์ ๊ฐ๋ฅ
- ๋ฒ์ญ๊ฐ๊ฐ ์ง์ ์์ ํ๊ธฐ ์ข์
- ๋ฒ์ ๊ด๋ฆฌ๊ฐ ํธํจ
- ๋ฌธ์์ด ์ธ์ ๊ฐ์ฒด ์ ์ฅ ๊ฐ๋ฅ
- ํ์ ์์ ์ฑ ํ๋ณด
- ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์ง์
- ์ฑ๋ฅ์ด ์ฝ๊ฐ ๋ ์ข์ (ํ์ฑ ๋ถํ์)
๐ ResourceBundle ํด๋ฐฑ ๋ฉ์ปค๋์ฆ ์ดํดํ๊ธฐ
ResourceBundle์ ๊ฐ์ฅ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ ์ค ํ๋๊ฐ ๋ฐ๋ก ํด๋ฐฑ(Fallback) ๋ฉ์ปค๋์ฆ์ด์ผ! ์ด๊ฒ ์ด๋ป๊ฒ ์๋ํ๋์ง ์์ธํ ์์๋ณด์. ๐ฏ
์๋ฅผ ๋ค์ด, ์บ๋๋ค ํ๋์ค์ด(fr_CA) ๋ก์ผ์ผ๋ก "messages" ๋ฒ๋ค์ ์์ฒญํ๋ค๊ณ ๊ฐ์ ํด๋ณด์. ResourceBundle์ ๋ค์ ์์๋ก ํ์ผ์ ์ฐพ์:
๐ ๊ฒ์ ์์
1๋จ๊ณ: messages_fr_CA.properties (๊ฐ์ฅ ๊ตฌ์ฒด์ )
2๋จ๊ณ: messages_fr.properties (์ธ์ด๋ง)
3๋จ๊ณ: messages.properties (๊ธฐ๋ณธ)
4๋จ๊ณ: ์ฐพ์ง ๋ชปํ๋ฉด MissingResourceException ๋ฐ์!
์ด ๊ณผ์ ์์ ListResourceBundle์ด PropertyResourceBundle๋ณด๋ค ์ฐ์ ์์๊ฐ ๋์. ์ฆ, messages_fr_CA.class๊ฐ ์์ผ๋ฉด messages_fr_CA.properties๋ณด๋ค ๋จผ์ ์ฌ์ฉ๋ผ!
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
public class FallbackDemo {
public static void main(String[] args) {
// ์กด์ฌํ์ง ์๋ ๋ก์ผ์ผ ์๋
Locale locale = new Locale("fr", "CA"); // ์บ๋๋ค ํ๋์ค์ด
try {
ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
// ์ค์ ๋ก ๋ก๋๋ ๋ก์ผ์ผ ํ์ธ
System.out.println("์์ฒญํ ๋ก์ผ์ผ: " + locale);
System.out.println("์ค์ ๋ก๋๋ ๋ก์ผ์ผ: " + bundle.getLocale());
// messages_fr_CA๊ฐ ์์ผ๋ฉด messages_fr์ ์ฐพ๊ณ
// ๊ทธ๊ฒ๋ ์์ผ๋ฉด messages๋ฅผ ์ฌ์ฉํจ
System.out.println(bundle.getString("greeting"));
} catch (MissingResourceException e) {
System.err.println("๋ฆฌ์์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: " + e.getMessage());
}
}
}
๐จ ์ปค์คํ ํด๋ฐฑ ๋ก์ง ๊ตฌํํ๊ธฐ
๋๋ก๋ ๊ธฐ๋ณธ ํด๋ฐฑ ๋ฉ์ปค๋์ฆ์ผ๋ก๋ ๋ถ์กฑํ ์ ์์ด. ์ด๋ด ๋๋ ResourceBundle.Control์ ์ฌ์ฉํด์ ์ปค์คํ
๋ก์ง์ ๊ตฌํํ ์ ์์ด! ๐ ๏ธ
import java.util.*;
import java.io.IOException;
public class CustomControl extends ResourceBundle.Control {
@Override
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
// ์ปค์คํ
ํด๋ฐฑ ์์ ์ ์
List<Locale> locales = new ArrayList<>();
if (locale.equals(new Locale("ko", "KR"))) {
locales.add(locale); // ko_KR
locales.add(new Locale("ko")); // ko
locales.add(new Locale("en")); // ์์ด๋ก ํด๋ฐฑ
locales.add(Locale.ROOT); // ๊ธฐ๋ณธ
} else {
// ๊ธฐ๋ณธ ๋์ ์ฌ์ฉ
locales = super.getCandidateLocales(baseName, locale);
}
return locales;
}
@Override
public long getTimeToLive(String baseName, Locale locale) {
// ์บ์ ์ ์ง ์๊ฐ ์ค์ (๋ฐ๋ฆฌ์ด)
// TTL_DONT_CACHE: ์บ์ ์ ํจ
// TTL_NO_EXPIRATION_CONTROL: ๋ง๋ฃ ์์
return 1000 * 60 * 60; // 1์๊ฐ
}
@Override
public boolean needsReload(String baseName, Locale locale,
String format, ClassLoader loader,
ResourceBundle bundle, long loadTime) {
// ๋ฆฌ์์ค ํ์ผ์ด ๋ณ๊ฒฝ๋์๋์ง ํ์ธ
return true; // ํญ์ ๋ฆฌ๋ก๋ (๊ฐ๋ฐ ์ค์ ์ ์ฉ)
}
}
// ์ฌ์ฉ ์์
public class CustomControlDemo {
public static void main(String[] args) {
Locale locale = new Locale("ko", "KR");
ResourceBundle.Control control = new CustomControl();
ResourceBundle bundle = ResourceBundle.getBundle(
"messages",
locale,
control
);
System.out.println(bundle.getString("greeting"));
}
}
๐ ๋ ์ง, ์๊ฐ, ์ซ์ ํฌ๋งทํ
๋ค๊ตญ์ด ์ง์์ ๋จ์ํ ํ ์คํธ ๋ฒ์ญ๋ง์ด ์๋์ผ! ๋ ์ง, ์๊ฐ, ์ซ์, ํตํ ๋ฑ๋ ๊ฐ ๋๋ผ์ ๊ด์ต์ ๋ง๊ฒ ํ์ํด์ผ ํด. ์๋ฐ๋ ์ด๋ฅผ ์ํ ๊ฐ๋ ฅํ ๋๊ตฌ๋ค์ ์ ๊ณตํ์ง! ๐
๐ ๋ ์ง์ ์๊ฐ ํฌ๋งทํ
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateFormattingDemo {
public static void main(String[] args) {
Date now = new Date();
// ํ๊ตญ์ด ๋ก์ผ์ผ
Locale koreaLocale = new Locale("ko", "KR");
DateFormat koreanDateFormat = DateFormat.getDateInstance(
DateFormat.LONG,
koreaLocale
);
DateFormat koreanTimeFormat = DateFormat.getTimeInstance(
DateFormat.LONG,
koreaLocale
);
DateFormat koreanDateTimeFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG,
DateFormat.LONG,
koreaLocale
);
System.out.println("=== ํ๊ตญ์ด ===");
System.out.println("๋ ์ง: " + koreanDateFormat.format(now));
System.out.println("์๊ฐ: " + koreanTimeFormat.format(now));
System.out.println("๋ ์ง+์๊ฐ: " + koreanDateTimeFormat.format(now));
// ๋ฏธ๊ตญ ์์ด ๋ก์ผ์ผ
Locale usLocale = Locale.US;
DateFormat usDateFormat = DateFormat.getDateInstance(
DateFormat.LONG,
usLocale
);
DateFormat usTimeFormat = DateFormat.getTimeInstance(
DateFormat.LONG,
usLocale
);
System.out.println("\n=== English (US) ===");
System.out.println("Date: " + usDateFormat.format(now));
System.out.println("Time: " + usTimeFormat.format(now));
// ์ผ๋ณธ์ด ๋ก์ผ์ผ
Locale japanLocale = Locale.JAPAN;
DateFormat japanDateFormat = DateFormat.getDateInstance(
DateFormat.LONG,
japanLocale
);
System.out.println("\n=== ๆฅๆฌ่ช ===");
System.out.println("ๆฅไป: " + japanDateFormat.format(now));
// ์ปค์คํ
ํจํด ์ฌ์ฉ
SimpleDateFormat customFormat = new SimpleDateFormat(
"yyyy๋
MM์ dd์ผ E์์ผ",
koreaLocale
);
System.out.println("\n์ปค์คํ
ํฌ๋งท: " + customFormat.format(now));
}
}
java.time ํจํค์ง์ DateTimeFormatter๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๋ ์ข์! ๋ ์ง๊ด์ ์ด๊ณ ๋ถ๋ณ ๊ฐ์ฒด๋ผ ์ค๋ ๋ ์์ ํด.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
public class ModernDateFormattingDemo {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
// ํ๊ตญ์ด
DateTimeFormatter koreanFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.LONG)
.withLocale(new Locale("ko", "KR"));
System.out.println("ํ๊ตญ์ด: " + now.format(koreanFormatter));
// ์์ด
DateTimeFormatter englishFormatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.LONG)
.withLocale(Locale.US);
System.out.println("English: " + now.format(englishFormatter));
// ์ปค์คํ
ํจํด
DateTimeFormatter customFormatter = DateTimeFormatter
.ofPattern("yyyy๋
M์ d์ผ (E) a h:mm", new Locale("ko", "KR"));
System.out.println("์ปค์คํ
: " + now.format(customFormatter));
}
}
๐ฐ ์ซ์์ ํตํ ํฌ๋งทํ
์ซ์ ํ๊ธฐ๋ฒ๋ ๋๋ผ๋ง๋ค ๋ฌ๋ผ. ํ๊ตญ์ 1,000,000์์ด์ง๋ง ๋ฏธ๊ตญ์ $1,000,000.00์ด๊ณ , ์ ๋ฝ ์ผ๋ถ ๊ตญ๊ฐ๋ 1.000.000,00โฌ ์ด๋ฐ ์์ด์ผ! ๐
import java.text.NumberFormat;
import java.util.Locale;
public class NumberFormattingDemo {
public static void main(String[] args) {
double number = 1234567.89;
double currency = 1234567.89;
double percent = 0.75;
// ํ๊ตญ
Locale koreaLocale = new Locale("ko", "KR");
NumberFormat koreanNumberFormat = NumberFormat.getNumberInstance(koreaLocale);
NumberFormat koreanCurrencyFormat = NumberFormat.getCurrencyInstance(koreaLocale);
NumberFormat koreanPercentFormat = NumberFormat.getPercentInstance(koreaLocale);
System.out.println("=== ํ๊ตญ ===");
System.out.println("์ซ์: " + koreanNumberFormat.format(number));
System.out.println("ํตํ: " + koreanCurrencyFormat.format(currency));
System.out.println("ํผ์ผํธ: " + koreanPercentFormat.format(percent));
// ๋ฏธ๊ตญ
Locale usLocale = Locale.US;
NumberFormat usNumberFormat = NumberFormat.getNumberInstance(usLocale);
NumberFormat usCurrencyFormat = NumberFormat.getCurrencyInstance(usLocale);
NumberFormat usPercentFormat = NumberFormat.getPercentInstance(usLocale);
System.out.println("\n=== USA ===");
System.out.println("Number: " + usNumberFormat.format(number));
System.out.println("Currency: " + usCurrencyFormat.format(currency));
System.out.println("Percent: " + usPercentFormat.format(percent));
// ๋
์ผ
Locale germanyLocale = Locale.GERMANY;
NumberFormat germanNumberFormat = NumberFormat.getNumberInstance(germanyLocale);
NumberFormat germanCurrencyFormat = NumberFormat.getCurrencyInstance(germanyLocale);
System.out.println("\n=== Deutschland ===");
System.out.println("Nummer: " + germanNumberFormat.format(number));
System.out.println("Wรคhrung: " + germanCurrencyFormat.format(currency));
// ์์์ ์๋ฆฟ์ ์กฐ์
NumberFormat customFormat = NumberFormat.getNumberInstance(koreaLocale);
customFormat.setMinimumFractionDigits(2);
customFormat.setMaximumFractionDigits(4);
System.out.println("\n์ปค์คํ
ํฌ๋งท: " + customFormat.format(number));
}
}
๐ฏ ์ค์ ํ๋ก์ ํธ: ๋ค๊ตญ์ด ์ง์ ์น ์ ํ๋ฆฌ์ผ์ด์
์ด์ ๋ฐฐ์ด ๋ด์ฉ์ ์ข ํฉํด์ ์ค์ ์น ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ์์๋ณด์! ๐ Spring Framework๋ฅผ ์ฌ์ฉํ๋ ์์๋ฅผ ๋ณด์ฌ์ค๊ฒ.
๐ธ Spring Boot์์ ๊ตญ์ ํ ์ค์ ํ๊ธฐ
# ๊ธฐ๋ณธ ๋ก์ผ์ผ ์ค์ spring.web.locale=ko_KR # ๋ก์ผ์ผ ๋ณ๊ฒฝ ํ๋ผ๋ฏธํฐ ์ด๋ฆ spring.web.locale-resolver=fixed # ๋ฉ์์ง ์์ค ๋ฒ ์ด์ค๋ค์ spring.messages.basename=messages # ์ธ์ฝ๋ฉ spring.messages.encoding=UTF-8 # ์บ์ ์๊ฐ (์ด, -1์ ์๊ตฌ) spring.messages.cache-duration=3600
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@Configuration
public class LocaleConfiguration implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(new Locale("ko", "KR"));
return resolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
// URL ํ๋ผ๋ฏธํฐ๋ก ์ธ์ด ๋ณ๊ฒฝ: ?lang=en
interceptor.setParamName("lang");
return interceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
๐จ Controller์์ ์ฌ์ฉํ๊ธฐ
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Locale;
@Controller
public class HomeController {
@Autowired
private MessageSource messageSource;
@GetMapping("/")
public String home(Locale locale, Model model) {
// ํ์ฌ ๋ก์ผ์ผ์ ๋ง๋ ๋ฉ์์ง ๊ฐ์ ธ์ค๊ธฐ
String greeting = messageSource.getMessage(
"greeting",
new Object[]{"์ฌ์ฉ์"},
locale
);
String welcomeMessage = messageSource.getMessage(
"welcome.message",
null,
locale
);
model.addAttribute("greeting", greeting);
model.addAttribute("welcomeMessage", welcomeMessage);
model.addAttribute("currentLocale", locale);
return "home";
}
@GetMapping("/profile")
public String profile(@RequestParam String username,
Locale locale,
Model model) {
// ๋์ ๋ฉ์์ง ์์ฑ
String profileMessage = messageSource.getMessage(
"profile.welcome",
new Object[]{username},
"Welcome to your profile, " + username, // ๊ธฐ๋ณธ๊ฐ
locale
);
model.addAttribute("message", profileMessage);
return "profile";
}
}
๐ผ๏ธ Thymeleaf ํ ํ๋ฆฟ์์ ์ฌ์ฉํ๊ธฐ
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="#{page.title}">Home</title>
</head>
<body>
<!-- ์ธ์ด ์ ํ ๋ฉ๋ด -->
<div class="language-selector">
<a th:href="@{/(lang=ko)}">ํ๊ตญ์ด</a> |
<a th:href="@{/(lang=en)}">English</a> |
<a th:href="@{/(lang=ja)}">ๆฅๆฌ่ช</a> |
<a th:href="@{/(lang=zh)}">ไธญๆ</a>
</div>
<!-- ๋ฉ์์ง ํ์ -->
<h1 th:text="${greeting}">Greeting</h1>
<p th:text="${welcomeMessage}">Welcome Message</p>
<!-- ์ง์ ๋ฉ์์ง ํค ์ฌ์ฉ -->
<h2 th:text="#{menu.home}">Home</h2>
<!-- ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๋ฉ์์ง -->
<p th:text="#{user.registered(${username}, ${date})}">
User registered message
</p>
<!-- ํ์ฌ ๋ก์ผ์ผ ํ์ -->
<p>Current Locale: <span th:text="${currentLocale}"></span></p>
<!-- ๋ฒํผ -->
<button th:text="#{button.submit}">Submit</button>
<button th:text="#{button.cancel}">Cancel</button>
</body>
</html>
โก ์ฑ๋ฅ ์ต์ ํ์ ๋ฒ ์คํธ ํ๋ํฐ์ค
๋ค๊ตญ์ด ์ง์์ ๊ตฌํํ ๋ ์ฑ๋ฅ๊ณผ ์ ์ง๋ณด์์ฑ์ ๊ณ ๋ คํด์ผ ํด. ๋ช ๊ฐ์ง ์ค์ํ ํ์ ์๋ ค์ค๊ฒ! ๐ฏ
๐ ์ฑ๋ฅ ์ต์ ํ ์ ๋ต
1๏ธโฃ ResourceBundle ์บ์ฑ ํ์ฉ
ResourceBundle์ ๊ธฐ๋ณธ์ ์ผ๋ก ์บ์ฑ๋์ง๋ง, ๊ฐ๋ฐ ์ค์๋ ๋ณ๊ฒฝ์ฌํญ์ ์ฆ์ ๋ฐ์ํ๊ณ ์ถ์ ์ ์์ด. ์ด๋ด ๋๋ ResourceBundle.clearCache()๋ฅผ ์ฌ์ฉํ๊ฑฐ๋, ์ปค์คํ
Control์ ๊ตฌํํด.
// ์บ์ ํด๋ฆฌ์ด ResourceBundle.clearCache(); // ํน์ ํด๋์ค๋ก๋์ ์บ์๋ง ํด๋ฆฌ์ด ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
2๏ธโฃ ์ง์ฐ ๋ก๋ฉ(Lazy Loading)
๋ชจ๋ ์ธ์ด์ ๋ฆฌ์์ค๋ฅผ ํ ๋ฒ์ ๋ก๋ํ์ง ๋ง๊ณ , ํ์ํ ๋๋ง ๋ก๋ํด. ํนํ ๋ง์ ์ธ์ด๋ฅผ ์ง์ํ๋ ๊ฒฝ์ฐ ์ค์ํด!
public class LocaleManager {
private Map<Locale, ResourceBundle> bundleCache = new ConcurrentHashMap<>();
public ResourceBundle getBundle(Locale locale) {
return bundleCache.computeIfAbsent(locale,
l -> ResourceBundle.getBundle("messages", l));
}
}
3๏ธโฃ ๋ฌธ์์ด ์ฐ๊ฒฐ ์ต์ํ
MessageFormat์ ์ฌ์ฉํ ๋ ๋ถํ์ํ ๋ฌธ์์ด ์ฐ๊ฒฐ์ ํผํด. ํนํ ๋ฐ๋ณต๋ฌธ ์์์๋ ๋์ฑ ์ฃผ์!
// โ ๋์ ์
String message = "";
for (int i = 0; i < items.size(); i++) {
message += MessageFormat.format(bundle.getString("item"), items.get(i));
}
// โ
์ข์ ์
StringBuilder sb = new StringBuilder();
MessageFormat format = new MessageFormat(bundle.getString("item"));
for (Item item : items) {
sb.append(format.format(new Object[]{item.getName()}));
}
String message = sb.toString();
๐ ๋ฒ ์คํธ ํ๋ํฐ์ค
1. ์ผ๊ด๋ ํค ๋ค์ด๋ฐ ๊ท์น ์ฌ์ฉ
module.component.action ํ์์ผ๋ก ๊ณ์ธต์ ์ผ๋ก ๊ด๋ฆฌํด. ์: user.profile.edit, order.payment.confirm
2. ๊ธฐ๋ณธ ์ธ์ด ํ์ผ ํญ์ ์ ๊ณต
messages.properties ํ์ผ์ ๋ฐ๋์ ๋ง๋ค์ด. ํด๋ฐฑ์ ๋ง์ง๋ง ๋ณด๋ฃจ์ผ!
3. UTF-8 ์ธ์ฝ๋ฉ ์ฌ์ฉ
Java 9+ ์์๋ properties ํ์ผ์ด UTF-8์ ๊ธฐ๋ณธ์ผ๋ก ์ง์ํด. ์ด์ ๋ฒ์ ์ native2ascii ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ XML ํ์์ ๊ณ ๋ คํด.
4. ๋ฌธ๋งฅ ์ ๋ณด ์ ๊ณต
๋ฒ์ญ๊ฐ๋ฅผ ์ํด ์ฃผ์์ผ๋ก ๋ฌธ๋งฅ ์ ๋ณด๋ฅผ ์ ๊ณตํด:
# ์ฌ์ฉ์ ํ๋กํ ํ์ด์ง์ ํ์ ๋ฉ์์ง
# {0}: ์ฌ์ฉ์ ์ด๋ฆ
user.welcome=Welcome back, {0}!
# ์ฃผ๋ฌธ ํ์ธ ๋ฒํผ (๊ฒฐ์ ํ์ด์ง)
order.confirm.button=Confirm Order
5. ๋ณต์ํ ์ฒ๋ฆฌ
ChoiceFormat์ ์ฌ์ฉํด์ ๋ณต์ํ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ฒ๋ฆฌํด:
# ์์ด
items.count={0,choice,0#No items|1#One item|1<{0,number,integer} items}
# ํ๊ตญ์ด (๋ณต์ํ ๊ตฌ๋ถ ์์)
items.count={0}๊ฐ์ ํญ๋ชฉ
1. ํ๋์ฝ๋ฉ ์ ๋ ๊ธ์ง
์ฝ๋์ ์ง์ ๋ฌธ์์ด์ ๋ฃ์ง ๋ง! ๋์ค์ ์ฐพ์์ ๋ฐ๊พธ๊ธฐ ์ ๋ง ํ๋ค์ด.
2. ๋ฌธ์์ด ์ฐ๊ฒฐ๋ก ๋ฌธ์ฅ ๋ง๋ค๊ธฐ
์ธ์ด๋ง๋ค ์ด์์ด ๋ค๋ฅด๋๊น ๋ฌธ์์ด์ ์กฐ๊ฐ๋ด์ ์ฐ๊ฒฐํ๋ฉด ์ ๋ผ:
// โ ๋์ ์
String message = bundle.getString("hello") + " " + username + " " + bundle.getString("welcome");
// โ
์ข์ ์
String message = MessageFormat.format(bundle.getString("greeting"), username);
3. ๋ ์ง/์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํ ํ ์ ์ฅ
ํญ์ ์๋ณธ ๋ฐ์ดํฐ ํ์
์ ์ ์งํ๊ณ , ํ์ํ ๋๋ง ํฌ๋งทํ
ํด.
4. ๋ก์ผ์ผ์ ํ๋์ฝ๋ฉ
new Locale("ko", "KR") ๊ฐ์ ์ฝ๋๋ฅผ ์ฌ๊ธฐ์ ๊ธฐ ํฉ๋ฟ๋ฆฌ์ง ๋ง. ์ค์์์ ๊ด๋ฆฌํด!
๐ง ์ค๋ฌด ํ: ๋ฒ์ญ ๊ด๋ฆฌ ์ ๋ต
์ค์ ํ๋ก์ ํธ์์๋ ์๋ฐฑ, ์์ฒ ๊ฐ์ ๋ฒ์ญ ํค๋ฅผ ๊ด๋ฆฌํด์ผ ํด. ํจ์จ์ ์ธ ๊ด๋ฆฌ ์ ๋ต์ด ํ์์ผ! ๐
๐ ํ์ผ ๊ตฌ์กฐ ์กฐ์งํ
๋ชจ๋ ๋ฒ์ญ์ ํ๋์ ํ์ผ์ ๋ฃ์ผ๋ฉด ๊ด๋ฆฌ๊ฐ ์ด๋ ค์. ๋ชจ๋๋ณ๋ก ๋ถ๋ฆฌํ๋ ๊ฒ ์ข์:
resources/ โโโ i18n/ โ โโโ common/ โ โ โโโ messages.properties โ โ โโโ messages_ko.properties โ โ โโโ messages_en.properties โ โโโ user/ โ โ โโโ user.properties โ โ โโโ user_ko.properties โ โ โโโ user_en.properties โ โโโ order/ โ โ โโโ order.properties โ โ โโโ order_ko.properties โ โ โโโ order_en.properties โ โโโ error/ โ โโโ errors.properties โ โโโ errors_ko.properties โ โโโ errors_en.properties
// ์ฌ๋ฌ ๋ฒ๋ค์ ์กฐํฉํด์ ์ฌ์ฉ
public class MultiResourceBundleManager {
private List<ResourceBundle> bundles;
public MultiResourceBundleManager(Locale locale) {
bundles = Arrays.asList(
ResourceBundle.getBundle("i18n/common/messages", locale),
ResourceBundle.getBundle("i18n/user/user", locale),
ResourceBundle.getBundle("i18n/order/order", locale),
ResourceBundle.getBundle("i18n/error/errors", locale)
);
}
public String getString(String key) {
for (ResourceBundle bundle : bundles) {
try {
return bundle.getString(key);
} catch (MissingResourceException e) {
// ๋ค์ ๋ฒ๋ค์์ ์ฐพ๊ธฐ
}
}
throw new MissingResourceException(
"Key not found in any bundle",
"MultiResourceBundle",
key
);
}
}
๐ ๏ธ ๋ฒ์ญ ๊ฒ์ฆ ๋๊ตฌ ๋ง๋ค๊ธฐ
๋ฒ์ญ์ด ๋๋ฝ๋๊ฑฐ๋ ์๋ชป๋ ๊ฑธ ์๋์ผ๋ก ์ฐพ์์ฃผ๋ ๋๊ตฌ๋ฅผ ๋ง๋ค๋ฉด ์ ๋ง ์ ์ฉํด! ๐
import java.io.*;
import java.util.*;
public class TranslationValidator {
public static void main(String[] args) throws IOException {
String basePath = "src/main/resources/";
String baseName = "messages";
// ๊ธฐ๋ณธ ํ์ผ ๋ก๋
Properties baseProps = loadProperties(basePath + baseName + ".properties");
// ๋ค๋ฅธ ์ธ์ด ํ์ผ๋ค ๊ฒ์ฆ
String[] locales = {"ko", "ja", "zh", "en"};
for (String locale : locales) {
String fileName = baseName + "_" + locale + ".properties";
Properties localeProps = loadProperties(basePath + fileName);
System.out.println("\n=== " + locale + " ๊ฒ์ฆ ===");
// ๋๋ฝ๋ ํค ์ฐพ๊ธฐ
Set<String> missingKeys = new HashSet<>(baseProps.stringPropertyNames());
missingKeys.removeAll(localeProps.stringPropertyNames());
if (!missingKeys.isEmpty()) {
System.out.println("โ ๋๋ฝ๋ ํค (" + missingKeys.size() + "๊ฐ):");
missingKeys.forEach(key -> System.out.println(" - " + key));
}
// ์ถ๊ฐ๋ ํค ์ฐพ๊ธฐ (๊ธฐ๋ณธ ํ์ผ์ ์๋ ํค)
Set<String> extraKeys = new HashSet<>(localeProps.stringPropertyNames());
extraKeys.removeAll(baseProps.stringPropertyNames());
if (!extraKeys.isEmpty()) {
System.out.println("โ ๏ธ ์ถ๊ฐ๋ ํค (" + extraKeys.size() + "๊ฐ):");
extraKeys.forEach(key -> System.out.println(" - " + key));
}
// ๋น ๊ฐ ์ฐพ๊ธฐ
List<String> emptyValues = new ArrayList<>();
for (String key : localeProps.stringPropertyNames()) {
if (localeProps.getProperty(key).trim().isEmpty()) {
emptyValues.add(key);
}
}
if (!emptyValues.isEmpty()) {
System.out.println("โ ๏ธ ๋น ๊ฐ (" + emptyValues.size() + "๊ฐ):");
emptyValues.forEach(key -> System.out.println(" - " + key));
}
if (missingKeys.isEmpty() && extraKeys.isEmpty() && emptyValues.isEmpty()) {
System.out.println("โ
๋ชจ๋ ๊ฒ์ฆ ํต๊ณผ!");
}
}
}
private static Properties loadProperties(String filePath) throws IOException {
Properties props = new Properties();
try (InputStream input = new FileInputStream(filePath)) {
props.load(input);
}
return props;
}
}
๐ ๋ฒ์ญ ๊ด๋ฆฌ ํ๋ซํผ ํ์ฉ
๋๊ท๋ชจ ํ๋ก์ ํธ์์๋ ์ ๋ฌธ ๋ฒ์ญ ๊ด๋ฆฌ ํ๋ซํผ์ ์ฌ์ฉํ๋ ๊ฒ ์ข์. ๋ช ๊ฐ์ง ์ถ์ฒํ ๊ฒ:
๐น Crowdin ๊ฐ๋ฐ์์ ๋ฒ์ญ๊ฐ๊ฐ ํ์ ํ๊ธฐ ์ข์ ํ๋ซํผ. GitHub ์ฐ๋์ด ์ ๋์ด ์์ด์ ์๋ํํ๊ธฐ ํธํด.
๐น Lokalise
UI๊ฐ ์ง๊ด์ ์ด๊ณ , ๋ฒ์ญ ๋ฉ๋ชจ๋ฆฌ ๊ธฐ๋ฅ์ด ๊ฐ๋ ฅํด. API๋ ์ ๋์ด ์์ด์ CI/CD์ ํตํฉํ๊ธฐ ์ข์.
๐น Phrase
์ํฐํ๋ผ์ด์ฆ๊ธ ๊ธฐ๋ฅ์ด ๋ง๊ณ , ํ์ง ๊ด๋ฆฌ ๋๊ตฌ๊ฐ ํ๋ฅญํด. ๋๊ท๋ชจ ํ์ ์ ํฉํด.
๐น POEditor
๊ฐ๊ฒฉ์ด ์ ๋ ดํ๊ณ ์ฌ์ฉํ๊ธฐ ์ฌ์. ์๊ท๋ชจ ํ๋ก์ ํธ๋ ์คํํธ์
์ ์ข์.
๐ ํํ ๋ฌธ์ ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
๋ค๊ตญ์ด ์ง์์ ๊ตฌํํ๋ค ๋ณด๋ฉด ์ฌ๋ฌ ๊ฐ์ง ๋ฌธ์ ์ ๋ถ๋ชํ ์ ์์ด. ์์ฃผ ๋ฐ์ํ๋ ๋ฌธ์ ๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์์๋ณด์! ๐ง
โ ๋ฌธ์ 1: ํ๊ธ์ด ๊นจ์ ธ์!
Java 8 ์ดํ: native2ascii ๋๊ตฌ๋ฅผ ์ฌ์ฉํด์ ์ ๋์ฝ๋ ์ด์ค์ผ์ดํ ์ํ์ค๋ก ๋ณํํด์ผ ํด.
# ๋ช ๋ น์ด native2ascii -encoding UTF-8 messages_ko.properties messages_ko_escaped.properties # ๋๋ IntelliJ IDEA ์ค์ ์์ # Settings โ Editor โ File Encodings โ Transparent native-to-ascii conversion ์ฒดํฌ
Java 9 ์ด์: properties ํ์ผ์ด UTF-8์ ๊ธฐ๋ณธ์ผ๋ก ์ง์ํ๋๊น ๊ทธ๋ฅ ์ฌ์ฉํ๋ฉด ๋ผ!
Spring Boot: application.properties์ ์ธ์ฝ๋ฉ ์ค์ ์ถ๊ฐ:
spring.messages.encoding=UTF-8
โ ๋ฌธ์ 2: MissingResourceException์ด ๋ฐ์ํด์!
java.util.MissingResourceException: Can't find bundle ์๋ฌ๊ฐ ๋์.
1. ํ์ผ ์์น ํ์ธ
properties ํ์ผ์ด ํด๋์คํจ์ค์ ์๋์ง ํ์ธํด. Maven/Gradle ํ๋ก์ ํธ๋ผ๋ฉด src/main/resources์ ์์ด์ผ ํด.
2. ํ์ผ ์ด๋ฆ ํ์ธ
basename_์ธ์ด_๊ตญ๊ฐ.properties ํ์์ด ์ ํํ์ง ํ์ธํด. ๋์๋ฌธ์๋ ์ค์ํด!
3. ๊ธฐ๋ณธ ํ์ผ ํ์ธ
messages.properties (์ธ์ด ์ฝ๋ ์๋ ๊ธฐ๋ณธ ํ์ผ)์ด ์๋์ง ํ์ธํด.
4. ๋น๋ ์ค์ ํ์ธ
Maven์ด๋ Gradle์์ ๋ฆฌ์์ค ํ์ผ์ด ์ ๋๋ก ๋ณต์ฌ๋๋์ง ํ์ธํด:
<!-- Maven pom.xml -->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
โ ๋ฌธ์ 3: ๋ก์ผ์ผ์ด ์๋์ผ๋ก ๋ณ๊ฒฝ๋์ง ์์์!
์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฒฝ์ฐ: LocaleResolver๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ค์ ํ๋์ง ํ์ธํด.
@Bean
public LocaleResolver localeResolver() {
// AcceptHeaderLocaleResolver: HTTP Accept-Language ํค๋ ์ฌ์ฉ
AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
resolver.setDefaultLocale(Locale.KOREAN);
return resolver;
// ๋๋ SessionLocaleResolver: ์ธ์
์ ์ ์ฅ
// SessionLocaleResolver resolver = new SessionLocaleResolver();
// resolver.setDefaultLocale(Locale.KOREAN);
// return resolver;
// ๋๋ CookieLocaleResolver: ์ฟ ํค์ ์ ์ฅ
// CookieLocaleResolver resolver = new CookieLocaleResolver();
// resolver.setDefaultLocale(Locale.KOREAN);
// resolver.setCookieName("language");
// resolver.setCookieMaxAge(3600);
// return resolver;
}
โ ๋ฌธ์ 4: MessageFormat ํ๋ผ๋ฏธํฐ๊ฐ ์ ๋๋ก ์นํ๋์ง ์์์!
{0}, {1} ๊ฐ์ ํ๋ ์ด์คํ๋๊ฐ ๊ทธ๋๋ก ์ถ๋ ฅ๋ผ์.
1. MessageFormat ์ฌ์ฉ ํ์ธ
๋จ์ํ getString()๋ง ํธ์ถํ๋ฉด ์นํ์ด ์ ๋ผ. MessageFormat.format()์ ์ฌ์ฉํด์ผ ํด!
// โ ์๋ชป๋ ๋ฐฉ๋ฒ
String message = bundle.getString("greeting");
// โ
์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ
String pattern = bundle.getString("greeting");
String message = MessageFormat.format(pattern, "ํ๊ธธ๋");
2. ์์๋ฐ์ดํ ์ด์ค์ผ์ดํ
MessageFormat์์ ์์๋ฐ์ดํ(')๋ ํน์ ๋ฌธ์์ผ. ๋ฌธ์ ๊ทธ๋๋ก ์ฌ์ฉํ๋ ค๋ฉด ๋ ๋ฒ ์จ์ผ ํด:
# โ ์๋ชป๋ ์
message=It's {0}'s birthday
# โ
์ฌ๋ฐ๋ฅธ ์
message=It''s {0}''s birthday
๐ ๊ณ ๊ธ ์ฃผ์ : ๋์ ๋ก์ผ์ผ ์ ํ
์ฌ์ฉ์๊ฐ ์ค์๊ฐ์ผ๋ก ์ธ์ด๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ ๊ธฐ๋ฅ์ ๊ตฌํํด๋ณด์! ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ฌ์ฉ์ ๊ฒฝํ์ ํฌ๊ฒ ํฅ์์ํฌ ์ ์์ด. ๐
๐ ์ค์๊ฐ ์ธ์ด ์ ํ ๊ตฌํ
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class LocaleManager {
private static LocaleManager instance;
private Locale currentLocale;
private Map<Locale, ResourceBundle> bundleCache;
private List<LocaleChangeListener> listeners;
private LocaleManager() {
this.currentLocale = Locale.getDefault();
this.bundleCache = new ConcurrentHashMap<>();
this.listeners = new ArrayList<>();
}
public static synchronized LocaleManager getInstance() {
if (instance == null) {
instance = new LocaleManager();
}
return instance;
}
public Locale getCurrentLocale() {
return currentLocale;
}
public void setCurrentLocale(Locale locale) {
if (!currentLocale.equals(locale)) {
Locale oldLocale = currentLocale;
currentLocale = locale;
notifyListeners(oldLocale, locale);
}
}
public ResourceBundle getBundle(String baseName) {
return bundleCache.computeIfAbsent(
currentLocale,
locale -> ResourceBundle.getBundle(baseName, locale)
);
}
public String getMessage(String key, Object... args) {
ResourceBundle bundle = getBundle("messages");
String pattern = bundle.getString(key);
if (args.length > 0) {
return MessageFormat.format(pattern, args);
}
return pattern;
}
public void addLocaleChangeListener(LocaleChangeListener listener) {
listeners.add(listener);
}
public void removeLocaleChangeListener(LocaleChangeListener listener) {
listeners.remove(listener);
}
private void notifyListeners(Locale oldLocale, Locale newLocale) {
LocaleChangeEvent event = new LocaleChangeEvent(oldLocale, newLocale);
for (LocaleChangeListener listener : listeners) {
listener.onLocaleChanged(event);
}
}
public void clearCache() {
bundleCache.clear();
ResourceBundle.clearCache();
}
}
// ๋ฆฌ์ค๋ ์ธํฐํ์ด์ค
interface LocaleChangeListener {
void onLocaleChanged(LocaleChangeEvent event);
}
// ์ด๋ฒคํธ ํด๋์ค
class LocaleChangeEvent {
private final Locale oldLocale;
private final Locale newLocale;
public LocaleChangeEvent(Locale oldLocale, Locale newLocale) {
this.oldLocale = oldLocale;
this.newLocale = newLocale;
}
public Locale getOldLocale() { return oldLocale; }
public Locale getNewLocale() { return newLocale; }
}
๐จ GUI ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉํ๊ธฐ
import javax.swing.*;
import java.awt.*;
import java.util.Locale;
public class MultilingualApp extends JFrame implements LocaleChangeListener {
private JLabel titleLabel;
private JButton submitButton;
private JButton cancelButton;
private JComboBox<String> languageCombo;
private LocaleManager localeManager;
public MultilingualApp() {
localeManager = LocaleManager.getInstance();
localeManager.addLocaleChangeListener(this);
initComponents();
updateTexts();
}
private void initComponents() {
setTitle("Multilingual Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(10, 10));
// ์ธ์ด ์ ํ ํจ๋
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Language:"));
String[] languages = {"ํ๊ตญ์ด", "English", "ๆฅๆฌ่ช", "ไธญๆ"};
languageCombo = new JComboBox<>(languages);
languageCombo.addActionListener(e -> changeLanguage());
topPanel.add(languageCombo);
add(topPanel, BorderLayout.NORTH);
// ๋ฉ์ธ ์ปจํ
์ธ
JPanel centerPanel = new JPanel(new GridLayout(3, 1, 10, 10));
centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
titleLabel = new JLabel();
titleLabel.setFont(new Font("Dialog", Font.BOLD, 18));
centerPanel.add(titleLabel);
submitButton = new JButton();
cancelButton = new JButton();
centerPanel.add(submitButton);
centerPanel.add(cancelButton);
add(centerPanel, BorderLayout.CENTER);
pack();
setSize(400, 250);
setLocationRelativeTo(null);
}
private void changeLanguage() {
int index = languageCombo.getSelectedIndex();
Locale newLocale;
switch (index) {
case 0: newLocale = new Locale("ko", "KR"); break;
case 1: newLocale = Locale.US; break;
case 2: newLocale = Locale.JAPAN; break;
case 3: newLocale = Locale.CHINA; break;
default: newLocale = Locale.getDefault();
}
localeManager.setCurrentLocale(newLocale);
}
private void updateTexts() {
titleLabel.setText(localeManager.getMessage("welcome.message"));
submitButton.setText(localeManager.getMessage("button.submit"));
cancelButton.setText(localeManager.getMessage("button.cancel"));
}
@Override
public void onLocaleChanged(LocaleChangeEvent event) {
// ๋ก์ผ์ผ์ด ๋ณ๊ฒฝ๋๋ฉด ๋ชจ๋ ํ
์คํธ ์
๋ฐ์ดํธ
SwingUtilities.invokeLater(this::updateTexts);
System.out.println("Language changed from " +
event.getOldLocale().getDisplayLanguage() + " to " +
event.getNewLocale().getDisplayLanguage());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new MultilingualApp().setVisible(true);
});
}
}
๐ ์ค์ ์์ : ์์ ํ ๋ค๊ตญ์ด ์ง์ ์์คํ
์ง๊ธ๊น์ง ๋ฐฐ์ด ๋ด์ฉ์ ๋ชจ๋ ์ข ํฉํด์ ์ค์ ๋ก ์ฌ์ฉํ ์ ์๋ ์์ ํ ์์คํ ์ ๋ง๋ค์ด๋ณด์! ๐
import java.text.*;
import java.util.*;
/**
* ์์ ํ ๊ตญ์ ํ ์ง์ ์ ํธ๋ฆฌํฐ ํด๋์ค
*/
public class I18nUtil {
private static final String DEFAULT_BUNDLE_NAME = "messages";
private Locale currentLocale;
private ResourceBundle bundle;
public I18nUtil() {
this(Locale.getDefault());
}
public I18nUtil(Locale locale) {
setLocale(locale);
}
public void setLocale(Locale locale) {
this.currentLocale = locale;
this.bundle = ResourceBundle.getBundle(DEFAULT_BUNDLE_NAME, locale);
}
public Locale getCurrentLocale() {
return currentLocale;
}
/**
* ๋จ์ ๋ฉ์์ง ๊ฐ์ ธ์ค๊ธฐ
*/
public String getMessage(String key) {
try {
return bundle.getString(key);
} catch (MissingResourceException e) {
return "???" + key + "???";
}
}
/**
* ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๋ฉ์์ง ๊ฐ์ ธ์ค๊ธฐ
*/
public String getMessage(String key, Object... args) {
String pattern = getMessage(key);
return MessageFormat.format(pattern, args);
}
/**
* ๋ ์ง ํฌ๋งทํ
*/
public String formatDate(Date date, int style) {
DateFormat formatter = DateFormat.getDateInstance(style, currentLocale);
return formatter.format(date);
}
public String formatDate(Date date) {
return formatDate(date, DateFormat.MEDIUM);
}
/**
* ์๊ฐ ํฌ๋งทํ
*/
public String formatTime(Date date, int style) {
DateFormat formatter = DateFormat.getTimeInstance(style, currentLocale);
return formatter.format(date);
}
public String formatTime(Date date) {
return formatTime(date, DateFormat.MEDIUM);
}
/**
* ๋ ์ง+์๊ฐ ํฌ๋งทํ
*/
public String formatDateTime(Date date, int dateStyle, int timeStyle) {
DateFormat formatter = DateFormat.getDateTimeInstance(
dateStyle, timeStyle, currentLocale
);
return formatter.format(date);
}
public String formatDateTime(Date date) {
return formatDateTime(date, DateFormat.MEDIUM, DateFormat.MEDIUM);
}
/**
* ์ซ์ ํฌ๋งทํ
*/
public String formatNumber(Number number) {
NumberFormat formatter = NumberFormat.getNumberInstance(currentLocale);
return formatter.format(number);
}
public String formatNumber(Number number, int minDecimals, int maxDecimals) {
NumberFormat formatter = NumberFormat.getNumberInstance(currentLocale);
formatter.setMinimumFractionDigits(minDecimals);
formatter.setMaximumFractionDigits(maxDecimals);
return formatter.format(number);
}
/**
* ํตํ ํฌ๋งทํ
*/
public String formatCurrency(Number amount) {
NumberFormat formatter = NumberFormat.getCurrencyInstance(currentLocale);
return formatter.format(amount);
}
/**
* ํผ์ผํธ ํฌ๋งทํ
*/
public String formatPercent(Number value) {
NumberFormat formatter = NumberFormat.getPercentInstance(currentLocale);
return formatter.format(value);
}
/**
* ๋ณต์ํ ์ฒ๋ฆฌ
*/
public String getPlural(String key, int count) {
String pattern = getMessage(key);
MessageFormat formatter = new MessageFormat(pattern, currentLocale);
return formatter.format(new Object[]{count});
}
/**
* ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ ๋ก์ผ์ผ ๊ฐ์ ธ์ค๊ธฐ
*/
public static List<Locale> getAvailableLocales() {
return Arrays.asList(Locale.getAvailableLocales());
}
/**
* ์ง์ํ๋ ์ธ์ด ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
*/
public static List<Locale> getSupportedLocales() {
return Arrays.asList(
new Locale("ko", "KR"),
Locale.US,
Locale.JAPAN,
Locale.CHINA,
Locale.FRANCE,
Locale.GERMANY
);
}
}
๐ป ์ฌ์ฉ ์์
public class I18nDemo {
public static void main(String[] args) {
// ํ๊ตญ์ด๋ก ์์
I18nUtil i18n = new I18nUtil(new Locale("ko", "KR"));
System.out.println("=== ํ๊ตญ์ด ===");
demonstrateFeatures(i18n);
// ์์ด๋ก ๋ณ๊ฒฝ
i18n.setLocale(Locale.US);
System.out.println("\n=== English ===");
demonstrateFeatures(i18n);
// ์ผ๋ณธ์ด๋ก ๋ณ๊ฒฝ
i18n.setLocale(Locale.JAPAN);
System.out.println("\n=== ๆฅๆฌ่ช ===");
demonstrateFeatures(i18n);
}
private static void demonstrateFeatures(I18nUtil i18n) {
// ๋จ์ ๋ฉ์์ง
System.out.println("ํ์ ๋ฉ์์ง: " + i18n.getMessage("welcome.message"));
// ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๋ฉ์์ง
System.out.println("์ธ์ฌ: " + i18n.getMessage("greeting", "ํ๊ธธ๋"));
// ๋ ์ง ํฌ๋งทํ
Date now = new Date();
System.out.println("๋ ์ง: " + i18n.formatDate(now));
System.out.println("์๊ฐ: " + i18n.formatTime(now));
System.out.println("๋ ์ง+์๊ฐ: " + i18n.formatDateTime(now));
// ์ซ์ ํฌ๋งทํ
double number = 1234567.89;
System.out.println("์ซ์: " + i18n.formatNumber(number));
System.out.println("ํตํ: " + i18n.formatCurrency(number));
// ํผ์ผํธ ํฌ๋งทํ
double percent = 0.75;
System.out.println("ํผ์ผํธ: " + i18n.formatPercent(percent));
// ๋ณต์ํ ์ฒ๋ฆฌ
System.out.println("ํญ๋ชฉ 1๊ฐ: " + i18n.getPlural("items.count", 1));
System.out.println("ํญ๋ชฉ 5๊ฐ: " + i18n.getPlural("items.count", 5));
}
}
๐ฏ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์, ์ฌ๊ธฐ๊น์ง ์๋ฐ์ ๊ตญ์ ํ์ ResourceBundle์ ๋ํด ๊น์ด ์๊ฒ ์์๋ดค์ด! ์ ๋ง ๋ง์ ๋ด์ฉ์ ๋ค๋ค์ง? ๐
๋ค๊ตญ์ด ์ง์์ ๋จ์ํ ํ ์คํธ๋ฅผ ๋ฒ์ญํ๋ ๊ฒ ์ด์์ด์ผ. ์ฌ์ฉ์์ ๋ฌธํ์ ๊ด์ต์ ์กด์คํ๊ณ , ์ ์ธ๊ณ ๋๊ตฌ๋ ํธํ๊ฒ ์ฌ์ฉํ ์ ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋๋ ๊ฑฐ์ง. ๐
๐ ํต์ฌ ์์ฝ
1๏ธโฃ ResourceBundle
์๋ฐ์ ๊ตญ์ ํ ํต์ฌ ๋๊ตฌ. properties ํ์ผ์ด๋ ์๋ฐ ํด๋์ค๋ก ์ธ์ด๋ณ ๋ฆฌ์์ค๋ฅผ ๊ด๋ฆฌํด.
2๏ธโฃ Locale
์ธ์ด์ ์ง์ญ์ ๋ํ๋ด๋ ๊ฐ์ฒด. ์ธ์ด, ๊ตญ๊ฐ, ๋ณํ์ผ๋ก ์ธ๋ฐํ๊ฒ ๊ตฌ๋ถํ ์ ์์ด.
3๏ธโฃ MessageFormat
๋์ ์ธ ๊ฐ์ ๋ฉ์์ง์ ํฌํจํ ๋ ์ฌ์ฉ. ๋ณต์ํ, ๋ ์ง, ์ซ์ ๋ฑ ๋ค์ํ ํฌ๋งท ์ง์.
4๏ธโฃ ํด๋ฐฑ ๋ฉ์ปค๋์ฆ
ํน์ ์ธ์ด ๋ฆฌ์์ค๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ์์ ์ธ์ด๋ ๊ธฐ๋ณธ ์ธ์ด๋ก ๋์๊ฐ.
5๏ธโฃ ํฌ๋งทํ
DateFormat, NumberFormat ๋ฑ์ผ๋ก ๋ ์ง, ์๊ฐ, ์ซ์, ํตํ๋ฅผ ๋ก์ผ์ผ์ ๋ง๊ฒ ํ์.
์ด์ ๋ฐฐ์ด ๋ด์ฉ์ ์ค์ ํ๋ก์ ํธ์ ์ ์ฉํด๋ณด์! ์์ ๊ฒ๋ถํฐ ์์ํด:
1. ๊ฐ๋จํ ์ฝ์ ์ ํ๋ฆฌ์ผ์ด์
์ ๋ค๊ตญ์ด ์ง์ ์ถ๊ฐ
2. ๊ธฐ์กด ํ๋ก์ ํธ์ ํ๋์ฝ๋ฉ๋ ๋ฌธ์์ด์ ResourceBundle๋ก ์ด๋
3. ์น ์ ํ๋ฆฌ์ผ์ด์
์ ์ธ์ด ์ ํ ๊ธฐ๋ฅ ๊ตฌํ
4. ๋ฒ์ญ ๊ด๋ฆฌ ๋๊ตฌ ๋์
๊ฒํ
์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ๊ธ๋ก๋ฒ ์๋น์ค๋ฅผ ์ ๊ณตํ๋ค๋ฉด, ์ฒ์๋ถํฐ ๊ตญ์ ํ๋ฅผ ๊ณ ๋ คํด์ ์ค๊ณํ๋ ๊ฒ ์ ๋ง ์ค์ํด. ๋์ค์ ์ถ๊ฐํ๋ ค๋ฉด ์์ฒญ๋ ๋ฆฌํฉํ ๋ง์ด ํ์ํ๊ฑฐ๋ ! ๐ช
ํ๋ก๊ทธ๋๋ฐ์ ๋จ์ํ ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ, ์ ์ธ๊ณ ์ฌ์ฉ์๋ค์๊ฒ ๊ฐ์น๋ฅผ ์ ๋ฌํ๋ ๊ฑฐ์ผ. ๋ค๊ตญ์ด ์ง์์ ๊ทธ ์ฒซ๊ฑธ์์ด์ง! ๐
๊ถ๊ธํ ์ ์ด ์๊ฑฐ๋ ๋ ์๊ณ ์ถ์ ๋ด์ฉ์ด ์๋ค๋ฉด ์ธ์ ๋ ๋ฌผ์ด๋ด. ํจ๊ป ์ฑ์ฅํ๋ ๊ฐ๋ฐ์๊ฐ ๋์! ํ์ดํ ! ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.