2021-08-16
This commit is contained in:
@@ -5,17 +5,6 @@
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
maven {
|
||||
setUrl("https://repository.elex-project.com/repository/maven")
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath ("com.jaredsburrows:gradle-license-plugin:0.8.90")
|
||||
}
|
||||
}
|
||||
plugins {
|
||||
id("com.github.ben-manes.versions") version "0.39.0"
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
|
||||
plugins {
|
||||
id("elex-base")
|
||||
id("elex-java")
|
||||
application
|
||||
}
|
||||
|
||||
|
||||
@@ -69,9 +69,9 @@ tasks.javadoc {
|
||||
dependencies {
|
||||
//implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
|
||||
implementation("org.slf4j:slf4j-api:1.7.32")
|
||||
implementation("org.jetbrains:annotations:21.0.1")
|
||||
implementation("org.jetbrains:annotations:22.0.0")
|
||||
|
||||
implementation("com.elex-project:abraxas:4.5.3")
|
||||
implementation("com.elex-project:abraxas:4.6.0")
|
||||
|
||||
compileOnly("org.projectlombok:lombok:1.18.20")
|
||||
annotationProcessor("org.projectlombok:lombok:1.18.20")
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
plugins {
|
||||
id ("elex-base")
|
||||
id ("elex-java")
|
||||
`java-library`
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
plugins {
|
||||
id ("elex-base")
|
||||
id ("elex-java")
|
||||
war
|
||||
}
|
||||
|
||||
|
||||
6
properties/README.md
Normal file
6
properties/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
## 자바 Resource Bundle 로캐일 선택 순서
|
||||
1. 사용자 로캐일과 일치하는 로캐일
|
||||
2. 사용자 로캐일 언어와 일치하는 로캐일
|
||||
3. 시스템 기본 로캐일
|
||||
4. 루트 로캐일
|
||||
14
properties/build.gradle.kts
Normal file
14
properties/build.gradle.kts
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
plugins {
|
||||
id("elex-java")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
}
|
||||
20
properties/logback.xml
Normal file
20
properties/logback.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Examples for Java
|
||||
~
|
||||
~ Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
~ https://www.elex-project.com/
|
||||
-->
|
||||
|
||||
<configuration>
|
||||
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="TRACE">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class MyResourceBundleControl extends ResourceBundle.Control {
|
||||
@Override
|
||||
public List<String> getFormats(String baseName) {
|
||||
if (baseName == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
return Collections.unmodifiableList(
|
||||
Arrays.asList("xml", "properties"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceBundle newBundle(String baseName, Locale locale,
|
||||
String format,
|
||||
ClassLoader loader,
|
||||
boolean reload)
|
||||
throws IllegalAccessException,
|
||||
InstantiationException, IOException {
|
||||
if (baseName == null || locale == null
|
||||
|| format == null || loader == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
ResourceBundle bundle = null;
|
||||
if (format.equals("xml")) {
|
||||
String bundleName = toBundleName(baseName, locale);
|
||||
String resourceName = toResourceName(bundleName, format);
|
||||
|
||||
|
||||
URL url = loader.getResource(resourceName);
|
||||
if (url != null) {
|
||||
URLConnection connection = url.openConnection();
|
||||
if (connection != null) {
|
||||
if (reload) {
|
||||
// disable caches if reloading
|
||||
connection.setUseCaches(false);
|
||||
}
|
||||
log.info("Bundle Name: {}", bundleName);
|
||||
log.info("Res Name: {}", resourceName);
|
||||
try (InputStream stream = connection.getInputStream()) {
|
||||
if (stream != null) {
|
||||
BufferedInputStream bis =
|
||||
new BufferedInputStream(stream);
|
||||
bundle = new XMLResourceBundle(bis);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (format.equals("properties")) {
|
||||
String bundleName = toBundleName(baseName, locale);
|
||||
String resourceName = toResourceName(bundleName, format);
|
||||
|
||||
|
||||
URL url = loader.getResource(resourceName);
|
||||
if (url != null) {
|
||||
URLConnection connection = url.openConnection();
|
||||
if (connection != null) {
|
||||
if (reload) {
|
||||
// disable caches if reloading
|
||||
connection.setUseCaches(false);
|
||||
}
|
||||
log.info("Bundle Name: {}", bundleName);
|
||||
log.info("Res Name: {}", resourceName);
|
||||
//System.out.println("Res Name: " + resourceName);
|
||||
try (InputStream stream = connection.getInputStream()) {
|
||||
if (stream != null) {
|
||||
InputStreamReader reader =
|
||||
new InputStreamReader(new BufferedInputStream(stream), StandardCharsets.UTF_8);
|
||||
bundle = new PropertyResourceBundle(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return bundle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
|
||||
|
||||
List<Locale> locales = Arrays.asList(
|
||||
locale,
|
||||
new Locale(locale.getLanguage()),
|
||||
Locale.ROOT);
|
||||
log.info("locales: {} -> {}", locale, locales);
|
||||
return locales;
|
||||
}
|
||||
|
||||
private static class XMLResourceBundle extends ResourceBundle {
|
||||
private final Properties props;
|
||||
|
||||
XMLResourceBundle(InputStream stream) throws IOException {
|
||||
props = new Properties();
|
||||
props.loadFromXML(stream);
|
||||
}
|
||||
|
||||
protected Object handleGetObject(String key) {
|
||||
if (key == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
return props.getProperty(key);
|
||||
}
|
||||
|
||||
public Enumeration<String> getKeys() {
|
||||
Set<String> handleKeys = props.stringPropertyNames();
|
||||
|
||||
return Collections.enumeration(handleKeys);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected Set<String> handleKeySet() {
|
||||
return props.stringPropertyNames();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.spi.ResourceBundleControlProvider;
|
||||
|
||||
public class MyResourceBundleControlProvider
|
||||
implements ResourceBundleControlProvider {
|
||||
static final ResourceBundle.Control XMLCONTROL =
|
||||
new MyResourceBundleControl();
|
||||
|
||||
public ResourceBundle.Control getControl(String baseName) {
|
||||
System.out.println("Class: " + getClass().getName()+".getControl");
|
||||
System.out.println(" called for " + baseName);
|
||||
|
||||
// Throws a NPE if baseName is null.
|
||||
if (baseName.startsWith("resources.Xml")) {
|
||||
System.out.println(" returns " + XMLCONTROL);
|
||||
return XMLCONTROL;
|
||||
}
|
||||
System.out.println(" returns null");
|
||||
System.out.println();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
9
properties/src/main/resources/lang.properties
Normal file
9
properties/src/main/resources/lang.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# Examples for Java
|
||||
#
|
||||
# Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
# https://www.elex-project.com/
|
||||
#
|
||||
|
||||
hello = Hello!
|
||||
only = Only in Root!
|
||||
8
properties/src/main/resources/lang_ja.properties
Normal file
8
properties/src/main/resources/lang_ja.properties
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# Examples for Java
|
||||
#
|
||||
# Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
# https://www.elex-project.com/
|
||||
#
|
||||
|
||||
hello = 오하이오!
|
||||
8
properties/src/main/resources/lang_ko.properties
Normal file
8
properties/src/main/resources/lang_ko.properties
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# Examples for Java
|
||||
#
|
||||
# Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
# https://www.elex-project.com/
|
||||
#
|
||||
|
||||
hello = 안녕!
|
||||
13
properties/src/main/resources/language.xml
Normal file
13
properties/src/main/resources/language.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!--
|
||||
~ Examples for Java
|
||||
~
|
||||
~ Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
~ https://www.elex-project.com/
|
||||
-->
|
||||
|
||||
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
|
||||
<properties>
|
||||
<entry key="hello">Hello</entry>
|
||||
<entry key="only">Only in Root</entry>
|
||||
</properties>
|
||||
12
properties/src/main/resources/language_ko.xml
Normal file
12
properties/src/main/resources/language_ko.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!--
|
||||
~ Examples for Java
|
||||
~
|
||||
~ Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
~ https://www.elex-project.com/
|
||||
-->
|
||||
|
||||
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
|
||||
<properties>
|
||||
<entry key="hello">안녕</entry>
|
||||
</properties>
|
||||
9
properties/src/main/resources/sample.properties
Normal file
9
properties/src/main/resources/sample.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# Examples for Java
|
||||
#
|
||||
# Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
# https://www.elex-project.com/
|
||||
#
|
||||
|
||||
key1 = value1
|
||||
key2 = value2
|
||||
7
properties/src/main/resources/sample.xml
Normal file
7
properties/src/main/resources/sample.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
|
||||
<properties version="1.0">
|
||||
<comment>Sample properties file in XML format.</comment>
|
||||
<entry key="key1">Value 1</entry>
|
||||
<entry key="key2">Value 2</entry>
|
||||
</properties>
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Properties;
|
||||
|
||||
public class PropertiesTest {
|
||||
@Test
|
||||
void properties() throws IOException {
|
||||
Properties properties = new Properties();
|
||||
// 인풋스트림만 사용하면 인코딩이 꺠질 수 있으므로 리더를 사용해서 인코딩을 지정해서 불러 온다.
|
||||
properties.load(new InputStreamReader(getClass()
|
||||
.getResourceAsStream("/sample.properties"), StandardCharsets.UTF_8));
|
||||
|
||||
String value1 = properties.getProperty("key1");
|
||||
System.out.println(value1);
|
||||
|
||||
// setProperty(key, value);
|
||||
// properties.store(writer, comment);
|
||||
}
|
||||
|
||||
@Test
|
||||
void xml_properties() throws IOException {
|
||||
Properties properties = new Properties();
|
||||
properties.loadFromXML(getClass()
|
||||
.getResourceAsStream("/sample.xml"));
|
||||
|
||||
String value1 = properties.getProperty("key1");
|
||||
System.out.println(value1);
|
||||
|
||||
// setProperty(key, value);
|
||||
//properties.storeToXML(os, comment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Examples for Java
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
@Slf4j
|
||||
public class ResourceBundleTest {
|
||||
@Test
|
||||
void resource_bundle_xml() {
|
||||
ResourceBundle bundle = ResourceBundle
|
||||
.getBundle("language", new MyResourceBundleControl());
|
||||
|
||||
System.out.println(bundle.getString("hello"));
|
||||
System.out.println(bundle.getString("only"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void resource_bundle_xml_jp() {
|
||||
ResourceBundle bundle = ResourceBundle
|
||||
.getBundle("language", Locale.JAPANESE, new MyResourceBundleControl());
|
||||
System.out.println(bundle.getLocale());
|
||||
System.out.println(bundle.getString("hello"));
|
||||
System.out.println(bundle.getString("only"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void resource_bundle() {
|
||||
ResourceBundle bundle = ResourceBundle
|
||||
.getBundle("lang");
|
||||
|
||||
System.out.println(bundle.getString("hello"));
|
||||
System.out.println(bundle.getString("only"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resource_bundle_jp() {
|
||||
ResourceBundle bundle = ResourceBundle
|
||||
.getBundle("lang", Locale.JAPAN, new MyResourceBundleControl());
|
||||
log.info("Bundle locale: {}", bundle.getLocale());
|
||||
System.out.println(bundle.getString("hello"));
|
||||
System.out.println(bundle.getString("only"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resource_bundle_fr() {
|
||||
Locale.setDefault(Locale.FRANCE);
|
||||
ResourceBundle bundle = ResourceBundle
|
||||
.getBundle("lang", Locale.FRENCH, new MyResourceBundleControl());
|
||||
log.info("Bundle locale: {}", bundle.getLocale());
|
||||
System.out.println(bundle.getString("hello"));
|
||||
System.out.println(bundle.getString("only"));
|
||||
}
|
||||
}
|
||||
@@ -13,5 +13,5 @@ include(
|
||||
"web-socket-servlet", "web-socket-client",
|
||||
"thread", "hibernate", "jdbc-sqlite",
|
||||
"xml", "jackson", "jsoup", "markdown", "network", "httpd",
|
||||
"swing", "java-fx"
|
||||
"swing", "java-fx", "properties"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user