How to localize p:captcha - jsf-2

I would like to create a localization for PrimeFaces <p:captcha> component. I know how to localize validation error messages by using requiredMessage or validatorMessage attributes as follows:
<p:captcha label="Captcha" theme="clean" requiredMessage="#{msg['primefaces.captcha.INVALID']}" validatorMessage="#{msg['primefaces.tree.REQUIRED']}"/>
The problem is that I don't know how to localize e.g. labels (as depicted below)
So I need to localize points 1) and 2) and tooltips which are showed when user hovers any of buttons. It would be also nice if reCaptcha could use some kind of localized dictionary (3rd point) but I doubt that it's possible and it's really optional. I know that reCaptcha has few supported languages but mine (pl - polish) is unfortunately not supported...

Just add a language attribute to the captcha tag
<p:captcha language="pl" />
If you have a session bean that stores the user language you can use that too, for example:
<p:captcha language="#{userSessionBean.locale}" />
The bean:
#ManagedBean
#SessionScoped
public class UserSessionBean {
private Locale locale;
public Locale getLocale(){ return locale; }
public void setLocale(Locale locale) { this.locale = locale; }
}

Related

java struts2 very strange behaviour while using iterator

public class UserAction{
private UserData user;
//getter, setter
public String Load() {
user = UserDao.getInstance().getItem(getUserContext().getId());
request.getSession().setAttribute("item", user);
return super.Load();
}
}
public class PropertyAction {
private List <PropertyData> propertyList;
//getter, setter
#Override
public String execute() throws Exception {
propertyList=PropertyDao.getInstance().getItems();
return "list";
}
}
jsp:
<s:iterator value="propertyList" var="item">
${item.name}
${item.thema}
${item.desc}
</s:iterator>
I want to show very strange behaviour of Struts2.
I click property link -> then run PropertyAction.execute() and it display above jsp.
I click user link -> then run UserAction.Load()
I click property link -> then run PropertyAction.execute() and error has been shown "UserData has no property thema".
I spy what happened and I notice that I set setAttribute with name "item". So if I use var="item" in my iterator in jsp, it doesn not use value from propertyList but from session !
My question is it is correct behaviour ?
This is defined behavior; whether or not it's "correct" is debatable.
Because you're using JSP EL, the Struts request wrapper is responsible for resolving JSP EL expressions. The normal application scopes are searched first (e.g., application, session, request). If nothing is found, only then will the value stack be queried for matching expressions.
If you accessed item via non-JSP EL means, e.g., the <s:property> tag, only the value stack would be queried, and you'd get the expected behavior.
When you mix ELs results are not always what you'd expect, so you must be aware how the frameworks in question relate to each other.

How to set autocomplete="off" in vaadin

