0

Internationalization without refreshing the page

asked 2013-12-03 09:02:22 +0800

Neus gravatar image Neus
1415 14

Hi, It is possible to reload the labels once the locale has changed without refreshing the page? I used to have an event that is called when the user selects another language and it reload all the labels manually (lbl.setValue(Labels.getLabel("blablabla"); btn.setLabel("Labels.getLabel("blablabla");...). But sometimes I have a lot of labeled components in the page and I spend a lot of time doing this.

Is there any other way to do it? Something more automatic?

Thank you!

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2013-12-04 01:07:24 +0800

dennis gravatar image dennis
3669 1 6
http://www.javaworld.com....

updated 2013-12-04 11:07:51 +0800

if you are using MVC pattern, then you have to do that manually one by one. but if you are using MVVM, then binder could help you to reload them by sending a notify-change. for example. <label value="@load(myMsgPool.message_key)"/>

After local change, you just post notify change by BindUtils.postNotifyChange(null,null,myMsgPool,"*");

myMsgPool is a simple object instance that wrap the static Labels in the viewModel

about MVVM, read this http://books.zkoss.org/wiki/ZKDeveloper'sReference/MVVM

For the myMsgPool, you can just use a delegater that implement get api for Map. for example

public class LabelsWrap implements Map<String, String>{


    @Override
    public String get(Object key) {
        return Labels.getLabel(key.toString());
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean containsKey(Object key) {
        return get(key)!=null;
    }

    @Override
    public boolean containsValue(Object value) {
        return false;
    }


    @Override
    public String put(String key, String value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String remove(Object key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(Map<? extends String, ? extends String> m) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<String> keySet() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection<String> values() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<java.util.Map.Entry<String, String>> entrySet() {
        throw new UnsupportedOperationException();
    }

}
link publish delete flag offensive edit

Comments

I'm using MVVM. But if I have the texts in a properties file because I'm using i18n, how can I do it? What I must put inside the myMsgPool object?

Neus ( 2013-12-04 08:02:54 +0800 )edit

this object will have for each key of your i18N a getter, and will fetch the Labels.getLabel(key) Or you can put just one getter with a param (the key).

chillworld ( 2013-12-04 08:58:33 +0800 )edit

I have a class called MyLabels that have only a getter and this getter recive by parameter the key. In the zul I have: <label value="@load(vm.myLabels.Idioma)"/> and it says that Property Idioma is not found in the type MyLabels (I supposed that is because the object doesn't have the getter

Neus ( 2013-12-04 09:58:05 +0800 )edit

for Idioma). Is it possible to solve it without creating a class with the getters for aaall my labels in the properties file?

Neus ( 2013-12-04 09:58:48 +0800 )edit
1

look here : (last post you will see example of @load with param) http://forum.zkoss.org/question/77904/request-for-the-binding-of-the-bean-property-by-the-object/

chillworld ( 2013-12-04 10:10:35 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-12-03 09:02:22 +0800

Seen: 26 times

Last updated: Dec 04 '13

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