You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by João Neves <se...@gmail.com> on 2010/12/15 17:47:45 UTC

Change application locale after startup

Hi,

Is it possible to change the application locale after it started?

Thanks!

--
João Neves

Re: Change application locale after startup

Posted by Greg Brown <gk...@verizon.net>.
> I was thinking that maybe there was a more subtle way of doing that. 
> Something like:
...
>         Resources resources = new Resources(getClass().getName(), Locale.getDefault());
>         bxmlSerializer.Update/Reload(resources);

Unfortunately, no. BXMLSerializer simply reads an XML file and outputs the corresponding object hierarchy. As the file is read, resource substitution is performed. In order to reload the resources you need to reload the BXML file.

> 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.).

Yes, that would be a side effect of reloading the UI. You'll want to save any persistent state locally so you can restore it when you reload the UI. You can use the standard JDK preferences API for this:

http://download.oracle.com/javase/6/docs/api/java/util/prefs/package-summary.html


Re: Change application locale after startup

Posted by Simon Chatelain <sc...@gmail.com>.
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...@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
>
>
>

Re: Change application locale after startup

Posted by Greg Brown <gk...@verizon.net>.
> 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



Re: Change application locale after startup

Posted by Simon Chatelain <sc...@gmail.com>.
Hello all,

I am using Pivot for about a month now and until now I was able to do
everything I needed to, but now I am facing an issue that I don’t know how
to solve: 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.
I looked at the localization example of the pivot tutorial, but it seemed
that this feature does not provide the level of dynamicity that I need.
Then I used the data binding feature: by setting the textKey property of all
the labels in my UI I am able, when the user click on the button to change
the display language, to build a dictionary containing the new translation
and update all displayed text with the load() method. That works pretty well
except that a lot of texts in my UI are not bindable, like the TabHeader,
Border title or Button text (only buttonData is bindable, so I cannot use
data binding to change only the text in the case I want to diplay an icon
and a text in the button).
A solution could be to use data binding where possible (mainly for labels)
and to write some specific code to handle TabHeader, Border title etc. But I
would prefer to avoid writing specific code.
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.
Or if anybody has another suggestion on how to implement this dynamic
localization, I would be also very interested.

Thank you

Simon


On Wed, Dec 15, 2010 at 6:26 PM, Greg Brown <gk...@verizon.net> wrote:

> If you define your UI in BXML, you can just reload it after you change the
> locale.
>
> On Dec 15, 2010, at 12:04 PM, João Neves wrote:
>
> > Thanks, somehow I missed that method.
> >
> > So there is no "automagic" way to update the UI, and I'll have to
> > change the text in all elements "manually", correct?
> >
> > Thanks once again for your help.
> >
> > --
> > João Neves
> >
> >
> >
> > On Wed, Dec 15, 2010 at 16:56, Greg Brown <gk...@verizon.net> wrote:
> >> Yes - you can call Locale.setDefault(). However, your UI won't
> automatically update itself. You'll need to handle this yourself.
> >>
> >> On Dec 15, 2010, at 11:47 AM, João Neves wrote:
> >>
> >>> Hi,
> >>>
> >>> Is it possible to change the application locale after it started?
> >>>
> >>> Thanks!
> >>>
> >>> --
> >>> João Neves
> >>
> >>
>
>

Re: Change application locale after startup

Posted by Greg Brown <gk...@verizon.net>.
If you define your UI in BXML, you can just reload it after you change the locale.

On Dec 15, 2010, at 12:04 PM, João Neves wrote:

> Thanks, somehow I missed that method.
> 
> So there is no "automagic" way to update the UI, and I'll have to
> change the text in all elements "manually", correct?
> 
> Thanks once again for your help.
> 
> --
> João Neves
> 
> 
> 
> On Wed, Dec 15, 2010 at 16:56, Greg Brown <gk...@verizon.net> wrote:
>> Yes - you can call Locale.setDefault(). However, your UI won't automatically update itself. You'll need to handle this yourself.
>> 
>> On Dec 15, 2010, at 11:47 AM, João Neves wrote:
>> 
>>> Hi,
>>> 
>>> Is it possible to change the application locale after it started?
>>> 
>>> Thanks!
>>> 
>>> --
>>> João Neves
>> 
>> 


Re: Change application locale after startup

Posted by João Neves <se...@gmail.com>.
Thanks, somehow I missed that method.

So there is no "automagic" way to update the UI, and I'll have to
change the text in all elements "manually", correct?

Thanks once again for your help.

--
João Neves



On Wed, Dec 15, 2010 at 16:56, Greg Brown <gk...@verizon.net> wrote:
> Yes - you can call Locale.setDefault(). However, your UI won't automatically update itself. You'll need to handle this yourself.
>
> On Dec 15, 2010, at 11:47 AM, João Neves wrote:
>
>> Hi,
>>
>> Is it possible to change the application locale after it started?
>>
>> Thanks!
>>
>> --
>> João Neves
>
>

Re: Change application locale after startup

Posted by Greg Brown <gk...@verizon.net>.
Yes - you can call Locale.setDefault(). However, your UI won't automatically update itself. You'll need to handle this yourself.

On Dec 15, 2010, at 11:47 AM, João Neves wrote:

> Hi,
> 
> Is it possible to change the application locale after it started?
> 
> Thanks!
> 
> --
> João Neves