pivot-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Chatelain <schat...@gmail.com>
Subject Re: Change application locale after startup
Date Fri, 28 Jan 2011 12:16:36 GMT
Ah ok, I see.

I was thinking that maybe there was a more subtle way of doing that.
Something like:

    private Window window = null;
    private BXMLSerializer bxmlSerializer = null;

    public void startup()  {
        Resources resources = new Resources(getClass().getName(),
Locale.getDefault());

        bxmlSerializer = new BXMLSerializer();
        window =
(Window)bxmlSerializer.readObject(Localization.class.getResource("my_window.bxml"),
resources);
        window.open(display);
    }
    private void setLocale(String lang) {
        Locale.setDefault(new Locale(lang));
        Resources resources = new Resources(getClass().getName(),
Locale.getDefault());
        bxmlSerializer.Update/Reload(resources);
    }

In fact the current state of my test application looks like :

public class Main implements Application {

    Window myWindow = new Window();

    public static void main(String[] args) {
        DesktopApplicationContext.main(Main.class, args);
    }

    private void setLanguage(String lang) {
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        loadUI();
    }

    private void loadUI() {
        try {
            Resources resources = new Resources(getClass().getName(),
                    Locale.getDefault());
            BXMLSerializer bxmlSerializer = new BXMLSerializer();
            TabPane l_TabPane = (TabPane) bxmlSerializer.readObject(
                    Main.class.getResource("ContainerView.bxml"),
resources);
            myWindow.setContent(l_TabPane);

        } catch (Exception _Ex) {
            _Ex.printStackTrace();
        }
    }

    @Override
    public void startup(Display _Display, Map<String, String> _Properties)
            throws Exception {

        loadUI();
        myWindow.setMaximized(true);
        myWindow.open(_Display);
    }
}

It works to update localized strings but even if it is easy to reload all
data displayed in the UI by using data binding after reloading the bxml
file, I will loose the state of the UI (split ratio of splitters, selected
tab in a tab pane, etc.).

Anyway, thank you for your answer.

Simon

On Thu, Jan 27, 2011 at 4:05 PM, Greg Brown <gk_brown@verizon.net> wrote:

> I need to implement a dynamic localization in my application, meaning the
> user must be able to click on a button in some kind of toolbar and the UI
> must change directly to display all texts in the chosen language without
> restarting the application.
>
> ...
>
> So I go back to the localization feature and found on the mailing list the
> question of Joao Neves, and the answer of Greg Brown saying
> >If you define your UI in BXML, you can just reload it after you change the
> locale.
> That seems interesting but I do not understand how to “reload the UI”; if
> someone could post a little code snippet showing how to do that, that would
> be great.
>
>
> I don't think there is an example that demonstrates how to do this, but the
> idea is pretty simple. Let's say you have a main Window defined in
> my_window.bxml. Your application loads this window in startup() and stores a
> reference to it. When you want to apply a locale change, you can reload
> my_window.bxml using the Resources loaded from the new locale.
>
> You might be able to do this by calling shutdown() and startup() on the
> application yourself. I haven't tried it but it might be worth a shot.
>
> G
>
>
>

Mime
View raw message