JSF2 internationalization example - jsf-2

Taking as a start point the JSF2 internationalization example of this link:
http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
I want this example to display the language of the combos in the actual language selected.
Could somebody point me how this can be done?
Thanks!

It's just a matter of setting the proper labels in the static variable countries:
static {
countries = new LinkedHashMap<String,Object>();
countries.put("English", Locale.ENGLISH); //label, value
countries.put("Deutsch", Locale.GERMAN);
countries.put("Français", Locale.FRENCH);
// ... fill in with additional languages/locales as needed
}
You can get a bigger list of language names in their original language here: http://www.omniglot.com/language/names.htm
UPDATE: According to OP's comment, he needs the language names translated to every language. For that, one could come up with a solution simply making a map of language maps (that's quite a few maps), like this:
// set a default value for localeCode
private String localeCode = Locale.ENGLISH.toString();
// ...
static {
countries = new LinkedHashMap<Object, <String,Object>>();
englishCountries = new LinkedHashMap<String,Object>();
englishCountries.put("English", Locale.ENGLISH); //label, value
englishCountries.put("German", Locale.GERMAN);
englishCountries.put("French", Locale.FRENCH);
countries.put(Locale.ENGLISH, englishCountries);
germanCountries = new LinkedHashMap<String,Object>();
germanCountries.put("Englisch", Locale.ENGLISH);
germanCountries.put("Deutsch", Locale.GERMAN);
germanCountries.put("Französisch", Locale.FRENCH);
countries.put(Locale.GERMAN, germanCountries);
frenchCountries = new LinkedHashMap<String,Object>();
frenchCountries.put("Anglais", Locale.ENGLISH);
frenchCountries.put("Allemand", Locale.GERMAN);
frenchCountries.put("Français", Locale.FRENCH);
countries.put(Locale.FRENCH, frenchCountries);
// ... fill in with additional languages/locales as needed
}
public Map<Object, <String,Object>> getCountriesInMap() {
return countries;
}
// adapted value change listener from original:
public void countryLocaleCodeChanged(ValueChangeEvent e){
String newLocaleValue = e.getNewValue().toString();
//loop country map to compare the locale code
for (Object key : countries.keySet()) {
if (key.toString().equals(newLocaleValue)) {
FacesContext.getCurrentInstance().getViewRoot()
.setLocale((Locale) key);
}
}
}
and then you would choose the proper map to be used for the selectItems, with something like this:
<h:selectOneMenu value="#{language.localeCode}" onchange="submit()"
valueChangeListener="#{language.countryLocaleCodeChanged}">
<f:selectItems value="#{language.countriesInMap[language.localeCode]}" />
</h:selectOneMenu>
Note: Don't forget to set a default value for language.localeCode, or the dropdown won't show any options
Please note, though, that this is probably NOT a good idea for usability, since a user who chooses a foreign language by mistake may have a hard time to get back to a known language for her (that's why it's a good practice to make the lists have the language names in each own language).

Related

Is there a way to change column name before fetching data?

