-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AbstractLocalization, WatchLoggingLocalization & it's tests, some bug…
…s fixing
- Loading branch information
Showing
10 changed files
with
393 additions
and
85 deletions.
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
68 changes: 68 additions & 0 deletions
68
src/main/java/net/cactusthorn/localization/AbstractLocalization.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,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
56
src/main/java/net/cactusthorn/localization/BasicLocalization.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
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
Oops, something went wrong.