Skip to content

Commit

Permalink
REFACTOR structures
Browse files Browse the repository at this point in the history
  • Loading branch information
sanguine6660 committed Dec 13, 2023
1 parent 1a5b9c1 commit 61671ba
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.izanagicraft.messages;
package com.izanagicraft.messages.placeholders;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -28,7 +28,7 @@
import java.util.regex.Pattern;

/**
* message-format; com.izanagicraft.messages:StatticMessagePlaceholders
* message-format; com.izanagicraft.messages.placeholders:StaticMessagePlaceholders
* <p>
* Utility class for handling placeholders in messages.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.izanagicraft.messages;
package com.izanagicraft.messages.strings;

/**
* message-format; com.izanagicraft.messages:WrappedString
* message-format; com.izanagicraft.messages.strings:WrappedString
* <p>
* The WrappedString class represents a simple wrapper around a String value.
* It provides methods to create, access, and modify the underlying String.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.izanagicraft.messages;
package com.izanagicraft.messages.translations;

import com.izanagicraft.messages.strings.WrappedString;
import com.izanagicraft.messages.placeholders.StaticMessagePlaceholders;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -28,14 +31,14 @@
import java.util.concurrent.ConcurrentHashMap;

/**
* message-format; com.izanagicraft.messages:Translations
* message-format; com.izanagicraft.messages.translations:GlobalTranslations
* <p>
* Utility class for handling translations with placeholders.
*
* @author <a href="https://github.com/sanguine6660">@sanguine6660</a>
* @since 13.12.2023
*/
public final class Translations {
public final class GlobalTranslations {

/**
* A map that stores translations for different locales.
Expand All @@ -48,7 +51,7 @@ public final class Translations {
private static Properties fallback;

// instantiation prevention
private Translations() {
private GlobalTranslations() {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package tests;


import com.izanagicraft.messages.Translations;
import com.izanagicraft.messages.translations.GlobalTranslations;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -32,15 +32,15 @@
import static org.junit.jupiter.api.Assertions.*;

/**
* message-format; tests:TranslationTest
* message-format; tests:GlobalTranslationsTest
*
* @author <a href="https://github.com/LuciferMorningstarDev">LuciferMorningstarDev</a>
* @since 13.12.2023
*/
public class TranslationsTest {
public class GlobalTranslationsTest {

@BeforeAll
static void initTranslations() {
static void initGlobalTranslations() {
// Load the test language file
File testFile = new File("src/test/resources/lang.properties");

Expand All @@ -49,7 +49,7 @@ static void initTranslations() {

// Initialize translations with the test language file
Map<String, Object> defaultReplacements = createDefaultReplacements();
Translations.init(defaultReplacements, testFile);
GlobalTranslations.init(defaultReplacements, testFile);
}

private static Map<String, Object> createDefaultReplacements() {
Expand All @@ -62,24 +62,24 @@ private static Map<String, Object> createDefaultReplacements() {
@Test
void testInit() {
// Ensure translations are initialized
assertNotNull(Translations.getTranslations());
assertFalse(Translations.getTranslations().isEmpty());
assertNotNull(GlobalTranslations.getTranslations());
assertFalse(GlobalTranslations.getTranslations().isEmpty());

// Ensure fallback is set
assertNotNull(Translations.getFallback());
assertNotNull(GlobalTranslations.getFallback());
}

@Test
void testTranslateGreeting() {
// Test translating the 'greeting' key with a placeholder
String translated = Translations.translate("greeting", "John");
String translated = GlobalTranslations.translate("greeting", "John");
assertEquals("[PREFIX] Hello, John!", translated);
}

@Test
void testTranslateGreetingWithLocale() {
// Test translating the 'greeting' key with a specific locale
String translated = Translations.translate(Locale.US, "greeting", "Alice");
String translated = GlobalTranslations.translate(Locale.US, "greeting", "Alice");
assertEquals("[PREFIX] Hello, Alice!", translated);
}

Expand All @@ -88,7 +88,7 @@ void testTranslateIterator() {
// Test translating the 'iterator' key with a numeric placeholder
for (int i = 0; i < 5; i++) {
int placeholderValue = 42;
String translated = Translations.translate("iterator", placeholderValue);
String translated = GlobalTranslations.translate("iterator", placeholderValue);
String expected = "[PREFIX] Current Iteration Index " + placeholderValue + ".";
assertEquals(expected, translated);
}
Expand All @@ -100,7 +100,7 @@ void testTranslateIteratorWithLocale() {
// Test translating the 'iterator' key with a specific locale and numeric placeholder
for (int i = 0; i < 5; i++) {
int placeholderValue = i;
String translated = Translations.translate(locale, "iterator", placeholderValue);
String translated = GlobalTranslations.translate(locale, "iterator", placeholderValue);
String expected = "[PREFIX] Current Iteration Index " + placeholderValue + ".";
assertEquals(expected, translated);
}
Expand All @@ -109,21 +109,21 @@ void testTranslateIteratorWithLocale() {
@Test
void testTranslateWithMissingKey() {
// Test translating a key that does not exist in the language file
String translated = Translations.translate("nonexistent.key", "arg1", "arg2");
String translated = GlobalTranslations.translate("nonexistent.key", "arg1", "arg2");
assertEquals("nonexistent.key", translated);
}

@Test
void testTranslateWithDefaultReplacements() {
// Test translating with default replacements
String translated = Translations.translate("greeting");
String translated = GlobalTranslations.translate("greeting");
assertEquals("[PREFIX] Hello, null!", translated);
}

@Test
void testTranslateWithInvalidLocale() {
// Test translating with an invalid locale, should fall back to default
String translated = Translations.translate(new Locale("invalid"), "greeting", "Bob");
String translated = GlobalTranslations.translate(new Locale("invalid"), "greeting", "Bob");
assertEquals("[PREFIX] Hello, Bob!", translated);
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/tests/StaticMessagePlaceholdersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package tests;

import com.izanagicraft.messages.StaticMessagePlaceholders;
import com.izanagicraft.messages.placeholders.StaticMessagePlaceholders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down

0 comments on commit 61671ba

Please sign in to comment.