Is there a way to change column name before fetching data in all the controllers I call that column from, for example I had 2 columns in my table "Section" : "Title" and "TitleEN"
If the current culture is "en", I want to fetch "TitleEn" data but in my code I'll only use "Title" like this:
section.Where(p =>
p.Title.ToUpper().Contains(searchword.ToUpper()));
I was working on a project with only one language and then the client requested adding English. Unfortunately I made the wrong decision by adding language fields for every table having string fields.
Now I want to fix this because the project has a lot of code dealing only with fields without "En".
I'm not entirely sure on how this is being called, but you could be a bit sneaky on the entity itself. I think you are saying that you are always using the Title property on the entity, but you need it to be EN when the culture is set to EN. If you don't mind selecting both values every time on every query, there is an easy fix for this.
In your entity, I would do something like this:
public class MyEntity
{
[Column("Title")] // Can use FluentApi here instead
public string TitleOld { get; set; }
[Column("TitleEN")]
public string TitleEN { get; set; }
[NotMapped]
public string Title
{
get { return Thread.CurrentThread.CurrentUICulture.Name == "en-US" ? TitleEN : TitleOld; }
set { TitleOld = value; }
}
}
This way, your code wouldn't change, but you'd be selecting both values down every time.
not the best solution, but you can just use an If currentcul is En, then
section.Where(p =>
p.TitleEN.ToUpper().Contains(searchword.ToUpper()));
Else{
section.Where(p =>
p.Title.ToUpper().Contains(searchword.ToUpper()));
}
I would recommend using a diffrent solution tho.
I hope this will work
var section = new List<Test>();
var result = section.Where(x => (System.Globalization.CultureInfo.CurrentCulture.Name.Equals("en")
&& x.TitleEn.ToUpper().Contains(searchword.ToUpper()))
|| (!System.Globalization.CultureInfo.CurrentCulture.Name.Equals("en")
&& x.Title.ToUpper().Contains(searchword.ToUpper())));

Nested bean : a collection inside an object

