You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by so...@apache.org on 2021/12/23 16:58:18 UTC
[wicket] 01/01: Save info about Locale direction LTR/RTL to Session metadata
This is an automated email from the ASF dual-hosted git repository.
solomax pushed a commit to branch session-locale-rtl
in repository https://gitbox.apache.org/repos/asf/wicket.git
commit a9cf794db1b42af32aad5bf2488dc2d623326eac
Author: Maxim Solodovnik <so...@gmail.com>
AuthorDate: Thu Dec 23 23:57:52 2021 +0700
Save info about Locale direction LTR/RTL to Session metadata
---
.../src/main/java/org/apache/wicket/Session.java | 7 +++
.../apache/wicket/core/util/lang/LocaleUtils.java | 51 ++++++++++++++++++++++
2 files changed, 58 insertions(+)
diff --git a/wicket-core/src/main/java/org/apache/wicket/Session.java b/wicket-core/src/main/java/org/apache/wicket/Session.java
index c4152c9..4f63eaa 100644
--- a/wicket-core/src/main/java/org/apache/wicket/Session.java
+++ b/wicket-core/src/main/java/org/apache/wicket/Session.java
@@ -30,6 +30,7 @@ import java.util.function.Supplier;
import org.apache.wicket.application.IClassResolver;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.core.request.ClientInfo;
+import org.apache.wicket.core.util.lang.LocaleUtils;
import org.apache.wicket.core.util.lang.WicketObjects;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.event.IEventSink;
@@ -126,6 +127,11 @@ public abstract class Session implements IClusterable, IEventSink, IMetadataCont
{
private static final long serialVersionUID = 1L;
};
+ /** records if pages have been unlocked for the current request */
+ public static final MetaDataKey<Boolean> IS_RTL = new MetaDataKey<>()
+ {
+ private static final long serialVersionUID = 1L;
+ };
/** Name of session attribute under which this session is stored */
public static final String SESSION_ATTRIBUTE_NAME = "session";
@@ -606,6 +612,7 @@ public abstract class Session implements IClusterable, IEventSink, IMetadataCont
if (!Objects.equal(getLocale(), locale))
{
this.locale.set(locale);
+ setMetaData(IS_RTL, LocaleUtils.isRtlLanguage(locale));
dirty();
}
return this;
diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/lang/LocaleUtils.java b/wicket-core/src/main/java/org/apache/wicket/core/util/lang/LocaleUtils.java
new file mode 100644
index 0000000..e8cd605
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/core/util/lang/LocaleUtils.java
@@ -0,0 +1,51 @@
+package org.apache.wicket.core.util.lang;
+
+import java.util.Locale;
+import java.util.regex.Pattern;
+
+import org.apache.wicket.util.lang.Args;
+
+public class LocaleUtils {
+ /**
+ * taken from BidiUtils
+ *
+ * A regular expression for matching right-to-left language codes. See
+ * {@link #isRtlLanguage} for the design.
+ */
+ private static final Pattern RtlLocalesRe = Pattern.compile("^(ar|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Arab|Hebr|Thaa|Nkoo|Tfng))"
+ + "(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)");
+
+ private LocaleUtils() {
+ // utility class
+ }
+
+ /**
+ * Check if a BCP 47 / III language code indicates an RTL language, i.e.
+ * either: - a language code explicitly specifying one of the right-to-left
+ * scripts, e.g. "az-Arab", or
+ * <p>
+ * - a language code specifying one of the languages normally written in a
+ * right-to-left script, e.g. "fa" (Farsi), except ones explicitly
+ * specifying Latin or Cyrillic script (which are the usual LTR
+ * alternatives).
+ * <p>
+ * The list of right-to-left scripts appears in the 100-199 range in
+ * http://www.unicode.org/iso15924/iso15924-num.html, of which Arabic and
+ * Hebrew are by far the most widely used. We also recognize Thaana, N'Ko,
+ * and Tifinagh, which also have significant modern usage. The rest (Syriac,
+ * Samaritan, Mandaic, etc.) seem to have extremely limited or no modern
+ * usage and are not recognized. The languages usually written in a
+ * right-to-left script are taken as those with Suppress-Script:
+ * Hebr|Arab|Thaa|Nkoo|Tfng in
+ * http://www.iana.org/assignments/language-subtag-registry, as well as
+ * Sindhi (sd) and Uyghur (ug). The presence of other subtags of the
+ * language code, e.g. regions like EG (Egypt), is ignored.
+ *
+ * @param languageString - locale string
+ * @return <code>true</code> in case passed locale is right-to-left
+ */
+ public static boolean isRtlLanguage(final Locale locale) {
+ Args.notNull(locale, "locale");
+ return RtlLocalesRe.matcher(locale.toLanguageTag()).find();
+ }
+}