Is it possible to set HTML5 attribute autocomplete="off" on TextField in Vaadin 7?
I've searched but found no way to set attributes on text fields or just hint browser to disable native autocompletion on input fields in some other way in vaadin.
I think the only way if you use javascript:
TextField tf = new TextField();
tf.addStyleName("xyz");
JavaScript.getCurrent().execute(
"document.getElementsByClassName('xyz')[0].setAttribute('autocomplete', 'off')");
Extend the TextField...
package com.example;
import com.vaadin.ui.TextField;
public class MyTextField extends TextField {
// do other customization here as needed
}
...and - what's the key point here - its client-side Connector
package com.example.client;
import com.vaadin.client.ui.VTextField;
import com.vaadin.client.ui.textfield.TextFieldConnector;
import com.vaadin.shared.ui.Connect;
#Connect(com.example.MyTextField.class)
public class MyTextFieldConnector extends TextFieldConnector {
#Override
public VTextField getWidget() {
VTextField vTextField = super.getWidget();
vTextField.getElement().setAttribute("autocomplete","off");
return vTextField;
}
}
Don't forget to recompile the widget set.
If you use the Viritin add-on, you can now use the HtmlElementPropertySetter class to wrap your TextField component and use that to set the "autocomplete" element property to "off". You could also use the MTextField component that comes with Viritin and just create it as follows:
MTextField username = new MTextField("Username")
.withAutocompleteOff();
This is an extension to #Wojciech Marciniak's answer. His approach worked for me, but I want to note a couple or three modifications I had to do in order for it to work as of 2017/11/28.
1) autocomplete="off" don't seem to work anymore nowadays; at least not on Chrome. Instead, you can use autocomplete="new-password", which works on Chrome 62.0.3202.94 windows 64 bits. I also noticed some inconsistent behaviour with this attribute, as NOT always works - sometimes a list with choices for passwords will show up on the component (specially until you refresh a couple of times, etc.).
2a) Instead of extending the component, you may want to overwrite it by creating the com.vaadin.client.ui.(component)field package in your project, then put the modified (component)FieldConnector.java file in it (in my case I was modifying PasswordField) in case you want all your instances of this component to not remember passwords. The final class source should look like this:
package com.vaadin.client.ui.passwordfield;
import com.vaadin.client.ui.VPasswordField;
import com.vaadin.client.ui.textfield.TextFieldConnector;
import com.vaadin.shared.ui.Connect;
import com.vaadin.ui.PasswordField;
#Connect(PasswordField.class)
public class PasswordFieldConnector extends TextFieldConnector {
#Override
public VPasswordField getWidget() {
VPasswordField vTextField = (VPasswordField) super.getWidget();
vTextField.getElement().setAttribute("autocomplete","new-password");
return vTextField;
}
}
So this way you don't need any other class extending TextField (or PasswordField).
2b) If you want to allow some fields to remember passwords and other that don't, you can extend the component and use your preferred component accordingly. You can keep your connector class as in 2a) but remember to name it something like CustomPasswordFieldConnector, and it should also #Connect with that CustomPasswordField.class, put that class wherever it fits in your project and remember to add the proper import for it in the connector in case it's needed. This class is just a dummy one - you can leave its contents empty in case you don't need any extra functionality (but remember it should extend the proper (component)Field; PasswordField in the example).

How to internationalize a converter or renderer in Wicket?

