You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hop.apache.org by ha...@apache.org on 2022/04/02 12:29:22 UTC

[hop] branch master updated: Cleanup alt-key suffix in message on macOS

This is an automated email from the ASF dual-hosted git repository.

hansva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/master by this push:
     new 7c44f92  Cleanup alt-key suffix in message on macOS
     new 1b6ab7a  Merge pull request #1440 from shlxue/i18n_alt-key
7c44f92 is described below

commit 7c44f928615139ec1d4e4fb7ba4e6f4ccf06b58e
Author: Shl Xue <xu...@gmail.com>
AuthorDate: Fri Apr 1 23:27:36 2022 +0800

    Cleanup alt-key suffix in message on macOS
---
 .../main/java/org/apache/hop/laf/LafFactory.java   |  4 ++
 .../apache/hop/laf/RemoveAltKeyMessageHandler.java | 69 ++++++++++++++++++++++
 .../hop/laf/RemoveAltKeyMessageHandlerTest.java    | 39 ++++++++++++
 3 files changed, 112 insertions(+)

diff --git a/core/src/main/java/org/apache/hop/laf/LafFactory.java b/core/src/main/java/org/apache/hop/laf/LafFactory.java
index 2ab79a2..47fbcde 100644
--- a/core/src/main/java/org/apache/hop/laf/LafFactory.java
+++ b/core/src/main/java/org/apache/hop/laf/LafFactory.java
@@ -17,6 +17,7 @@
 
 package org.apache.hop.laf;
 
+import org.apache.hop.core.Const;
 import org.apache.hop.i18n.IMessageHandler;
 import org.apache.hop.i18n.LafMessageHandler;
 
@@ -42,6 +43,9 @@ public class LafFactory {
   static {
     handlerRef.put(IMessageHandler.class.getName(), _defMessageHandler);
     handlerRef.put(IPropertyHandler.class.getName(), _defPropertyHandler);
+    if (Const.isOSX()){
+      handlerRef.put(IMessageHandler.class.getName(), RemoveAltKeyMessageHandler.class);
+    }
   }
 
   @SuppressWarnings("unchecked")
diff --git a/core/src/main/java/org/apache/hop/laf/RemoveAltKeyMessageHandler.java b/core/src/main/java/org/apache/hop/laf/RemoveAltKeyMessageHandler.java
new file mode 100644
index 0000000..498d753
--- /dev/null
+++ b/core/src/main/java/org/apache/hop/laf/RemoveAltKeyMessageHandler.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hop.laf;
+
+import org.apache.hop.i18n.IMessageHandler;
+import org.apache.hop.i18n.LafMessageHandler;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+class RemoveAltKeyMessageHandler implements IMessageHandler {
+  private final IMessageHandler defMessageHandler;
+  private final Pattern altKeyPattern = Pattern.compile("\\(&[A-Z]\\)(\\.{3})?$");
+
+  public RemoveAltKeyMessageHandler() {
+    this(new LafMessageHandler());
+  }
+
+  RemoveAltKeyMessageHandler(IMessageHandler messageHandler) {
+    this.defMessageHandler = messageHandler;
+  }
+
+  @Override
+  public String getString(String key) {
+    return trimAltKey(defMessageHandler.getString(key));
+  }
+
+  @Override
+  public String getString(String packageName, String key) {
+    return trimAltKey(defMessageHandler.getString(packageName, key));
+  }
+
+  @Override
+  public String getString(String packageName, String key, String... parameters) {
+    return trimAltKey(defMessageHandler.getString(packageName, key, parameters));
+  }
+
+  @Override
+  public String getString(
+      String packageName, String key, Class<?> resourceClass, String... parameters) {
+    return trimAltKey(defMessageHandler.getString(packageName, key, resourceClass, parameters));
+  }
+
+  private String trimAltKey(String value) {
+    Matcher matcher = altKeyPattern.matcher(value);
+    if (matcher.find()) {
+      value = value.substring(0, matcher.start());
+      if (matcher.group(1) != null){
+          value += matcher.group(1);
+      }
+    }
+    return value;
+  }
+}
diff --git a/core/src/test/java/org/apache/hop/laf/RemoveAltKeyMessageHandlerTest.java b/core/src/test/java/org/apache/hop/laf/RemoveAltKeyMessageHandlerTest.java
new file mode 100644
index 0000000..44eafed
--- /dev/null
+++ b/core/src/test/java/org/apache/hop/laf/RemoveAltKeyMessageHandlerTest.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hop.laf;
+
+import org.apache.hop.i18n.IMessageHandler;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static org.junit.Assert.assertEquals;
+
+public class RemoveAltKeyMessageHandlerTest {
+
+  @Test
+  public void testChineseStyleAltKeyMessage() {
+    IMessageHandler defMessageHandler = Mockito.mock(IMessageHandler.class);
+    Mockito.when(defMessageHandler.getString("a")).thenReturn("a message");
+    Mockito.when(defMessageHandler.getString("b")).thenReturn("Edit(&E)");
+    Mockito.when(defMessageHandler.getString("c")).thenReturn("Open(&O)...");
+    IMessageHandler messageHandler = new RemoveAltKeyMessageHandler(defMessageHandler);
+    assertEquals("a message", messageHandler.getString("a"));
+    assertEquals("Edit", messageHandler.getString("b"));
+    assertEquals("Open...", messageHandler.getString("c"));
+  }
+}