Skip to content

Commit

Permalink
AbstractLocalization, WatchLoggingLocalization & it's tests, some bug…
Browse files Browse the repository at this point in the history
…s fixing
  • Loading branch information
Gmugra committed Dec 11, 2017
1 parent 7e7b91d commit 6d3ab6a
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 85 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.cactusthorn</groupId>
<artifactId>localization</artifactId>
<version>0.6</version>
<version>0.7</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package net.cactusthorn.localization;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import net.cactusthorn.localization.core.LocalizationKeys;

public abstract class AbstractLocalization implements Localization {

protected final Map<Locale, LocalizationKeys> translations;
protected final String systemId;
protected final Path l10nDirectory;
protected final Charset charset;

public AbstractLocalization(Map<Locale, LocalizationKeys> translations, String systemId, Path l10nDirectory, Charset charset) {
this.translations = translations;
this.systemId = systemId;
this.l10nDirectory = l10nDirectory;
this.charset = charset;
}

public Locale findNearest(Locale locale) {

if (translations.containsKey(locale) ) return locale;

if (!"".equals(locale.getVariant() ) ) {

Optional<Locale> found =
translations.keySet().stream()
.filter(l -> l.getLanguage().equals(locale.getLanguage() ) && l.getCountry().equals(locale.getLanguage() ) )
.findAny();

if (found.isPresent() ) return found.get();
}

Optional<Locale> found =
translations.keySet().stream()
.filter(l -> l.getLanguage().equals(locale.getLanguage() ) )
.findAny();

if (found.isPresent() ) return found.get();

return null;
}

@Override
public String get(Locale locale, String key) {
return get(locale, key, true, (Map<String, ?>)null);
}

@Override
public String get(Locale locale, String key, Parameter<?>... parameters) {
return get(locale, key, true, Parameter.asMap(parameters));
}

@Override
public String get(Locale locale, String key, boolean withFormatting, Parameter<?>... parameters ) {
return get(locale, key, withFormatting, Parameter.asMap(parameters));
}

@Override
public String get(Locale locale, String key, Map<String, ?> parameters) {
return get(locale, key, true, parameters);
}
}
56 changes: 5 additions & 51 deletions src/main/java/net/cactusthorn/localization/BasicLocalization.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,16 @@
package net.cactusthorn.localization;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import net.cactusthorn.localization.core.LocalizationKeys;