I'm translating a web application and things are generally going smoothly with wicket:message and properties files. But Wicket always wants to have a component for looking up strings.
How can I translate converters and renderers (i.e. implementations of IConverter and IChoiceRenderer) which don't have access to any Wicket component in their methods?
So far I found one way - Application.get().getResourceSettings().getLocalizer().getString(key, null) - but I have to make the strings "global", i.e. associated with the application class. That's not nice for separation and reuse. How can I do it better?
I think you should invent you own way how to achieve this. Here in my current project we registered our own IStringResourceLoader like this:
IStringResourceLoader stringResourceLoader = new OurOwnResourceLoaderImpl();
Application.get().getResourceSettings().getStringResourceLoaders().add(stringResourceLoader);
Then for example in IChoiceRenderer we just call Application.get().getLocalizer().getString("key", null).
Inside our IStringResourceLoader we are looking for bundles (property files) with some string pattern according our own conventions.
Or you can just register localization bundle (ie. properties file) distributed inside your library's jar in Application#init through org.apache.wicket.resource.loader.BundleStringResourceLoader.
Afaik there is no standard way to do that so it's up to you what path you choose.
Updated:
I found another solution how your library/extension can register it's own localization by itself so you needn't to touch Application#init or create your own IStringResourceLoaders.
There is preregistered string resource loader org.apache.wicket.resource.loader.InitializerStringResourceLoader (see wickets default IResourceSetting implementation ie. ResourceSetting and it's constructor) which uses wicket's Initializer mechanism - see IInitializer javadoc - basically you add wicket.properties file in your jar class root (ie. it is in default/none package) and inside file there is:
initializer=i.am.robot.MyInitilizer
then i.am.robot.MyInitilizer:
public class MyInitializer implements IInitializer {
/**
* #param application
* The application loading the component
*/
void init(Application application) {
// do whatever want
}
/**
* #param application
* The application loading the component
*/
void destroy(Application application) {
}
}
and now you create your localization bundles in same package and same name as IInitializer implementation (in our example MyInitializer)
I think I found another way...
I noticed that IStringResourceLoader also has a method String loadStringResource(Class<?> clazz, String key, Locale locale, String style); (and one more parameter for variation in newer Wicket versions) which does not require a component. clazz is supposed to be a component class, but... it doesn't actually have to be :)
I was able to implement my own class MyLocalizer extends Localizer with a new method
getString(String key, Class<?> cl, IModel<?> model, Locale locale, String defaultValue)
which works in a similar way to
getString(String key, Component component, IModel<?> model, String defaultValue)
but uses the class directly instead of a component. It still uses the same properties cache and resource loaders.
Then I wrote an abstract class MyConverter implements IConverter which has a MyLocalizer getLocalizer() and a few getString methods like the Component class. Basically it does getLocalizer().getString(key, getClass(), model, locale, defaultValue), so the properties can now be attached to the converter class.
Seems to work :)
If I understand your question...
You can use package based properties that means if you put your keys/values into a property file 'package.properties' in a package. Each localized resource of any subpackage under that package returns the value associated to the requested key until you override it in another property file.
The file name is 'package.properties' in Wicket prior to 1.6.x and 'wicket-package.properties' in Wicket 1.6+
See
https://cwiki.apache.org/confluence/display/WICKET/Migration+to+Wicket+6.0#MigrationtoWicket6.0-package.propertiesrenamedtowicket-package.properties
However it works just for componet, outside the componet (when component argument is null), it is possible to use:
WicketApplication.properties (the WebApplication class is WicketApplication.class, this property file is in the same package).
applicationGlobalProperty=My Global Localized Property
wicket-package.properties (package based, place it in the same package as the page)
localText=Localized text: A local component text based on wicket-package.properties
LocalizedPage.html (markup template)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Localized Page</title>
</head>
<body xmlns:wicket="http://wicket.apache.org">
<div>
<div>
<h2>Texts</h2>
<div>
<wicket:message key="localText"/> <br/>
<span wicket:id="localizedLabel"></span>
</div>
</div>
</div>
</body>
</html>
LocalizePage.java (code)
public class LocalizedPage extends WebPage {
private static final long serialVersionUID = 1L;
public LocalizedPage() {
super();
}
#Override
protected void onInitialize() {
super.onInitialize();
add(new Label("localizedLabel", new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 1L;
#Override
public String getObject() {
return WicketApplication.get().getResourceSettings().getLocalizer().getString("applicationGlobalProperty", null);
}
}));
}
}
See the full example on https://repo.twinstone.org/projects/WISTF/repos/wicket-examples-6.x/browse

How to use the selected values from the s:inputtransferselect struts tag

