-
Notifications
You must be signed in to change notification settings - Fork 88
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
vapadwal
wants to merge
8
commits into
devonfw:master
Choose a base branch
from
vapadwal:internationalization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
05346f4
added internationlization module
vapadwal 0f06485
Merge branch 'develop' into internationalization
vapadwal 646f592
resolved conflicts
vapadwal 965bb50
resolving conflicts
vapadwal d976468
resolved conflicts
vapadwal f7579e5
build error
vapadwal 5f5d507
Merge remote-tracking branch 'upstream/develop' into internationaliza…
vapadwal c4caa24
updated tests
vapadwal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
35 changes: 35 additions & 0 deletions
35
...8n/src/main/java/com/devonfw/module/i18n/common/api/exception/UnknownLocaleException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
modules/i18n/src/main/java/com/devonfw/module/i18n/common/api/nls/NlsBundleI18nRoot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
95 changes: 95 additions & 0 deletions
95
modules/i18n/src/main/java/com/devonfw/module/i18n/common/util/BundleMapSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
modules/i18n/src/main/java/com/devonfw/module/i18n/common/util/I18nConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "{"; | ||
|
||
/** | ||
* 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"; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: