Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP:Added Internationalization module #228

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions boms/minimal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<artifactId>devon4j-beanmapping-orika</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.devonfw.java.modules</groupId>
<artifactId>devon4j-i18n</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.devonfw.java.modules</groupId>
<artifactId>devon4j-security</artifactId>
Expand Down
46 changes: 46 additions & 0 deletions modules/i18n/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.devonfw.java.dev</groupId>
<artifactId>devon4j-modules</artifactId>
<version>dev-SNAPSHOT</version>
</parent>
<groupId>com.devonfw.java.modules</groupId>
<artifactId>devon4j-i18n</artifactId>
<version>${devon4j.version}</version>
<name>${project.artifactId}</name>
<packaging>jar</packaging>
<description>Module for i18n.</description>

<dependencies>
<dependency>
<groupId>net.sf.m-m-m</groupId>
<artifactId>mmm-util-cli</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>net.sf.m-m-m</groupId>
<artifactId>mmm-util-nls</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>com.devonfw.java.modules</groupId>
<artifactId>devon4j-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.devonfw.module.i18n.common.api.exception;

/**
* Signals Exception when locale is not found
*
*/
public class UnknownLocaleException extends RuntimeException {

/**
* Default serial version UID
*/
private static final long serialVersionUID = 1L;

/**
* Creates a new {@link UnknownLocaleException} with the given message
*
* @param msg error message
*/
public UnknownLocaleException(String msg) {

super(msg);
}

/**
* Creates a new {@link UnknownLocaleException} with the given message and the given cause
*
* @param msg error message
* @param ex cause of the created exception
*/
public UnknownLocaleException(String msg, Throwable ex) {

super(msg, ex);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* Copyright 2015-2018 Capgemini SE.
*
* Licensed 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 com.devonfw.module.i18n.common.api.nls;

import javax.inject.Named;

import net.sf.mmm.util.nls.api.NlsBundle;
import net.sf.mmm.util.nls.api.NlsBundleMessage;
import net.sf.mmm.util.nls.api.NlsMessage;

/**
* This is the {@link NlsBundle} for this application.
*
* @author kugawand
* @since dev
*
*/

public interface NlsBundleI18nRoot extends NlsBundle {
/**
* @param name
* @return
*/
@SuppressWarnings("javadoc")
@NlsBundleMessage("{name}. This Module is related to internationalization ")
public NlsMessage getLocale(@Named("name") String name);

/**
* @param name
* @return
*/
@SuppressWarnings("javadoc")
@NlsBundleMessage("Hello {name}")
NlsMessage messageSayHi(@Named("name") String name);

@NlsBundleMessage("Sorry. The login \"{login}\" is already in use. Please choose a different login.")
NlsMessage errorLoginInUse(@Named("login") String login);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.devonfw.module.i18n.common.util;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
* {@link JsonSerializer} for {@link Map}.
*/
public class BundleMapSerializer implements JsonSerializer<Map<String, String>> {

private static final Logger LOG = LoggerFactory.getLogger(BundleMapSerializer.class);

/**
* {@inheritDoc}
*/
@Override
public JsonElement serialize(final Map<String, String> bundleMap, final Type typeOfSrc,
final JsonSerializationContext context) {

final JsonObject resultJson = new JsonObject();

for (final String key : bundleMap.keySet()) {
try {
createFromBundleKey(resultJson, key, bundleMap.get(key));
} catch (final IOException e) {
LOG.error("Bundle Map Serialization Exception: ", e);
}
}

return resultJson;
}

/**
* @param resultJson JSON that will be returned for a filter
* @param key from the resource Bundle
* @param value from the resource Bundle
* @return JsonObject JSON that will be returned for a key
* @throws IOException thrown by createFromBundleKey
*/
private static JsonObject createFromBundleKey(final JsonObject resultJson, final String key, final String value)
throws IOException {

if (!key.contains(I18nConstants.DOT)) {
resultJson.addProperty(key, value);

return resultJson;
}

final String currentKey = firstKey(key);

if (currentKey != null) {
final String subRightKey = key.substring(currentKey.length() + 1, key.length());
final JsonObject childJson = getJsonIfExists(resultJson, currentKey);
resultJson.add(currentKey, createFromBundleKey(childJson, subRightKey, value));
}

return resultJson;
}

private static String firstKey(final String fullKey) {

final String[] splittedKey = fullKey.split("\\.");

return (splittedKey.length != 0) ? splittedKey[0] : fullKey;
}

private static JsonObject getJsonIfExists(final JsonObject parent, final String key) {

if (parent == null) {
LOG.warn("Parent json parameter is null!");
return null;
}

if (parent.get(key) != null && !(parent.get(key) instanceof JsonObject)) {
throw new IllegalArgumentException("Invalid key \'" + key + "\' for parent: " + parent
+ "\nKey can not be JSON object and property or array at a time");
}

if (parent.getAsJsonObject(key) != null) {
return parent.getAsJsonObject(key);
} else {
return new JsonObject();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.devonfw.module.i18n.common.util;

/**
* Constants for i18n module
*
*/
public class I18nConstants {

/**
* Constant for the character DOT
*/
public static final String DOT = ".";

/**
* Constant for the character 0
*/
public static final int ZERO = 0;

/**
* Constant for the Error message in case of Invalid Locale
*/
public static final String INVALID_LOCALE = "Invalid Locale . Please provide valid Locale";

/**
* Constants for Resource path for NLS bundle
*
* The files needed to be store in following package, and file name will be starting with NlsBundleI18n
*
* sample - com\devonfw\module\i18n\common\api\nls\NlsBundleI18n_en.properties
*/
public static final String NLS_BUNDLE_INTF_NAME = "com/devonfw/module/i18n/common/api/nls/NlsBundleI18n";

/**
* Empty String
*/
public static final String EMPTY_STRING = "";

/**
*
*/
// public static final String LOCALE_FILES_LOCATION = "src/main/resources/locale/";

/**
* Constants for resource for other locale integration using gson option
*
* The files needed to be store in following package, and file name will be starting with messages
*
* Sample file - locale\messages_en_US.properties
*/
public static final String RESOURCE_BASENAME = "locale/messages";

/**
* Closing bracket
*/
public static final String CLOSING_BRACE = "}";

/**
* Opening bracket
*/
public static final String OPENING_BRACE = "{";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO such stuff does not belong here:

  1. it is an implementation pointlessly exposed to the public
  2. it has nothing to do with I18n but is due to the fact that we decided to use JSON serialization here.


/**
* Underscore
*/
public static final String UNDER_SCORE = "_";

/**
* Property file extension
*/
public static final String PROPERTIES = "properties";

/**
* English locale
*
*/
public static final String en = "en";

/**
* Constants for Resource path for NLS bundle - qualified name.
*
* The files needed to be store in following package, and file name will be starting with NlsBundleI18n
*
* sample - com\devonfw\module\i18n\common\api\nls\NlsBundleI18n_en.properties
*/
public static final String NLS_BUNDLE_INTF_QUAL_NAME = "com.devonfw.module.i18n.common.api.nls.NlsBundleI18n";
}
Loading