I got a simple POJO class that i wish to display / update in a form
Using the BeanItem class and the binding of component data, i was able to quickly display the first attributes of may data class. However i've hit a wall for tow related attributes :
my class posses a set of available status, as a list of object 'AppStatus'. it also possess a current status, that is one of the status in the 'available' list.
I would like to display the list in the form as a combobox, with the current status selected.
I'we managed to associate the 'available' attribute with a combobox, but i can't seem to be able to fill this combobox when setting the data source (method setItemDataSource). How do i get the avalaible status list and the current status from my Item ?
I could always use a workaround and add a parameter to the method to get the source objet in addition to the BeanItem, but i would prefer to avoid this if the Item properties can give me my attribute.
Regards
Edit : shortened exemple, with code from Eric R.
class Status {
String id;
Sting label
+ setter /getter
}
class App {
String AppId;
String AppLabel
ArrayList<Status> availablestatus;
Status currentStatus
+setter/getter
}
in the form extension, in the createField of the fieldfactory i added the following lines
if ("status".equals(propertyId)) {
// create the combobox
ComboBox status = new ComboBox(
texts.getString("application.label.status"));
status.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
status.setItemCaptionPropertyId("label");
status.setImmediate(true);
status.setNullSelectionAllowed(false);
IndexedContainer container = new IndexedContainer(
(Collection<ApplicationStatus>) item.getItemProperty(
"availableStatus").getValue());
status.setContainerDataSource(container);
status.setPropertyDataSource(item.getItemProperty("currentStatus"));
return status;
} else...
this didn't work, i do get a combobox, with the correct number of lines, but all empties.
i tried to use a beanContainer instead of a IndexedContainer
BeanContainer<String, ApplicationStatus> container =
new BeanContainer<String, ApplicationStatus>(ApplicationStatus.class);
container.addAll((Collection<ApplicationStatus>) item
.getItemProperty("availableStatus").
container.setBeanIdProperty("id");
the result is slightly better, since i do have the available values in the combobox.
only the currentValue is not selected...
I also tried to use a nestedbean property to get the id of the currentstatus, but the result is still not valid... i get a combobox, with the correct value selected, but i can not see others values anymore, since the combobox is readonly ?(even with setReadOnly(false);)
I suggest my way to resolve this. I don't think this is the nicest way, but it's works.
The beanItem class contains all you need.
I did the following in a simple project and it's work verry well :
ComboBox status = new ComboBox("ComboBox");
status.setImmediate(true);
status.setNullSelectionAllowed(false);
for(Status st : (Collection<Status>)item.getItemProperty("availableStatus").getValue()) {
status.addItem(st);
status.setItemCaption(st, st.getLabel());
}
status.setPropertyDataSource(item.getItemProperty("currentStatus"));
Hope it's works.
Regards Éric
From the vaadin demo site you can get this sample that show how to fill a combobox with countries. You could do the same i would guess (not sure I understand your problem 100%):
myForm.setFormFieldFactory(new MyFormFieldFactory ());
private class MyFormFieldFactory extends DefaultFieldFactory {
final ComboBox countries = new ComboBox("Country");
public MyFormFieldFactory () {
countries.setWidth(COMMON_FIELD_WIDTH);
countries.setContainerDataSource(ExampleUtil.getISO3166Container());
countries
.setItemCaptionPropertyId(ExampleUtil.iso3166_PROPERTY_NAME);
countries.setItemIconPropertyId(ExampleUtil.iso3166_PROPERTY_FLAG);
countries.setFilteringMode(ComboBox.FILTERINGMODE_STARTSWITH);
}
#Override
public Field createField(Item item, Object propertyId,
Component uiContext) {
Field f = (Field)item;
if ("countryCode".equals(propertyId)) {
// filtering ComboBox w/ country names
return countries;
}
return f;
}
}

ASP.NET Custom Localization

I've done quite a bit of research and I'm not sure how I should proceed with this.
Usual localization would change only when the language changes, so Hello for french would be Bonjour but my application needs to have special keywords for for certain users so UserX might say "Hello" needs to be "Allo".
I would like to have resource key with IdentityName_resourceKey and if this key is present take it otherwize fall back to resourceKey.
I'm thinking I need a custom ResourceProvider but my implementation is a simple if statement so I would not want to write a complete resource provider.
I wrote a extension of DisplayName attribute which works fine but this is not very good as I will need one of those for every data annotation attributes and this would not work if I use resources directly in pages or controllers...
public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
private readonly PropertyInfo _propertyInfo;
public LocalizedDisplayNameAttribute(string resourceKey, Type resourceType) : base(resourceKey)
{
var clientName = CustomMembership.Instance.CurrentUser.Client.Name;
_propertyInfo = resourceType.GetProperty(clientName + "_" + base.DisplayName, BindingFlags.Static | BindingFlags.Public)
?? resourceType.GetProperty(base.DisplayName, BindingFlags.Static | BindingFlags.Public);
}
public override string DisplayName
{
get
{
if (_propertyInfo == null)
{
return base.DisplayName;
}
return (string) _propertyInfo.GetValue(_propertyInfo.DeclaringType, null);
}
}
}
I'm looking for the best way to implement this with the least amount of code..
Thank you!
There is a better way, Data Annotations is your answer!
this is just a sample, you need go more deeper with System.Globalization.CultureInfo and Data Annotations (System.ComponentModel.DataAnnotations)
you can define your model class like this (assuming we have a resource file named CustomResourceValues with a value "strHello")
public class SomeObject(){
<Display(Name:="strHello", ResourceType:=GetType(My.Resources.CustomResourceValues))>
public string HelloMessage{ get; set; }
}
so, in our view the work must do it by the htmlhelper (assuming razor like render engine and the model is type of "SomeObject")
#Html.LabelFor(Function(x) x.HelloMessage)
basic info http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute.resourcetype(v=vs.95).aspx

example to override getValueFieldProperties FilterBuilder

I want to override getValueFieldProperties for FilterBuilde.
My requirement is for some specific type of field I want to show selection, for Value field instead of simple text box.
I have visited following:
http://code.google.com/p/smartgwt/source/browse/tags/2.5/main/src/com/smartgwt/client/widgets/form/FilterBuilder.java?r=1796
thanks.
I found the solution for the requirement requirement is, for some specific type of field, I want to show selection, for Value field instead of simple text box.
for (DataSourceField field : dataSource.getFields()) {
String type = field.getAttribute("serverType");
if (type!=null && type.equals("SPECIFIC_TYPE")) {
TreeMap<String, String> map = new TreeMap<String, String>();
map.put("1", "value 1");
map.put("2", "value 2");
field.setValueMap(map);
}
}

Text on TextBox with UpdateSourceTrigger=PropertyChanged is not updated when coercion of text input results in unchanged source value

