Currently I change my locale in GWT using a javascript as a native code. if the user click on changeLocale button on my GUI, i call this native method. Is there any java method/way to do that without using any native code???
Have you looked at the Showcase sample?
Redux'd, it's as easy as:
Window.Location.assign( // or replace()
Window.Location.createUrlBuilder()
.setParameter(LocaleInfo.getLocaleQueryParam(), "fr-FR")
.buildString());
Related
I need to add a content assistant to an editor in Java. I have found some material here.
However I don't understand how can I trigger the content assistant. I have a class that implemented the method getContentAssistant, but I don't know how to call it.
How does language switching work in Java/Vaadin? I have a web application and would like to integrate a combo box, that changes the language of every text in this application. Do i need to mark each text that should get translated manually and define its translation? How complex is it to implement this function into an exting project?
Do i need to mark each text that should get translated manually and define its translation?
You should use ResourceBundles to store/read translations of strings.
How does language switching work in Java/Vaadin?
You need to provide a class that implements I18NProvider. Documentation about that can be found here
Once implemented correctly, you will be able to call getTranslation("HelloWorld") on any Component (and therefore on any view since they must extend a component), to receive the translation of the key "HelloWorld" defined in the ResourceBundle-file of the current UI-Locale.
Views that extend LocaleChangeObserver are notified when the Locale is changed, and then you can call getTranslation("HelloWorld") again to find the translation of the freshly set language.
I would like to integrate a combo box, that changes the language of every text in this application.
See this SO answer of mine where I posted example code of a Select component that acts as a language switcher. It is using both ResourceBundle and I18NProvider. (You can use a ComboBox too, but with the downside that you can only display a String for the selected value)
The important part in that code there is that the Select has a ValueChangeListener that sets the Locale of the VaadinSession, which in turn will trigger the localeChange method of the LocaleChangeObserver that your view now should implement. In the localeChange method, you can re-translate the translatable Strings of every component in the view; set new texts in Labels, set new labels and placeholders for TextFields, etc etc.
How complex is it to implement this function into an exting project?
That depends very much on your definition of complex, and how familiar you already are with ResourceBundles. There certainly are less complex topics than this, but I18N is never easy. In my opinion, Vaadin has done a very great job of providing us devs with a way to use I18N in our applications.
Most people use a sort of translation file system for their localization. Basically you make a text file for each language with a key and value system where you name every translated message with a key and a translated value. You can then use these keys (that should be predefined) to get the correct message for the language you want. These files can be anything really, but if you're looking for a simple java implementation then there are pretty simple ways to do it. For an example look here.
Did you look at this section of the documentation? https://vaadin.com/docs/v14/flow/advanced/tutorial-i18n-localization.html
I am using SmartGWT and I wish to access com.smartgwt.client.Version from JavaScript. In Firefox's Web Console, I have tried:
frames[0].$entry(Lcom_smartgwt_client_Version::getVersion()));
and
frames[0].$entry(#com.smartgwt.client.Version.getVersion());
and
frames[0].$entry(#com.smartgwt.client.Version::getVersion());
and
frames[0].$entry(#com.smartgwt.client.Version::getVersion()());
But all of them return a syntax error.
SmartGWT is deployed with my WAR and I can see other SmartGWT classes listed when I do just frames[0].
What is the right syntax to call this static Java method?
Those JSNI references do not work except in JSNI code in your java files. References to Java methods and fields in JSNI are not actually valid JavaScript, but part of the JSNI language to enable those native methods to both use Java and JavaScript. The JSNI string #com.smartgwt.client.Version::getVersion()() will be rewritten as something like $getVersion1() in PRETTY, or something just one or two characters long in OBF mode, so you can't rely on that method name being the same.
Instead, you need to export a JavaScript function from inside your application so that this external JavaScript can invoke it. Check out https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI#calling for specific details on this.
Here is an example of how this might look in your application:
public native void exportGetVersion() /*-{
$wnd.getSmartGwtVersion = $entry(function() {
return #com.smartgwt.client.Version::getVersion()();
});
}-*/;
Make sure you call this function in your app somewhere to export the function - any time after that is called, you can invoke getSmartGwtVersion() from your regular JavaScript - no need to use frames or $entry.
I'm working on a GWT application, which should behave in a slightly different manner when it is running inside a frame and when running directly in a browser window.
The question is: how to determine at runtime whether we're in a frame or in a window?
Wrap a bit of javascript into JSNI:
public static native boolean isFrame() /*-{
return ($wnd!=$wnd.top);
}-*/;
I haven't tested this but looking at the javadoc you can try something like:
RootPanel.getBodyElement().getParentElement().getTagName()
and see if it's an iframe or whatever tag you need
I have a javascript function (very big one!) that I need its functionality in a Java (Groovy) class. It is a simple calendar converter. I can rewrite it in groovy but just want to know if it is possible to call javascript function from a java (groovy) method? I guess functional testing libraries like selenium and Canoo should have something like this, am I right?
PS: I don't want to wake up a real-world browser in order to use its JS runtime env.
Thanks,
As mentioned in the other answers, it is possible to use the Scripting API provided as part of the javax.script package, available from Java 6.
The following is a Groovy example which executes a little bit of Javascript:
import javax.script.*
manager = new ScriptEngineManager()
engine = manager.getEngineByName("JavaScript")
javascriptString = """
obj = {"value" : 42}
print(obj["value"])
"""
engine.eval(javascriptString) // prints 42
It is not necessary to call a browser to execute Javascript when using the Scripting API, but one should keep in mind that browser-specific features (probably the DOM-related functionalities) will not be available.
You can use Rhino, an implementation of JavaScript language in Java. Here is example of calling JavaScript function from java, but you can do it from groovy also.