CER은 범용 Java 클래스 String을 지원합니다.
String은 규칙 세트를 개발하는 초기 단계에 유용하게 사용될 수 있습니다. 그러나 규칙에 다른 로케일로 사용자에게 표시해야 하는 결과가 포함된 경우 CER의 자국어 지원을 이용해야 할 수도 있습니다.
아래의 예는 영어를 읽는 일반 사용자에게 매우 익숙한 String 값 "Hello World"입니다. 그러나 사용자가 영어를 읽지 못하는 경우에는 어떻게 될까요?
<?xml version="1.0" encoding="UTF-8"?> <RuleSet name="HelloWorldRuleSet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "http://www.curamsoftware.com/CreoleRulesSchema.xsd"> <Class name="HelloWorld"> <Attribute name="greeting"> <type> <javaclass name="String"/> </type> <derivation> <String value="Hello, world!"/> </derivation> </Attribute> </Class> </RuleSet>
CER에는 런타임 시 값을 로케일에 따라 다른 문자열로 변환할 수 있는 curam.creole.value.Message가 포함되어 있습니다.
curam.creole.value.Message의 인스턴스를 작성할 수 있는 CER 표현식 목록은 자국어 지원 가능 메시지의 내용을 참조하십시오.
이제 자국어를 지원하도록 HelloWorld 예를 다음과 같이 다시 작성합니다.
<?xml version="1.0" encoding="UTF-8"?> <RuleSet name="LocalizableHelloWorldRuleSet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "http://www.curamsoftware.com/CreoleRulesSchema.xsd"> <Class name="HelloWorld"> <Attribute name="greeting"> <type> <!-- String이 아니라 Message 사용 --> <javaclass name="curam.creole.value.Message"/> </type> <derivation> <!-- 단일 언어 String을 하드 코딩하지 않고 자국어 지원 가능 특성에서 값을 검색 --> <ResourceMessage key="greeting" resourceBundle="curam.creole.example.HelloWorld"/> </derivation> </Attribute> </Class> </RuleSet>
# curam/creole/example/HelloWorld_en.properties 파일 greeting=Hello, world!
# curam/creole/example/HelloWorld_fr.properties 파일 greeting=Bonjour, monde!
런타임 시 이 메시지가 작동하는 방식은 다음과 같습니다. 규칙 세트와 상호작용하는 모든 코드가 모든 메시지에 대해 toLocale 메소드를 호출하여 해당 메시지를 필요한 로케일로 변환해야 합니다.
package curam.creole.example; import java.util.Locale; import junit.framework.TestCase; import curam.creole.execution.session.RecalculationsProhibited; import curam.creole.execution.session.Session; import curam.creole.execution.session.Session_Factory; import curam.creole.execution.session.StronglyTypedRuleObjectFactory; import curam.creole.ruleclass.LocalizableHelloWorldRuleSet.impl.HelloWorld; import curam.creole.ruleclass.LocalizableHelloWorldRuleSet.impl.HelloWorld_Factory; import curam.creole.storage.inmemory.InMemoryDataStorage; import curam.creole.value.Message; public class TestLocalizableHelloWorld extends TestCase { /** * 독립형 Java 애플리케이션으로 클래스를 실행합니다. */ public static void main(final String[] args) { final TestLocalizableHelloWorld testLocalizableHelloWorld = new TestLocalizableHelloWorld(); testLocalizableHelloWorld.testLocalizedRuleOutput(); } /** * 다른 로케일로 자국어 지원된 결과를 표시하는 간단한 * 테스트 케이스입니다. */ public void testLocalizedRuleOutput() { final Session session = Session_Factory.getFactory().newInstance( new RecalculationsProhibited(), new InMemoryDataStorage( new StronglyTypedRuleObjectFactory())); final HelloWorld helloWorld = HelloWorld_Factory.getFactory().newInstance(session); // String이 아니라 Message 리턴 final Message greeting = helloWorld.greeting().getValue(); // 메시지를 디코드하려면 사용자 로케일을 사용해야 함 final String greetingEnglish = greeting.toLocale(Locale.ENGLISH); final String greetingFrench = greeting.toLocale(Locale.FRENCH); System.out.println(greetingEnglish); System.out.println(greetingFrench); assertEquals("Hello, world!", greetingEnglish); assertEquals("Bonjour, monde!", greetingFrench); } }
자국어 지원 가능 메시지에서 사용하는 경우 다음 데이터 유형은 런타임 시 로케일을 인식하는 방식으로 형식화됩니다.
기타 모든 오브젝트는 toString 메소드를 사용하여 표시됩니다.