You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RememberToTheSetTheLocale.asciidoc 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ---
  2. title: Remember To The Set The Locale
  3. order: 23
  4. layout: page
  5. ---
  6. [[remember-to-the-set-the-locale]]
  7. = Remember to the set the locale
  8. The Locale of an application or an individual component dictates the
  9. language and format used for displaying and parsing numbers and dates.
  10. This includes things like names of months, 12 or 24 hour time formats,
  11. the order of days and months in dates, and decimal separators in
  12. numbers.
  13. _Displaying_ values in a locale foreign to the user is bad enough.
  14. Forcing your users to _enter_ values in a foreign locale (such as
  15. decimal separator periods instead of commas, or vice versa) can be
  16. considered a cruel (although sadly not particularly unusual) punishment.
  17. A Vaadin application’s locale, if not explicitly set, defaults to the
  18. locale defined in the request headers sent by the browser , or, if
  19. that’s missing, the locale returned by `java.util.Locale.getDefault()`,
  20. which in turn is the default locale of the Java Virtual Machine, which
  21. depends on the configuration of the environment in which it happens to
  22. be running. Either way, chances are that the end result does not match
  23. what your users expect.
  24. [[per-component]]
  25. Per Component
  26. ~~~~~~~~~~~~~
  27. Vaadin components have a *`setLocale()`* method that allows specifying
  28. the locale separately for each component.
  29. [source,java]
  30. ....
  31. InlineDateField datePicker = new InlineDateField();
  32. datePicker.setLocale( java.util.Locale.CANADA_FRENCH );
  33. ....
  34. [[inheritance]]
  35. Inheritance
  36. ^^^^^^^^^^^
  37. A component _inherits its locale_ from its parent. So setting the locale
  38. on a component affects its nested children components by default.
  39. Remember that layouts and even the
  40. https://vaadin.com/api/7.2.5/com/vaadin/ui/UI.html[UI] (Vaadin 7) object
  41. are components too, so setting their locale affects their content.
  42. [[session-default]]
  43. Session Default
  44. ~~~~~~~~~~~~~~~
  45. Instead of setting a locale for each individual component, you may set a
  46. Session-wide default. The components inherit that property. You may
  47. continue to override this session-default locale for individual
  48. components, where needed, with a call to `setLocale`.
  49. Setting the default locale changed between Vaadin 6 and 7.
  50. [[in-vaadin-6]]
  51. In Vaadin 6
  52. ^^^^^^^^^^^
  53. In Vaadin 6, you set the locale by calling
  54. https://vaadin.com/api/6.8.9/com/vaadin/Application.html#setLocale(java.util.Locale)[`setLocale`]method
  55. on the
  56. https://vaadin.com/api/6.8.9/com/vaadin/Application.html[`Application`]
  57. object. See
  58. https://vaadin.com/book/vaadin6/-/page/advanced.global.html[chapter
  59. 12.14 Accessing Session-Global Data] of the Book of Vaadin, Vaadin 6
  60. edition, for important information about accessing the Application
  61. object.
  62. [[in-vaadin-7]]
  63. In Vaadin 7
  64. ^^^^^^^^^^^
  65. In Vaadin session, you must go through the current `VaadinSession`
  66. object to call `setLocale`. You may access that object in either of two
  67. ways:
  68. * Through that class’ static method `getCurrent` +
  69. * Jumping from a component object to a UI object to the current
  70. VaadinSession object.
  71. [[bug-workaround]]
  72. Bug & Workaround\\
  73. ++++++++++++++++++
  74. Unfortunately, there is a bug (or design issue) in Vaadin 7. See
  75. http://dev.vaadin.com/ticket/12350[ticket 12350]. The problem is that
  76. setting the locale of the VaadinSession does not affect already existing
  77. UI objects. So, while the `init` method of your app's UI is the natural
  78. place to set the session's locale, doing so does not take effect in that
  79. current UI until after a page refresh.
  80. The *workaround* is to explicitly set the current UI object's locale, in
  81. addition to setting the VaadinSession object's locale. The first affects
  82. the current UI, while the second affects any future UI objects that may
  83. be instantiated in your app.
  84. This ( if in a UI object's "init" method)…
  85. [source,java]
  86. ....
  87. Locale locale = Locale.CANADA_FRENCH;
  88. this.setLocale( locale ); // Call to affect this current UI. Workaround for bug: http://dev.vaadin.com/ticket/12350
  89. this.getSession().setLocale( locale ); // Affects only future UI instances, not current one because of bug. See workaround in line above.
  90. // VaadinSession.getCurrent().setLocale( locale ); // Alternative to "this.getSession".
  91. ....
  92. …or this ( if not in a UI object, add "getUI()" )…
  93. [source,java]
  94. ....
  95. Locale locale = Locale.CANADA_FRENCH;
  96. this.getUI().setLocale( locale ); // Call to affect this current UI. Workaround for bug: http://dev.vaadin.com/ticket/12350
  97. this.getUI().getSession().setLocale( locale ); // Affects only future UI instances, not current one because of bug. See workaround in line above.
  98. // VaadinSession.getCurrent().setLocale( locale ); // Alternative to "this.getSession".
  99. ....
  100. [[example-app]]
  101. Example App
  102. ^^^^^^^^^^^
  103. Here is source code for a complete Vaadin 7 example app.
  104. [source,java]
  105. ....
  106. package com.example.vaadinexperiment;
  107. import com.vaadin.annotations.Theme;
  108. import com.vaadin.annotations.VaadinServletConfiguration;
  109. import com.vaadin.server.VaadinRequest;
  110. import com.vaadin.server.VaadinServlet;
  111. import com.vaadin.server.VaadinSession;
  112. import com.vaadin.ui.InlineDateField;
  113. import com.vaadin.ui.UI;
  114. import com.vaadin.ui.VerticalLayout;
  115. import java.util.Locale;
  116. import javax.servlet.annotation.WebServlet;
  117. @Theme ( "mytheme" )
  118. @SuppressWarnings ( "serial" )
  119. public class MyVaadinUI extends UI
  120. {
  121. @WebServlet ( value = "/*" , asyncSupported = true )
  122. @VaadinServletConfiguration ( productionMode = false , ui = MyVaadinUI.class , widgetset = "com.example.vaadinexperiment.AppWidgetSet" )
  123. public static class Servlet extends VaadinServlet
  124. {
  125. }
  126. @Override
  127. protected void init ( VaadinRequest request )
  128. {
  129. final VerticalLayout layout = new VerticalLayout();
  130. layout.setMargin( true );
  131. layout.setSpacing( true );
  132. setContent( layout );
  133. Locale locale = Locale.CANADA_FRENCH;
  134. this.setLocale( locale ); // Call to affect this current UI. Workaround for bug: http://dev.vaadin.com/ticket/12350
  135. this.getSession().setLocale( locale ); // Affects only future UI instances, not current one because of bug. See workaround in line above.
  136. // VaadinSession.getCurrent().setLocale( locale ); // Alternative to "this.getSession".
  137. InlineDateField datePicker = new InlineDateField();
  138. datePicker.setCaption( "This component’s locale → defaults to parent’s, → which defaults to VaadinSession’s, → which defaults to JVM" );
  139. layout.addComponent( datePicker );
  140. InlineDateField datePickerFinnish = new InlineDateField();
  141. datePickerFinnish.setCaption( "This component’s default locale is overridden for Finnish" );
  142. datePickerFinnish.setLocale( new Locale( "fi" , "FI" ) );
  143. layout.addComponent( datePickerFinnish );
  144. }
  145. }
  146. ....
  147. image:http://i.imgur.com/w9CViCR.png[Calendar]
  148. '''''
  149. Information here drawn from
  150. http://stackoverflow.com/q/16331112/642706[this StackOverflow.com
  151. question].