Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Adding a library api:
<dependency>
<groupId>org.densy.polyglot</groupId>
<artifactId>api</artifactId>
<vearsion>1.1.1-SNAPSHOT</vearsion>
<vearsion>1.1.2-SNAPSHOT</vearsion>
</dependency>
```

Expand All @@ -82,7 +82,7 @@ Adding a library implementation:
<dependency>
<groupId>org.densy.polyglot</groupId>
<artifactId>core</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</dependency>
```

Expand All @@ -97,10 +97,10 @@ maven {

Adding a library api:
```groovy
implementation "org.densy.polyglot:api:1.1.1-SNAPSHOT"
implementation "org.densy.polyglot:api:1.1.2-SNAPSHOT"
```

Adding a library implementation:
```groovy
implementation "org.densy.polyglot:core:1.1.1-SNAPSHOT"
implementation "org.densy.polyglot:core:1.1.2-SNAPSHOT"
```
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ java {

allprojects {
group = "org.densy.polyglot"
version = "1.1.1-SNAPSHOT"
version = "1.1.2-SNAPSHOT"
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.densy.polyglot.core.provider;

import org.densy.polyglot.api.language.Language;
import org.densy.polyglot.api.language.LanguageStandard;
import org.densy.polyglot.api.provider.TranslationProvider;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
* Properties file translation provider.
*/
public class PropertiesFileProvider implements TranslationProvider {
public static final Set<String> ALLOWED_EXTENSIONS = Set.of("properties", "props", "ini", "cfg", "conf", "env", "lang", "txt");

private final File folder;
private final LanguageStandard languageStandard;
private final Map<Language, Map<String, String>> translations;
private final Set<String> allowedExtensions;

public PropertiesFileProvider(File folder, LanguageStandard languageStandard) {
this(folder, languageStandard, ALLOWED_EXTENSIONS);
}

public PropertiesFileProvider(File folder, LanguageStandard languageStandard, Set<String> allowedExtensions) {
this.folder = folder;
this.languageStandard = languageStandard;
this.allowedExtensions = new HashSet<>(allowedExtensions);
this.translations = new HashMap<>();
this.loadTranslationsFromFolder();
}

private void loadTranslationsFromFolder() {
if (!folder.exists() || !folder.isDirectory()) {
return;
}

File[] propertiesFiles = folder.listFiles((dir, name) -> {
String lowerName = name.toLowerCase();
int dotIndex = lowerName.lastIndexOf('.');
if (dotIndex == -1) {
return false;
}
String extension = lowerName.substring(dotIndex + 1);
return allowedExtensions.contains(extension);
});

if (propertiesFiles == null) {
return;
}

for (File file : propertiesFiles) {
try {
String fileName = file.getName();
String languageString = fileName.substring(0, fileName.lastIndexOf('.'));

if (languageStandard.matches(languageString)) {
Language language = languageStandard.parseLanguage(languageString);
Properties properties = new Properties();

try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
properties.load(reader);
}

Map<String, String> translationMap = new HashMap<>();
for (String key : properties.stringPropertyNames()) {
translationMap.put(key, properties.getProperty(key));
}

this.translations.put(language, translationMap);
}
} catch (IOException e) {
throw new RuntimeException("Error loading translation file: " + file.getName(), e);
} catch (Exception e) {
throw new RuntimeException("Error parsing translation file: " + file.getName(), e);
}
}
}

@Override
public Map<Language, Map<String, String>> getTranslations() {
return new HashMap<>(translations);
}

@Override
public Set<Language> getSupportedLanguages() {
return new HashSet<>(translations.keySet());
}
}