I am unable to find the attribute which holds the selected value in the case of <s:inputtransferselect/> Struts Tag. I referred to the documentation but I'm still unable to figure out which attribute exactly holds the selected values.
In the case of <s:optiontransferselect/>, I was able to get the selected values from the doubleId attribute. I am looking for a similar attribute for <s:inputtransferselect/>.
Inputtransferselect creates an input field, a multi option select list and some buttons to add and remove from the select list. When you submit your form this triggers some javascript to select all of the options in the select list, these options then appear in the list on the java side.
Here is an example.
Your .jsp file:
<s:inputtransferselect
key="cartoonCharacters"
addLabel="Add element"
removeLabel="Remove element"
removeAllLabel="Remove all elements"
upLabel="Move up"
downLabel="Move down"
leftTitle="Add Character"
rightTitle="Existing Characters"
list="cartoonCharacters"
/>
Make sure that your .jsp includes a <s:head/> tag else the javascript required for the inputtransferselect to work won't be available to your page.
To see all available attribute parameters of this tag refer to the documentation.
Your action .java file should contain a list which corisponds to the key and list of your inputtransferselect. To get hold of the values, use the list's getter:
public abstract class MyAction extends AbstractAction
{
private List<String> cartoonCharacters = new ArrayList<>();
public MyAction()
{
cartoonCharacters.add("Popeye");
cartoonCharacters.add("Superman");
cartoonCharacters.add("Scoobydoo");
}
public List<String> getCartoonCharacters()
{
return this.cartoonCharacters;
}
public void setCartoonCharacters(List pCartoonCharacters)
{
this.cartoonCharacters = pCartoonCharacters;
}
public String execute() throws Exception
{
return SUCCESS;
}
}
I should also note that in my implementation I haven't got this to work properly - the javascript to automatically select all options on form submit isn't firing so I had to write my own event for this, it's probably something to do with my other on submit events clashing.

Silverlight DataGridColumn AttachedProperties

I am attempting to create an AttachedProperty for a DataGridColumn within Silverlight 3.0 and I am having some issues.
Here is the AttachedProperty:
public class DataGridColumnHelper
{
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.RegisterAttached("Header", typeof(string), typeof(DataGridColumnHelper),
new PropertyMetadata(OnHeaderPropertyChanged));
private static void OnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
string header = GetHeader(d);
var dataGridColumn = d as DataGridColumn;
if (dataGridColumn == null)
{
return;
}
dataGridColumn.Header = GetHeader(dataGridColumn);
}
public static string GetHeader(DependencyObject obj)
{
return (string)obj.GetValue(HeaderProperty);
}
public static void SetHeader(DependencyObject obj, string value)
{
obj.SetValue(HeaderProperty, value);
}
}
As you can see it is really simple, I am trying to overcome the limitation that the Header Property in the DataGridColumn class cannot be bound.
This XAML works as expected...
<Controls:DataGridTextColumn Binding="{Binding OwnerName}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="User Name"/>
However this XAML throws an error...(Specifically: {System.Windows.Markup.XamlParseException: AG_E_PARSER_PROPERTY_NOT_FOUND [Line: 224 Position: 112]
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
....})
<Controls:DataGridTextColumn Binding="{Binding OwnerName}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="{Binding Resources.UserNameListViewHeading, Source={StaticResource Labels}}"/>
Just for experimentation I attached this property (with the binding syntax above) to a DataGrid and checked the DataGridColumnHelper.Header property in the OnHeaderPropertyChanged method and the value was correct (and an exception wasn't thrown)
It is my understanding that the object that the AttachedProperty is attached to must be a DependencyProperty. Looking through Reflector, DataGridColumn (from which DataGridTextColumn derives) derives from DependencyProperty.
Can somebody please shed some light on this? I am trying to Localize our application, and I am having trouble with the DataGrid. I am sure I can do this in code-behind, but I am trying to avoid that.
Chris, the problem is very simple, this won't work because the DataGridTextColumn is "detached" from the Visual Tree. Your DataGridTextColumn object is rooted in the Columns collection of the DataGrid - see the indirection. So even attached properties will not work as you expect. Now there is a way to make all this work using something I'm calling Attached Bindings, see:
http://www.orktane.com/Blog/post/2009/09/29/Introducing-nRouteToolkit-for-Silverlight-(Part-I).aspx
Just remember to attach the binding properties using something that is in the VisualTree (so the Grid holding the column would do just fine.)
Hope this helps.
Try using this, im assuming UserName is a property in your viewmodel
<Controls:DataGridTextColumn Binding="{Binding OwnerName}"
HeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
Behaviors:DataGridColumnHelper.Header="{Binding UserName}"/>
I cant test your scenario so my post is just an idea. Might work, might not.

Resources