Skip to content

Commit

Permalink
Language and resource APIs have been moved to the "common" module
Browse files Browse the repository at this point in the history
  • Loading branch information
p0loskun committed Sep 20, 2024
1 parent 13f4337 commit e8f01e3
Show file tree
Hide file tree
Showing 91 changed files with 320 additions and 286 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.minersstudios.whomine.api.locale;

import com.google.common.base.Joiner;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.*;

import javax.annotation.concurrent.Immutable;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;

/**
* Represents a language file containing translations for a specific locale
*/
@SuppressWarnings("unused")
@Immutable
public class LanguageFile {

private final Locale locale;
private final Map<String, String> translationMap;

/**
* Constructs a new language file with the given locale
*
* @param locale The locale of the language file
*/
public LanguageFile(final @NotNull Locale locale) {
this.locale = locale;
this.translationMap = new Object2ObjectOpenHashMap<>();
}

/**
* Returns the locale of this language file
*
* @return The locale of this language file
*/
public @NotNull Locale getLocale() {
return this.locale;
}

/**
* Returns the unmodifiable map of translations in this language file
*
* @return The unmodifiable map of translations in this language file
*/
public @NotNull @Unmodifiable Map<String, String> getTranslationMap() {
return Collections.unmodifiableMap(this.translationMap);
}

/**
* Gets the translation for the given path
*
* @param path The path to get the translation for
* @return The translation for the given path, or null if it doesn't exist
*/
public @Nullable String get(final @NotNull String path) {
return this.getOrDefault(path, null);
}

/**
* Gets the translation for the given path, or the fallback if it doesn't
* exist
*
* @param path The path to get the translation for
* @param fallback The fallback to return if the translation doesn't exist
* @return The translation for the given path, or the fallback if it doesn't
* exist
*/
public @UnknownNullability String getOrDefault(
final @NotNull String path,
final @Nullable String fallback
) {
return this.translationMap.getOrDefault(path, fallback);
}

/**
* Returns the number of translations in this language file
*
* @return The number of translations in this language file
*/
public int size() {
return this.translationMap.size();
}

/**
* Returns a hash code based on the locale and translations
*
* @return A hash code based on the locale and translations
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;

result = prime * result + this.locale.hashCode();
result = prime * result + this.translationMap.hashCode();

return result;
}

/**
* Compares the specified object with this language file for equality
*
* @param obj The object to compare
* @return True if the object is a language file and has the same locale and
* translations
*/
@Contract("null -> false")
@Override
public boolean equals(final @Nullable Object obj) {
return this == obj
|| (
obj instanceof LanguageFile that
&& this.getLocale().equals(that.getLocale())
&& this.getTranslationMap().equals(that.getTranslationMap())
);
}

/**
* Checks whether the given path exists in this language file
*
* @param path The path to check for
* @return Whether the given path exists in this language file
*/
public boolean containsPath(final @NotNull String path) {
return this.translationMap.containsKey(path);
}

/**
* Returns whether this language file contains the given translation
*
* @param translation The translation to check for
* @return True if this language file contains the given translation
*/
public boolean containsTranslation(final @NotNull String translation) {
return this.translationMap.containsValue(translation);
}

/**
* Returns whether this language file contains no translations
*
* @return True if this language file contains no translations
*/
public boolean isEmpty() {
return this.translationMap.isEmpty();
}

/**
* Returns a string representation of this language file
*
* @return A string representation of this language file containing the
* translations
*/
@Override
public @NotNull String toString() {
return this.getClass().getSimpleName() +
"{locale=" + this.locale +
", translations=[" +
Joiner.on(", ")
.withKeyValueSeparator("=")
.join(this.translationMap) +
"]}";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minersstudios.whomine.locale;
package com.minersstudios.whomine.api.locale;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
Expand All @@ -12,12 +12,14 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import static com.minersstudios.whomine.locale.TranslationRegistry.registry;
import static com.minersstudios.whomine.api.locale.TranslationRegistry.registry;

/**
* Represents a translation
*/
@SuppressWarnings("unused")
public final class Translation {

private final String path;
private final MessageFormat fallback;
private final Map<Locale, MessageFormat> map;
Expand Down Expand Up @@ -251,7 +253,7 @@ public boolean equals(final @Nullable Object obj) {
builder.setLength(builder.length() - 2);
}

return "Translation{" +
return this.getClass().getSimpleName() + '{' +
"path=" + this.path +
", fallback=" + this.fallback +
", translations=[" + builder + ']' +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minersstudios.whomine.locale;
package com.minersstudios.whomine.api.locale;

import com.minersstudios.whomine.api.utility.SharedConstants;
import net.kyori.adventure.key.Key;
Expand All @@ -15,6 +15,7 @@
import java.util.Locale;
import java.util.Map;

@SuppressWarnings("unused")
public interface TranslationRegistry extends Translator {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minersstudios.whomine.locale;
package com.minersstudios.whomine.api.locale;

import com.minersstudios.whomine.api.utility.ChatUtils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
Expand All @@ -16,13 +16,13 @@
import java.util.concurrent.ConcurrentHashMap;

final class TranslationRegistryImpl implements TranslationRegistry {
static volatile TranslationRegistryImpl registry;

private final Key name;
private final Locale defaultLocale;
private final Map<String, Translation> translationMap;
private final TranslatableComponentRenderer<Locale> renderer;

static volatile TranslationRegistryImpl registry;

TranslationRegistryImpl(
final @NotNull Key name,
final @NotNull Locale defaultLocale
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.minersstudios.whomine.locale;
package com.minersstudios.whomine.api.locale;

import com.minersstudios.whomine.api.utility.SharedConstants;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.minersstudios.whomine.locale.TranslationRegistry.registry;
import static com.minersstudios.whomine.api.locale.TranslationRegistry.registry;
import static com.minersstudios.whomine.api.utility.Font.Chars.*;

/**
Expand Down Expand Up @@ -372,13 +372,13 @@ private Translations() throws AssertionError {
}

private static @NotNull Translation register(
final @NotNull String key,
final @NotNull String path,
final @Nullable String translation
) {
return translation == null
? registry().registerPath(key)
? registry().registerPath(path)
: registry().register(
key,
path,
SharedConstants.DEFAULT_LOCALE,
translation
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.minersstudios.whomine.locale.resource;
package com.minersstudios.whomine.api.locale.resource;

import com.minersstudios.whomine.resource.file.AbstractFileResourceManager;
import com.minersstudios.whomine.api.resource.file.AbstractFileResourceManager;
import org.jetbrains.annotations.NotNull;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.minersstudios.whomine.locale.resource;
package com.minersstudios.whomine.api.locale.resource;

import com.minersstudios.whomine.resource.github.AbstractGithubResourceManager;
import com.minersstudios.whomine.api.resource.github.AbstractGithubResourceManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.net.URI;

public final class GitHubTranslationResourceManager extends AbstractGithubResourceManager implements TranslationResourceManager {
private final String folderPath;

private static final String DOWNLOAD_TRANSLATION_URL = "https://raw.githubusercontent.com/%s/%s/%s/%s";

private final String folderPath;

GitHubTranslationResourceManager(
final @NotNull File file,
final @NotNull String user,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.minersstudios.whomine.locale.resource;
package com.minersstudios.whomine.api.locale.resource;

import com.minersstudios.whomine.resource.ResourceManager;
import com.minersstudios.whomine.api.resource.ResourceManager;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.minersstudios.whomine.locale.resource;
package com.minersstudios.whomine.api.locale.resource;

import com.minersstudios.whomine.resource.uri.AbstractURIResourceManager;
import com.minersstudios.whomine.api.resource.uri.AbstractURIResourceManager;
import org.jetbrains.annotations.NotNull;

import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.minersstudios.whomine.resource;
package com.minersstudios.whomine.api.resource;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;

@SuppressWarnings("unused")
public interface ResourceManager {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minersstudios.whomine.resource.file;
package com.minersstudios.whomine.api.resource.file;

import org.jetbrains.annotations.NotNull;

Expand All @@ -7,6 +7,7 @@
import java.io.IOException;

public abstract class AbstractFileResourceManager implements FileResourceManager {

private final File file;

protected AbstractFileResourceManager(final @NotNull File file) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.minersstudios.whomine.resource.file;
package com.minersstudios.whomine.api.resource.file;

import com.minersstudios.whomine.resource.ResourceManager;
import com.minersstudios.whomine.api.resource.ResourceManager;
import org.jetbrains.annotations.NotNull;

import java.io.File;

@SuppressWarnings("unused")
public interface FileResourceManager extends ResourceManager {

/**
Expand Down
Loading

0 comments on commit e8f01e3

Please sign in to comment.