Skip to content

Commit b608e5d

Browse files
committed
Added options to create a ParamI18N loading keys from a different bundle
than the main property key
1 parent 079fffe commit b608e5d

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed

fj-core/src/main/java/org/fugerit/java/core/util/i18n/HelperI18N.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Arrays;
55
import java.util.List;
66

7+
import org.fugerit.java.core.util.ObjectUtils;
8+
79
public abstract class HelperI18N {
810

911
protected HelperI18N( String baseName, String defaultLanguge, String... altLocale ) {
@@ -35,7 +37,9 @@ public String getString( String lang, String key, Object... params ) {
3537
for ( int k=0; k<params.length; k++ ) {
3638
Object current = params[k];
3739
if ( current instanceof ParamI18N ) {
38-
current = this.resolveString( lang, ((ParamI18N)current).getKey() );
40+
ParamI18N param = (ParamI18N)current;
41+
HelperI18N helper = ObjectUtils.objectWithDefault( param.getHelper() , this );
42+
current = helper.resolveString( lang, param.getKey() );
3943
}
4044
realParams[k] = current;
4145
}

fj-core/src/main/java/org/fugerit/java/core/util/i18n/ParamI18N.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,31 @@
22

33
public class ParamI18N {
44

5-
private ParamI18N( String key ) {
5+
private ParamI18N( String key, HelperI18N helper ) {
66
this.key = key;
7+
this.helper = helper;
78
}
89

910
private String key;
1011

12+
private HelperI18N helper;
13+
1114
public String getKey() {
1215
return key;
1316
}
1417

18+
public HelperI18N getHelper() {
19+
return helper;
20+
}
21+
1522
public static ParamI18N newParamI18N( String key ) {
16-
return new ParamI18N( key );
23+
return newParamI18N( key, null );
1724
}
1825

26+
public static ParamI18N newParamI18N( String key, HelperI18N helper ) {
27+
return new ParamI18N( key, helper );
28+
}
29+
1930
@Override
2031
public String toString() {
2132
return this.getClass().getSimpleName()+"[key:"+this.getKey()+"]";

fj-core/src/test/java/test/org/fugerit/java/core/util/i18n/TestHelperI18N.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,25 @@ public class TestHelperI18N {
2323

2424
private static final String CONF_PATH = "core.util.i18n.test";
2525

26+
private static final String CONF_PATH_PARAM = "core.util.i18n.param";
27+
2628
private HelperI18N helper;
2729

28-
private void testHelper( String expectedValue, String lang, String key, Object... params ) {
29-
String value = this.helper.getString( lang , key, params );
30-
logger.info( "key:{} , lang:{} -> {}", lang, key, value );
31-
Assert.assertEquals( "Value different for key : "+key+", lang : "+lang , expectedValue, value );
32-
}
30+
private HelperI18N helperParam;
3331

32+
3433
@Before
3534
public void init() {
3635
this.helper = BundleMapI18N.newHelperI18N( CONF_PATH, LANG_DEF, LANG_ALT );
36+
this.helperParam = BundleMapI18N.newHelperI18N( CONF_PATH_PARAM, LANG_DEF, LANG_ALT );
3737
}
3838

39+
private void testHelper( String expectedValue, String lang, String key, Object... params ) {
40+
String value = this.helper.getString( lang , key, params );
41+
logger.info( "key:{} , lang:{} -> {}", lang, key, value );
42+
Assert.assertEquals( "Value different for key : "+key+", lang : "+lang , expectedValue, value );
43+
}
44+
3945
@Test
4046
public void testSimpleProperty1() {
4147
String key = "test.prop.1"; // test.prop.1 is available for both languages
@@ -67,6 +73,14 @@ public void testComplexProperty1() {
6773
LANG_ALT[0], key, new Integer( 3 ), ParamI18N.newParamI18N( keyParam ) );
6874
}
6975

70-
76+
@Test
77+
public void testComplexProperty1Alt() {
78+
String key = "test.complex.prop.1"; // contains a integer param and a i18n param
79+
String keyParam = "test.param.1.alt"; // the key for the i18n param
80+
this.testHelper( "Complex property 1 -> simple parameter : 3 and i18n parameter : (ParamI18N value alternative bundle)",
81+
LANG_DEF, key, new Integer( 3 ), ParamI18N.newParamI18N( keyParam, this.helperParam ) );
82+
this.testHelper( "Proprietà complessa test 1 -> parametro semplice : 3 e parametro i18n : (valore ParamI18N bundle alternativo)",
83+
LANG_ALT[0], key, new Integer( 3 ), ParamI18N.newParamI18N( keyParam, this.helperParam ) );
84+
}
7185

7286
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
3+
<properties>
4+
<entry key="test.param.1.alt">(ParamI18N value alternative bundle)</entry>
5+
</properties>
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
3+
<properties>
4+
<!-- this bundle must remain empty as it's for the default language -->
5+
</properties>
6+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
3+
<properties>
4+
<entry key="test.param.1.alt">(valore ParamI18N bundle alternativo)</entry>
5+
</properties>

0 commit comments

Comments
 (0)