public class BasicLocalization implements Localization{
public class BasicLocalization extends AbstractLocalization {

private Map<Locale, LocalizationKeys> translations;

public BasicLocalization(Map<Locale, LocalizationKeys> translations) {
this.translations = translations;
}

@Override
public Locale findNearest(Locale locale) {

if (translations.containsKey(locale) ) return locale;

if (!"".equals(locale.getVariant() ) ) {

Optional<Locale> found =
translations.keySet().stream()
.filter(l -> l.getLanguage().equals(locale.getLanguage() ) && l.getCountry().equals(locale.getLanguage() ) )
.findAny();

if (found.isPresent() ) return found.get();
}

Optional<Locale> found =
translations.keySet().stream()
.filter(l -> l.getLanguage().equals(locale.getLanguage() ) )
.findAny();

if (found.isPresent() ) return found.get();

return null;
}

@Override
public String get(Locale locale, String key) {
return get(locale, key, true, (Map<String, ?>)null);
}

@Override
public String get(Locale locale, String key, Parameter<?>... parameters) {
return get(locale, key, true, Parameter.asMap(parameters));
}

@Override
public String get(Locale locale, String key, boolean withFormatting, Parameter<?>... parameters ) {
return get(locale, key, withFormatting, Parameter.asMap(parameters));
}

@Override
public String get(Locale locale, String key, Map<String, ?> parameters) {
return get(locale, key, true, parameters);
public BasicLocalization(Map<Locale, LocalizationKeys> translations, String systemId, Path l10nDirectory, Charset charset) {
super(translations, systemId, l10nDirectory, charset);
}

@Override
Expand Down
58 changes: 33 additions & 25 deletions src/main/java/net/cactusthorn/localization/LocalizationLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Files;
Expand All @@ -22,7 +23,7 @@

public class LocalizationLoader {

private Class<? extends Localization> localizationClass = BasicLocalization.class;
private Class<? extends AbstractLocalization> localizationClass = BasicLocalization.class;

private Path l10nDirectory;

Expand All @@ -44,48 +45,55 @@ public LocalizationLoader setCharset(Charset charset) {
return this;
}

public LocalizationLoader setClass(Class<? extends Localization> localizationClass) {
public LocalizationLoader setClass(Class<? extends AbstractLocalization> localizationClass) {
this.localizationClass = localizationClass;
return this;
}

public Localization load() throws IOException {


Map<Locale, LocalizationKeys> localizationKeys = loadAsMap();

try {
Constructor<? extends Localization> constructor = localizationClass.getConstructor(Map.class);

Map<Locale, LocalizationKeys> defaults = loadMap(true);
Map<Locale, LocalizationKeys> locales = loadMap(false);

locales.entrySet().forEach(e -> { if (defaults.containsKey(e.getKey())) { defaults.get(e.getKey()).combineWith(e.getValue()); } } );
locales.entrySet().forEach(e -> defaults.putIfAbsent(e.getKey(), e.getValue() ) );

return constructor.newInstance(defaults );
} catch (RuntimeException | IOException e) {
throw e;
} catch (Exception e) {
Constructor<? extends Localization> constructor = localizationClass.getConstructor(Map.class, String.class, Path.class, Charset.class);
return constructor.newInstance(localizationKeys, systemId, l10nDirectory, charset );
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

public static final String DEFAULT_FILE_PREFIX = "default.";

protected Map<Locale, LocalizationKeys> loadMap(boolean defaults) throws IOException, URISyntaxException {
public Map<Locale, LocalizationKeys> loadAsMap() throws IOException {

Path path = l10nDirectory;
if (path == null ) {
path = Paths.get(LocalizationLoader.class.getClassLoader().getResource("L10n").toURI());
if (l10nDirectory == null ) {
try {
l10nDirectory = Paths.get(LocalizationLoader.class.getClassLoader().getResource("L10n").toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

Map<Locale, LocalizationKeys> defaults = loadMap(true);
Map<Locale, LocalizationKeys> locales = loadMap(false);

locales.entrySet().forEach(e -> { if (defaults.containsKey(e.getKey())) { defaults.get(e.getKey()).combineWith(e.getValue()); } } );
locales.entrySet().forEach(e -> defaults.putIfAbsent(e.getKey(), e.getValue() ) );

return defaults;
}

public static final String DEFAULT_FILE_PREFIX = "default.";

protected Map<Locale, LocalizationKeys> loadMap(boolean defaults) throws IOException {

if (!Files.isDirectory(path) ) {
if (!Files.isDirectory(l10nDirectory ) ) {
throw new IOException("l10nDirectory path " + l10nDirectory + " is not directory");
}

File[] files;
if (defaults) {
files = path.toFile().listFiles(f -> f.getName().endsWith(".properties") && f.getName().startsWith(DEFAULT_FILE_PREFIX) );
files = l10nDirectory.toFile().listFiles(f -> !f.isDirectory() && f.getName().endsWith(".properties") && f.getName().startsWith(DEFAULT_FILE_PREFIX) );
} else {
files = path.toFile().listFiles(f -> f.getName().endsWith(".properties") && !f.getName().startsWith(DEFAULT_FILE_PREFIX) );
files = l10nDirectory.toFile().listFiles(f -> !f.isDirectory() && f.getName().endsWith(".properties") && !f.getName().startsWith(DEFAULT_FILE_PREFIX) );
}

Map<Locale, LocalizationKeys> trs = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.cactusthorn.localization;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -12,8 +14,8 @@
@Slf4j
public class LoggingLocalization extends BasicLocalization {

public LoggingLocalization(Map<Locale, LocalizationKeys> translations) {
super(translations);
public LoggingLocalization(Map<Locale, LocalizationKeys> translations, String systemId, Path l10nDirectory, Charset charset) {
super(translations, systemId, l10nDirectory, charset);
}

@Override
Expand Down
Loading

0 comments on commit 6d3ab6a

Please sign in to comment.