I have a text box whose Text property has a TwoWay MultiBinding with UpdateSourceTrigger set to PropertyChanged. The first Binding is to a dependency property (Value) which has a PropertyChangedCallBack function that rounds the value to one decimal place.
The purpose of the text box is to perform the rounding as the user types rather than when the text box loses focus, hence why UpdateSourceTrigger is set to PropertyChanged.
The problem I am having is that if text is entered that does NOT result in Value changing, the Text property and Value become out of sync. Only if the rounding operation causes Value to change does Text get updated on the fly. E.g., if Text and Value are both 123.4 and the user types 1 after this, Value is rounded to the same value (123.4), but Text shows 123.41. However, if 9 is then typed after the 4, Value is rounded up to 123.5. And because of this actual change, Text is then updated to the same (123.5).
Is there any way of forcing a text box to update from its source even when the source hasn't changed since the last trigger? I have tried using BindingExpressionBase.UpdateTarget() but this only works when UpdateSourceTrigger is set to Explicit, which can't be used as Value no longer gets updated prior to a suitable time where UpdateTarget could be called (such as a TextInput handler). I have tried other methods such as explicitly updating the Text value from the bound Value, forcing an actual change to Value temporarily to invoke an update, but these "hacks" either don't work or cause other problems.
Any help would be greatly appreciated.
The code is below.
XAML snippet
<TextBox>
<TextBox.Text>
<MultiBinding Converter="{local:NumberFormatConverter}"
UpdateSourceTrigger="Explicit"
Mode="TwoWay">
<Binding Path="Value"
RelativeSource="{RelativeSource AncestorType={x:Type Window}}"
Mode="TwoWay" />
</MultiBinding>
</TextBox.Text>
</TextBox>
C# snippet
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value", typeof(decimal), typeof(MainWindow),
new FrameworkPropertyMetadata(0m,
new PropertyChangedCallback(OnValueChanged)));
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
obj.SetValue(ValueProperty, Math.Round((decimal)args.NewValue, 1));
}
Converter class required
public class NumberFormatConverter : MarkupExtension, IMultiValueConverter
{
public static NumberFormatConverter Instance { private set; get; }
static NumberFormatConverter()
{
Instance = new NumberFormatConverter();
}
public override object ProvideValue(IServiceProvider serviceProvider_)
{
return Instance;
}
#region Implementation of IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values[0].ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
var result = 0m;
if (value != null)
{
decimal.TryParse(value.ToString(), out result);
}
return new object[] { result };
}
#endregion
}
I did a little digging on the Internet, and it turns out this was broken in WPF 4. Someone with an almost-identical problem to me posted here:
http://www.go4answers.com/Example/textbox-shows-old-value-being-coerced-137799.aspx
'Answer 8' states this was broken in WPF 4 and suggests a solution, which is to actually use UpdateSourceTrigger="Explicit" but to handle the TextChanged event and call BindingExpression.UpdateSource() to force changes in the text box to be reflected in the underlying value as if UpdateSourceTrigger="PropertyChanged", as per this post:
Coerce a WPF TextBox not working anymore in .NET 4.0
I implemented this, but lo and behold there were further side effects, in particular that every keystroke caused the caret to jump to the start of the text box due to updating the source and raising a PropertyChanged event. And also, any leading or trailing zeros or decimal places entered with the intention of entering further digits would get wiped out immediately. So, a simple condition to check the parsed decimal value of the text box versus the underlying value resolved this.
The following event handler is all that was needed:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var tb = (TextBox)e.Source;
MultiBindingExpression binding = BindingOperations.GetMultiBindingExpression(tb, TextBox.TextProperty);
decimal result = 0m;
decimal.TryParse(tb.Text, out result);
if ((decimal)GetValue(ValueProperty) != result && binding != null)
{
int caretIndex = tb.CaretIndex;
binding.UpdateSource();
tb.CaretIndex = caretIndex;
}
}

Resources