Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to react to Locale changes at runtime with string based translations ?
How to react to Locale changes at runtime with string based translations ? [message #1622582] Wed, 18 February 2015 15:57 Go to next message
Thomas Elskens is currently offline Thomas ElskensFriend
Messages: 159
Registered: September 2014
Location: Brussels - Belgium
Senior Member
Hello,

I was wondering how I could react on locale changes at runtime in case I have to use a string based system.

I have a case where I have to use a string-based system, as I only know the keys at runtime (they are to be fetched from a database via a webservice).

For the moment, the only solution I could think of consists of something like this :

@Inject
@Optional
private void getNotified(@UIEventTopic(ILocaleChangeService.LOCALE_CHANGE) Locale locale)
{
	String value = ResourceBundle.getBundle("l10n.Application",locale).getString("employerdetail") ;
	nameLabel.setText(value);
}


Isn't there another way ? I already use the MessageRegistry system for normal labels (which is much more secure) and ideally I would like to limit the number of localization methods used...

Thanks in advance,

Thomas Elskens
Re: How to react to Locale changes at runtime with string based translations ? [message #1622641 is a reply to message #1622582] Wed, 18 February 2015 16:40 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6363
Registered: July 2009
Senior Member
The message registry uses @Translation hence it can load it's messages
from anywhere on this planet.

So maybe you should replace the default implementation (who uses
..property-Files) with something more powerful?

a) by replacing IMessageFactoryService
b) by replacing ResourceBundleProvider

I guess you could also use ResourceBundle.Control support but I'm not
really sure how this works. Dirk knows this stuff best, I was only the
guy with the carzy idea how to implement the translation thing but he
did the implementation ;-)

Tom

On 18.02.15 16:58, Thomas Elskens wrote:
> Hello,
>
> I was wondering how I could react on locale changes at runtime in case I
> have to use a string based system.
> I have a case where I have to use a string-based system, as I only know
> the keys at runtime (they are to be fetched from a database via a
> webservice).
> For the moment, the only solution I could think of consists of something
> like this :
>
>
> @Inject
> @Optional
> private void
> getNotified(@UIEventTopic(ILocaleChangeService.LOCALE_CHANGE) Locale
> locale)
> {
> String value =
> ResourceBundle.getBundle("l10n.Application",locale).getString("employerdetail")
> ;
> nameLabel.setText(value);
> }
>
>
> Isn't there another way ? I already use the MessageRegistry system for
> normal labels (which is much more secure) and ideally I would like to
> limit the number of localization methods used...
>
> Thanks in advance,
>
> Thomas Elskens
Re: How to react to Locale changes at runtime with string based translations ? [message #1623672 is a reply to message #1622641] Thu, 19 February 2015 08:44 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2526
Registered: July 2012
Senior Member
Tricky one! Since you say that your keys are dynamic, it is not possible to create a Messages class for them, as you can not create a mapping between key and member variable. But if you are interested in seeing how you could customize the loading of a ResourceBundle (e.g. via webservice or database) you can have a look at this example: https://github.com/fipro78/e4classbasedtranslation
It uses a class based ResourceBundle that is returned by a custom ResourceBundleProvider. Therefore it is automatically used by the application model and the Messages instances.

The question is more, do you have more than one ResourceBundle, since you are saying that on the one hand you are using the MessageRegistry, on the other hand you use some webservice. If you are using one ResourceBundle, some custom mechanism for creating it might be a good solution.

Otherwise you can use the following workaround. Since you are using the MessageRegistry, I assume you are using the one from e(fx)clipse, as the platform one is only available with Mars M5. I therefore assume you are using Java 8 and be able to use method references.

That said, you can use the MessageRegistry for such a binding too. You just need to get the ResourceBundle.

private ResourceBundle resourceBundle;
	
@Inject
void setResourceBundle(ResourceBundleProvider provider, @Named(TranslationService.LOCALE) Locale locale) {
	Bundle bundle = FrameworkUtil.getBundle(this.getClass());
	this.resourceBundle = provider.getResourceBundle(bundle, locale.toString());
}
	
public String getMessage(String key) {
	if (this.resourceBundle != null) {
		return this.resourceBundle.getString(key);
	}
	return "!"+key+"!";
}

...

Label test = new Label(parent, SWT.NONE);
registry.register(test::setText, (m) -> getMessage("employerdetail"));


This way you are using the typed MessageRegistry for binding to another method. But you are taking part in the dynamic method injection.

Note that the above code can not be put in a Messages class, because the instances are created using an ExtendedObjectSupplier. Therefore there is no access to the IEclipseContext they are called from, and so there is no support for injection in Messages classes.
Re: How to react to Locale changes at runtime with string based translations ? [message #1625335 is a reply to message #1623672] Fri, 20 February 2015 09:57 Go to previous message
Thomas Elskens is currently offline Thomas ElskensFriend
Messages: 159
Registered: September 2014
Location: Brussels - Belgium
Senior Member
Thanks a lot for the answer, I understand much better now how the MessageRegistry works. T

I think I'm going to stay with multiple ResourceBundles, as they come from very different sources : if for one reason or another, the webservice is not available, I would be able to express that failure in all relevant languages to the user Smile

Besides, it will be much easier : I just have to write a custom ResourceBundleProvider (thanks for that example, too !).

Regards,

Thomas

Previous Topic:Model to Viewer Databinding
Next Topic:Key binding for "find on web page"
Goto Forum:
  


Current Time: Sat Nov 25 00:11:05 GMT 2017

Powered by FUDForum. Page generated in 0.01968 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software