Skip to content

Commit 8d1666f

Browse files
committed
refactored localization logic
1 parent 4fbae2b commit 8d1666f

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

src/main/java/com/civilizer/config/Configurator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ private static Locale getLocaleByName(String name) {
7575
}
7676

7777
public static Locale resolveLocale(Locale localeFromClient) {
78+
// A locale setting specified via application options (e.g. app-options.properties)
7879
final String localeFromAppOption = System.getProperty(AppOptions.LOCALE);
7980
Locale locale = getLocaleByName(localeFromAppOption);
8081
if (locale == null) {
82+
// Or we use a locale setting from client (e.g. cookie)
8183
assert localeFromAppOption != null;
8284
locale = localeFromClient;
8385
}

src/main/java/com/civilizer/web/controller/MainController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,9 @@ public String onRequestForFragment(ModelMap model, @PathVariable Long fragmentId
951951
}
952952

953953
@RequestMapping(value = "/fragment/help", method = { RequestMethod.GET })
954-
public String onRequestForFragment(ModelMap model, HttpServletRequest req) {
955-
final String title = ViewUtil.getHelpString("help_title", req);
956-
final String content = ViewUtil.getHelpString("help_content", req);
954+
public String onRequestForFragment(ModelMap model) {
955+
final String title = ViewUtil.getHelpString("help_title");
956+
final String content = ViewUtil.getHelpString("help_content");
957957
final Fragment frg = new Fragment(title, content, null);
958958
model.addAttribute("fragmentType", "app-help-fragment");
959959
return onRequestForFragment(model, frg);

src/main/java/com/civilizer/web/view/ViewUtil.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,13 @@ public static void setLocale(RequestContext rc) {
7474
}
7575

7676
public static String getResourceBundleString(String key) {
77-
Locale locale;
78-
final RequestContext rc = RequestContextHolder.getRequestContext();
79-
if (rc == null) {
80-
locale = new Locale(System.getProperty(AppOptions.CUR_LOCALE));
81-
}
82-
else {
83-
final ServletExternalContext ec = (ServletExternalContext)rc.getExternalContext();
84-
final HttpServletRequest req = (HttpServletRequest)ec.getNativeRequest();
85-
locale = RequestContextUtils.getLocale(req); // Retrieve the locale info from the cookie
86-
}
77+
final Locale locale = Configurator.getCurLocale();
8778
final ResourceBundle bundle = ResourceBundle.getBundle(MESSAGE_RESOURCE_BASE_NAME, locale);
8879
return bundle.getString(key);
8980
}
9081

91-
public static String getHelpString(String key, HttpServletRequest req) {
92-
final Locale locale = RequestContextUtils.getLocale(req);
82+
public static String getHelpString(String key) {
83+
final Locale locale = Configurator.getCurLocale();
9384
final ResourceBundle bundle = ResourceBundle.getBundle(HELP_RESOURCE_BASE_NAME, locale);
9485
return bundle.getString(key);
9586
}

src/main/resources/i18n/MessageResources_en.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ close_others=Close Others
118118
support_cvz=Do you think Civilizer is useful for you? Then, please, support Civilizer
119119
remember_fragments=Remember These Fragments?
120120
reminder_interval=Reminder Interval
121+
no_locale_dlg=You've set locale via app-options.properties or similar means. This dialog is not available in that case.
121122

122123
# format patterns
123124
date_time_format=YYYY-MM-dd HH:mm

src/main/resources/i18n/MessageResources_ja.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ close_others=\u4ed6\u3092\u9589\u3058\u308b
118118
support_cvz=\u30b7\u30d3\u30e9\u30a4\u30b6\u30fc\u304c\u3082\u3063\u3068\u5f79\u306b\u7acb\u3064\u3088\u3046\u5fdc\u63f4\u3057\u3066\u304f\u3060\u3055\u3044\uff0e
119119
remember_fragments=\u3053\u308c\u3089\u306e\u30d5\u30e9\u30b0\u30e1\u30f3\u30c8\u3092\u899a\u3048\u3066\u3044\u307e\u3059\u304b?
120120
reminder_interval=\u30ea\u30de\u30a4\u30f3\u30c9\u306e\u767a\u751f\u5468\u671f
121+
no_locale_dlg=app-options.properties\u30D5\u30A1\u30A4\u30EB\u7D4C\u7531\u306A\u3069\u3067\u30ED\u30B1\u30FC\u30EB\u3092\u8A2D\u5B9A\u3057\u307E\u3057\u305F\uFF0E\u305D\u306E\u5834\u5408\uFF0C\u3053\u306E\u30C0\u30A4\u30A2\u30ED\u30B0\u3092\u5229\u7528\u3059\u308B\u3053\u3068\u306F\u51FA\u6765\u307E\u305B\u3093\uFF0E
121122

122123
# format patterns
123124
date_time_format=YYYY-MM-dd HH:mm

src/main/webapp/WEB-INF/flows/home/home.xhtml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,23 @@
178178
<p:dialog widgetVar="localeSettingsDlg" header="#{msg['change_locale']}"
179179
width="440px" modal="true" closeOnEscape="true" showEffect="fade" position="center" draggable="true" resizable="false">
180180

181-
<p:panel>
182-
<span value="en" class="locale-item-btn fa fa-flag">#{msg['locale_en']}</span>
183-
<span value="ja" class="locale-item-btn fa fa-flag">#{msg['locale_ja']}</span>
184-
<!-- <span value="ko" class="locale-item-btn fa fa-flag">#{msg['locale_ko']}</span> -->
185-
</p:panel>
181+
<c:choose>
182+
<c:when test="#{not empty systemProperties['civilizer.locale']}">
183+
<p:panel>
184+
<h:outputText value="#{msg['no_locale_dlg']}" />
185+
</p:panel>
186+
</c:when>
187+
<c:otherwise>
188+
<p:panel>
189+
<span value="en" class="locale-item-btn fa fa-flag">#{msg['locale_en']}</span>
190+
<span value="ja" class="locale-item-btn fa fa-flag">#{msg['locale_ja']}</span>
191+
<!-- <span value="ko" class="locale-item-btn fa fa-flag">#{msg['locale_ko']}</span> -->
192+
</p:panel>
186193

187-
<p:commandButton onclick="LSC.onClickApply()" value="#{msg['ok']}" icon="ui-icon-check"/>
188-
<p:commandButton onclick="LSC.onClickCancel()" value="#{msg['cancel']}" icon="ui-icon-close"/>
194+
<p:commandButton onclick="LSC.onClickApply()" value="#{msg['ok']}" icon="ui-icon-check"/>
195+
<p:commandButton onclick="LSC.onClickCancel()" value="#{msg['cancel']}" icon="ui-icon-close"/>
196+
</c:otherwise>
197+
</c:choose>
189198
</p:dialog>
190199
</h:form>
191200

0 commit comments

Comments
 (0)