1

localization with parameter [closed]

asked 2014-06-09 16:34:02 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2014-06-09 17:17:32 +0800

Hi all,

i have no luck by searching for a sample for localization with parameters.

Labels.getLabel("myKey", new Object[]{user.getFirstname(), user.getLastname()});

i18n

myKey=Hello :${firstname} :${lastname}, How do you do?

does not work. Any idea?

best Stephan

Update: next code lines doesn't work too.

myKey=Hello :${param.firstname} :${param.lastname}, How do you do?

Map<String, String> map = new HashMap<String, String>();
map.put("firstname", user.getFirstname());
map.put("lastname", user.getLastname());
Executions.getCurrent().setAttribute("param", map);

message.setText(Labels.getLabel("myKey") + "\n\n" + url + "=" + paramValue);
delete flag offensive retag edit

The question has been closed for the following reason "the question is answered, right answer was accepted" by terrytornado
close date 2014-06-12 13:56:36

7 Answers

Sort by ยป oldest newest most voted
3

answered 2014-06-10 07:25:59 +0800

khcyt gravatar image khcyt
216 1

Hi Stephan, i think the formatting will only work if you try it with the following way.

myKey=Hello {0} {1}, How do you do?

Labels.getLabel("myKey", new Object[]{user.getFirstname(), user.getLastname()});

Kai

link publish delete flag offensive edit
1

answered 2014-06-11 08:06:39 +0800

chillworld gravatar image chillworld flag of Belgium
5357 4 9
https://github.com/chillw...

updated 2014-06-11 12:42:17 +0800

The problem is that we can't create the String[] or Object[] directly in the zul.

So we have 3 solutions.

First solution

Make a getter in the vm what returns the String[]

vm:

public String[] getFullName () {
    return new String[] {user.getFirstname(), user.getLastname()};
}

zul:

<label [email protected](ox:lDef('myKey', vm.fullName)}" />

Second solution

Oke this is an ugly one, but works completly in the zul.

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<label [email protected](ox:lDef('myKey', c:split(c:cat3(vm.user.firstname,'%%%%%',vm.user.lastname), '%%%%%')))}" />

Explication :

While we can't create a new String[] or Object[] directly from the zul, we could make a method for us to do so.
c:cat3 => means concatenate these 3 strings.
c:split => split them with the following regex.

Now this was a nice test and so I would like to go further in improving this way.

Third solution

<!-- Create an array from the given strings-->
<function>
    <name>stringsToArray2</name>
    <function-class>my.helper.class</function-class>
    <function-signature>
            java.lang.String[] convertToArray(java.lang.String, java.lang.String)
    </function-signature>
    <description>create a string array from these parameters.</description>
</function>

<!-- Create an array from the given strings-->
<function>
    <name>stringsToArray3</name>
    <function-class>my.helper.class</function-class>
    <function-signature>
            java.lang.String[] convertToArray(java.lang.String, java.lang.String, java.lang.string)
    </function-signature>
    <description>create a string array from these parameters.</description>
</function>

then we could do in zul :

<label [email protected](ox:lDef('myKey', ox:stringsToArray2(vm.user.firstname,vm.user.lastname)))}" />

In the class (watch out the method must be static):

public static String[] convertToArray2(String arg1, String arg2) {
    return new String[] { arg1, arg2 };
}

Personally I like the third solution.
I put the stringsToArray2 and stringsToArray3 cause when we see at the core library they do the same for concatination (cat, cat3, cat4, cat5).
This is also because the binder has no way to go with varargs.
I should recommend to ZK to add support for varargs to.

Greetz chill.

link publish delete flag offensive edit
0

answered 2014-06-10 10:11:46 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Thanks Kai,

this works perfect in java code. Thanks. Now i will investigate how this is working in a zul file.

Thanks Stephan

link publish delete flag offensive edit
0

answered 2014-06-10 20:51:43 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2014-06-10 20:52:46 +0800

Get it not to work. What's the hack do i false?

taglib:

  <!-- Retrieve an I18N string with a default value and replace parameters -->
    <function>
        <name>lDefParam</name>
        <function-class>org.zkoss.util.resource.Labels</function-class>
        <function-signature>
            java.lang.String getLabel(java.lang.String, java.lang.String, java.lang.Object[])
        </function-signature>
        <description>Retrieve a label with a default and replace parameters.</description>
</function>

zul:

<label value="${ox:lDefParam('ox_Message_Info_NewUserRegistration','defaultText', {'test1','test2'} )}"></label>


Caused by: org.zkoss.zel.impl.parser.ParseException: Encountered " <ILLEGAL_CHARACTER> "{ "" at line 1, column 69.
    Was expecting one of:
        <INTEGER_LITERAL> ...
        <FLOATING_POINT_LITERAL> ...
        <STRING_LITERAL> ...
        "true" ...
        "false" ...
        "null" ...
        "(" ...
        "!" ...
        "not" ...
        "empty" ...
        "-" ...
        <IDENTIFIER> ...

        at org.zkoss.zel.impl.parser.ELParser.generateParseException(ELParser.java:2215)

any idea are welcome Stephan

link publish delete flag offensive edit
0

answered 2014-06-11 11:21:03 +0800

khcyt gravatar image khcyt
216 1

Hello Stephan, Hello chill, I would prefer the second solution. Since we do not need a new entry as a function mapper, (l2 already exists for java.lang.String getLabel (java.lang.String, java.lang.String, java.lang.Object [])), the label component but does not look so complicated. concat() and split() can be called directly on the string objects.

<label value="${c:l2('tst.app.testlabel', 'String A'.concat('%%').concat('String B').split('%%'))}"/>

Kai

link publish delete flag offensive edit
0

answered 2014-06-11 12:05:26 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2014-06-11 12:07:01 +0800

I do not agree with that. As i remember the problems with the i18n stuff begans with the missing core functionality for a default value. It does not run out of the box. Means we have make a custom taglib for using default values in the zul

                            value="@load( c:l2('ox_Message_Info_NewUserRegistration', 'defaultText', c:split(c:cat3(vm.firstname, '%%%%%', vm.lastname), '%%%%%')))" />

throws a:

Caused by: org.zkoss.zel.ELException: Function 'c:l2' specifies 2 params, but 3 were declared

The next problem was the formatting of params.

These two things are definitively must in the core and not in user defind taglib functions.

Only my personally impression. best Stephan

link publish delete flag offensive edit

Comments

OK, your requirement for a standard parameter I have ignored :-)

Greetings, Kai

khcyt ( 2014-06-11 12:28:22 +0800 )edit
0

answered 2014-06-11 15:56:24 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Ok, have it now with customized taglibs. Thanks to chill.

Get all codes back with the ZKBoost application.

  1. can set 'default' value in zul for not existing/finded key.
  2. can set params for message formatting. Like myKey=Hello {0}{1}, How are you?

zul:

<label value="${ox:lDefParam('ox_Message_Info_NewUserRegistration', 'defaultText', ox:convertToArray2('Bill','Gates') )}" />

thanks Stephan

link publish delete flag offensive edit

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-06-09 16:34:02 +0800

Seen: 103 times

Last updated: Jun 11 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More