Skip to content

Commit 68d9114

Browse files
Merge pull request #24 from aquality-automation/Feature/8-Localization-Services
Feature #8: Localization services
2 parents f3b0786 + d465d0c commit 68d9114

File tree

12 files changed

+340
-3
lines changed

12 files changed

+340
-3
lines changed

src/main/java/aquality/selenium/core/application/AqualityModule.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package aquality.selenium.core.application;
22

3+
import aquality.selenium.core.localization.*;
34
import aquality.selenium.core.logging.Logger;
45
import aquality.selenium.core.utilities.ISettingsFile;
56
import aquality.selenium.core.utilities.JsonSettingsFile;
@@ -11,7 +12,8 @@
1112
/**
1213
* Describes all dependencies which is registered for the project.
1314
*/
14-
public class AqualityModule<T extends IApplication> extends AbstractModule {
15+
public class AqualityModule<T extends IApplication> extends AbstractModule
16+
implements ILocalizationModule {
1517

1618
private final Provider<T> applicationProvider;
1719

@@ -31,6 +33,8 @@ protected void configure() {
3133
bind(ITimeoutConfiguration.class).to(TimeoutConfiguration.class).in(Singleton.class);
3234
bind(IRetryConfiguration.class).to(RetryConfiguration.class).in(Singleton.class);
3335
bind(IElementCacheConfiguration.class).to(ElementCacheConfiguration.class).in(Singleton.class);
36+
bind(ILocalizationManager.class).to(getLocalizationManagerImplementation()).in(Singleton.class);
37+
bind(ILocalizedLogger.class).to(getLocalizedLoggerImplementation()).in(Singleton.class);
3438
}
3539

3640
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package aquality.selenium.core.localization;
2+
3+
/**
4+
* This interface is used for translation messages to different languages.
5+
*/
6+
public interface ILocalizationManager {
7+
/**
8+
* Gets localized message from resources by its key.
9+
* @param messageKey Key in resource file.
10+
* @param args Arguments, which will be provided to template of localized message.
11+
* @return Localized message.
12+
*/
13+
String getLocalizedMessage(String messageKey, Object... args);
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package aquality.selenium.core.localization;
2+
3+
/**
4+
* Describes implementations of localization services to be registered in DI container.
5+
*/
6+
public interface ILocalizationModule {
7+
/**
8+
* @return class which implements ILocalizationManager
9+
*/
10+
default Class<? extends ILocalizationManager> getLocalizationManagerImplementation() {
11+
return LocalizationManager.class;
12+
}
13+
14+
/**
15+
* @return class which implements ILocalizedLogger
16+
*/
17+
default Class<? extends ILocalizedLogger> getLocalizedLoggerImplementation() {
18+
return LocalizedLogger.class;
19+
}
20+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package aquality.selenium.core.localization;
2+
3+
/**
4+
* Log messages in current language.
5+
*/
6+
public interface ILocalizedLogger {
7+
/**
8+
* Logs localized message for action with INFO level which is applied for element, for example, click, send keys etc.
9+
* @param elementType Type of the element.
10+
* @param elementName Name of the element.
11+
* @param messageKey Key in resource file.
12+
* @param args Arguments, which will be provided to template of localized message.
13+
*/
14+
void infoElementAction(String elementType, String elementName, String messageKey, Object... args);
15+
16+
/**
17+
* Logs localized message with INFO level.
18+
* @param messageKey Key in resource file.
19+
* @param args Arguments, which will be provided to template of localized message.
20+
*/
21+
void info(String messageKey, Object... args);
22+
23+
/**
24+
* Logs localized message with DEBUG level.
25+
* @param messageKey Key in resource file.
26+
* @param args Arguments, which will be provided to template of localized message.
27+
*/
28+
void debug(String messageKey, Object... args);
29+
30+
/**
31+
* Logs localized message with DEBUG level.
32+
* @param messageKey Key in resource file.
33+
* @param throwable Throwable to log.
34+
* @param args Arguments, which will be provided to template of localized message.
35+
*/
36+
void debug(String messageKey, Throwable throwable, Object... args);
37+
38+
/**
39+
* Logs localized message with WARN level.
40+
* @param messageKey Key in resource file.
41+
* @param args Arguments, which will be provided to template of localized message.
42+
*/
43+
void warn(String messageKey, Object... args);
44+
45+
/**
46+
* Logs localized message with ERROR level.
47+
* @param messageKey Key in resource file.
48+
* @param args Arguments, which will be provided to template of localized message.
49+
*/
50+
void error(String messageKey, Object... args);
51+
52+
/**
53+
* Logs localized message with FATAL level.
54+
* @param messageKey Key in resource file.
55+
* @param throwable Throwable to log.
56+
* @param args Arguments, which will be provided to template of localized message.
57+
*/
58+
void fatal(String messageKey, Throwable throwable, Object... args);
59+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package aquality.selenium.core.localization;
2+
3+
import aquality.selenium.core.configurations.ILoggerConfiguration;
4+
import aquality.selenium.core.logging.Logger;
5+
import aquality.selenium.core.utilities.ISettingsFile;
6+
import aquality.selenium.core.utilities.JsonSettingsFile;
7+
import com.google.inject.Inject;
8+
9+
public class LocalizationManager implements ILocalizationManager {
10+
private static final String LANG_RESOURCE_TEMPLATE = "localization/%1$s.json";
11+
private final ISettingsFile localizationFile;
12+
private final Logger logger;
13+
private final String locResourceName;
14+
15+
@Inject
16+
public LocalizationManager(ILoggerConfiguration loggerConfiguration, Logger logger) {
17+
this.logger = logger;
18+
String language = loggerConfiguration.getLanguage();
19+
locResourceName = String.format(LANG_RESOURCE_TEMPLATE, language.toLowerCase());
20+
localizationFile = new JsonSettingsFile(locResourceName);
21+
}
22+
23+
@Override
24+
public String getLocalizedMessage(String messageKey, Object... args) {
25+
String jsonKeyPath = "/".concat(messageKey);
26+
if (localizationFile.isValuePresent(jsonKeyPath)) {
27+
return String.format(localizationFile.getValue(jsonKeyPath).toString(), args);
28+
}
29+
30+
logger.warn(String.format("Cannot find localized message by key '%1$s' in resource file %2$s",
31+
jsonKeyPath, locResourceName));
32+
return messageKey;
33+
}
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package aquality.selenium.core.localization;
2+
3+
import aquality.selenium.core.logging.Logger;
4+
import com.google.inject.Inject;
5+
6+
public class LocalizedLogger implements ILocalizedLogger {
7+
8+
private final ILocalizationManager localizationManager;
9+
private final Logger logger;
10+
11+
@Inject
12+
public LocalizedLogger(ILocalizationManager localizationManager, Logger logger) {
13+
this.localizationManager = localizationManager;
14+
this.logger = logger;
15+
}
16+
17+
private String localizeMessage(String messageKey, Object... args) {
18+
return localizationManager.getLocalizedMessage(messageKey, args);
19+
}
20+
21+
@Override
22+
public void infoElementAction(String elementType, String elementName, String messageKey, Object... args) {
23+
String message = String.format("%1$s '%2$s' :: %3$s", elementType, elementName, localizeMessage(messageKey, args));
24+
logger.info(message);
25+
}
26+
27+
@Override
28+
public void info(String messageKey, Object... args) {
29+
logger.info(localizeMessage(messageKey, args));
30+
}
31+
32+
@Override
33+
public void debug(String messageKey, Object... args) {
34+
logger.debug(localizeMessage(messageKey, args));
35+
}
36+
37+
@Override
38+
public void debug(String messageKey, Throwable throwable, Object... args) {
39+
logger.debug(localizeMessage(messageKey, args), throwable);
40+
}
41+
42+
@Override
43+
public void warn(String messageKey, Object... args) {
44+
logger.warn(localizeMessage(messageKey, args));
45+
}
46+
47+
@Override
48+
public void error(String messageKey, Object... args) {
49+
logger.error(localizeMessage(messageKey, args));
50+
}
51+
52+
@Override
53+
public void fatal(String messageKey, Throwable throwable, Object... args) {
54+
logger.fatal(localizeMessage(messageKey, args), throwable);
55+
}
56+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"loc.clicking": "Націскаем",
3+
"loc.el.getattr": "Атрымліваем атрыбут '%1$s'",
4+
"loc.get.text": "Атрымліваем тэкст элемента",
5+
"loc.text.sending.keys": "Націскаем клавішы '%1$s'",
6+
"loc.no.elements.found.in.state": "Не знайшлі элементаў па лакатару '%1$s' у %2$s стане",
7+
"loc.no.elements.found.by.locator": "Не знайшлі элементаў па лакатару '%1$s'",
8+
"loc.elements.were.found.but.not.in.state": "Знайшлі элементы па лакатару '%1$s', але яны не ў жаданым стане %2$s",
9+
"loc.elements.found.but.should.not": "Не павінна быць знойдзена элементаў па лакатару '%1$s' у %2$s стане",
10+
"loc.search.of.elements.failed": "Пошук элемента па лакатару '%1$s' прайшоў няўдала"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"loc.clicking": "Clicking",
3+
"loc.el.getattr": "Getting attribute '%1$s'",
4+
"loc.get.text": "Getting text from element",
5+
"loc.text.sending.keys": "Sending keys '%1$s'",
6+
"loc.no.elements.found.in.state": "No elements with locator '%1$s' were found in %2$s state",
7+
"loc.no.elements.found.by.locator": "No elements were found by locator '%1$s'",
8+
"loc.elements.were.found.but.not.in.state": "Elements were found by locator '%1$s' but not in desired state %2$s",
9+
"loc.elements.found.but.should.not": "No elements should be found by locator '%1$s' in %2$s state",
10+
"loc.search.of.elements.failed": "Search of element by locator '%1$s' failed"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"loc.clicking": "Клик",
3+
"loc.el.getattr": "Получение аттрибута '%1$s'",
4+
"loc.get.text": "Получение текста элемента",
5+
"loc.text.sending.keys": "Нажатие клавиши '%1$s'",
6+
"loc.no.elements.found.in.state": "Не удалось найти элементов по локатору '%1$s' в %2$s состоянии",
7+
"loc.no.elements.found.by.locator": "Не удалось найти элементов по локатору '%1$s'",
8+
"loc.elements.were.found.but.not.in.state": "Удалось найти элементы по локатору '%1$s', но они не в желаемом состоянии %2$s",
9+
"loc.elements.found.but.should.not": "Не должно быть найдено элементов по локатору '%1$s' в %2$s состоянии",
10+
"loc.search.of.elements.failed": "Поиск элемента по локатору '%1$s' прошел неудачно"
11+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
log4j.rootLogger=INFO, stdout, file
1+
log4j.rootLogger=DEBUG, stdout, file
22

33
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
44
log4j.appender.stdout.encoding=UTF-8
55
log4j.appender.stdout.target=System.out
66
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
77
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n
8+
log4j.appender.stdout.Threshold=INFO
89

910
log4j.appender.file = org.apache.log4j.RollingFileAppender
1011
log4j.appender.file.File = target/log/log.log
1112
log4j.appender.file.MaxFileSize=10MB
1213
log4j.appender.file.layout=org.apache.log4j.PatternLayout
13-
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n
14+
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n
15+
log4j.appender.file.Threshold=DEBUG

0 commit comments

Comments
 (0)