Skip to content

Commit

Permalink
i18n for REST APIs and error messages (#68)
Browse files Browse the repository at this point in the history
* Localized strings in REST controller
* Support for localization in ErrorService
* Add parameters to the ApiMessage and use locale-aware Formatter
* Allow redefining of numbered messages so applications can redefine numbers and text for the SDK commons messages
* Using localized numbered message on the server
* Instructions for other EBCDIC codepages (such as IBM-870) in STDOUT
* Accent stripping logging encoder
  • Loading branch information
plavjanik authored Nov 6, 2019
1 parent 5374365 commit d20946e
Show file tree
Hide file tree
Showing 32 changed files with 878 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
package org.zowe.commons;

import java.text.Normalizer;

import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Layout;
import lombok.Setter;

public class AccentStrippingPatternLayerEncoder extends PatternLayoutEncoder {
private static final String STRIP_ACCENTS_PROPERTY_NAME = "org.zowe.commons.logging.stripAccents";

@Setter
private boolean stripAccents = "true".equalsIgnoreCase(System.getProperty(STRIP_ACCENTS_PROPERTY_NAME));

@Override
public byte[] encode(ILoggingEvent event) {
String txt = layout.doLayout(event);
if (stripAccents) {
return stripAccents(txt).getBytes();
}
else {
return super.encode(event);
}
}

private String stripAccents(String input) {
return input == null ? null
: Normalizer.normalize(input, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}

void overrideLayout(Layout layout) {
this.layout = layout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.zowe.commons.error.ErrorServiceImpl;

public final class CommonsErrorService {
private static ErrorService errorService = new ErrorServiceImpl();
private static ErrorService errorService = ErrorServiceImpl.getCommonsDefault();

private CommonsErrorService() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public ErrorMessage getErrorMessage(String key) {
* @param messages Error message
*/
public void addMessages(ErrorMessages messages) {
Map<String, ErrorMessage> currentKeyMap = new HashMap<>();
for (ErrorMessage message : messages.getMessages()) {
if (!keyMap.containsKey(message.getKey())) {
if (!currentKeyMap.containsKey(message.getKey())) {
if (!numberMap.containsKey(message.getNumber())) {
currentKeyMap.put(message.getKey(), message);
keyMap.put(message.getKey(), message);
numberMap.put(message.getNumber(), message);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.zowe.commons.error;

import java.util.List;
import java.util.Locale;

import org.zowe.commons.rest.response.ApiMessage;

Expand All @@ -19,12 +20,13 @@
*/
public interface ErrorService {
/**
* Create {@link ApiMessage} that contains one
* {@link org.zowe.commons.rest.response.Message} for provided key with array of
* Create {@link ApiMessage} that contains list of
* {@link org.zowe.commons.rest.response.Message} with same key and provided
* parameters.
*
* @param key of message in messages.yml file
* @param parameters for message
* @param key Key of message in messages.yml file.
* @param parameters A list of parameters that will be used for formatting.
*
* @return {@link ApiMessage} for key
*/
ApiMessage createApiMessage(String key, Object... parameters);
Expand All @@ -34,25 +36,63 @@ public interface ErrorService {
* {@link org.zowe.commons.rest.response.Message} with same key and provided
* parameters.
*
* @param key of message in messages.yml file
* @param parameters list that contains arrays of parameters
* @param key Key of message in messages.yml file.
* @param parameters A list that contains arrays of parameters that will be used
* for formatting.
* @return {@link ApiMessage} for key
*/
ApiMessage createApiMessage(String key, List<Object[]> parameters);

/**
* Load messages to the context from the provided message file path
* Create {@link ApiMessage} that contains list of
* {@link org.zowe.commons.rest.response.Message} with same key and provided
* parameters.
*
* @param messagesFilePath path of the message file
* @param locale The locale that is used to retrieve the localized message.
* If it is null or message key is not found then the fallback
* is to use the US English message.
* @param key Key of message in messages.yml file.
* @param parameters A list of parameters that will be used for formatting.
* @return {@link ApiMessage} for key
*/
ApiMessage createApiMessage(Locale locale, String key, Object... parameters);

/**
* Create {@link ApiMessage} that contains list of
* {@link org.zowe.commons.rest.response.Message} with same key and provided
* parameters.
*
* @param locale The locale that is used to retrieve the localized message.
* If it is null or message key is not found then the fallback
* is to use the US English message.
* @param key Key of message in messages.yml file.
* @param parameters A list that contains arrays of parameters that will be used
* for formatting.
* @return {@link ApiMessage} for key
*/
ApiMessage createApiMessage(Locale locale, String key, List<Object[]> parameters);

/**
* Loads messages to the context from the provided message file path.
*
* @param messagesFilePath Path of the message file resource.
*/
void loadMessages(String messagesFilePath);

/**
* Loads localized messages from the provided resource bundle.
*
* @param baseName The base name of the resource bundle, a fully qualified class
* name.
*/
void addResourceBundleBaseName(String baseName);

/**
* Returns the message in the format that can be printed to console as a single
* line or displayed to the user.
*
* @param key Message key to be retrieved
* @param parameters Positional parameters require by the message
* @param key Message key to be retrieved.
* @param parameters Positional parameters require by the message.
* @return Readable text for the given message key.
*/
String getReadableMessage(String key, Object... parameters);
Expand Down
Loading

0 comments on commit d20946e

Please sign in to comment.