|
|
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 |
Dirk Fauth Messages: 2902 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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.02244 seconds