Skip to content

Commit a7f57d8

Browse files
authored
Merge pull request #9 from DensyDev/feat/properties-file-provider
feat: properties file provider
2 parents 4747290 + 13489ca commit a7f57d8

3 files changed

Lines changed: 102 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Adding a library api:
7373
<dependency>
7474
<groupId>org.densy.polyglot</groupId>
7575
<artifactId>api</artifactId>
76-
<vearsion>1.1.1-SNAPSHOT</vearsion>
76+
<vearsion>1.1.2-SNAPSHOT</vearsion>
7777
</dependency>
7878
```
7979

@@ -82,7 +82,7 @@ Adding a library implementation:
8282
<dependency>
8383
<groupId>org.densy.polyglot</groupId>
8484
<artifactId>core</artifactId>
85-
<version>1.1.1-SNAPSHOT</version>
85+
<version>1.1.2-SNAPSHOT</version>
8686
</dependency>
8787
```
8888

@@ -97,10 +97,10 @@ maven {
9797

9898
Adding a library api:
9999
```groovy
100-
implementation "org.densy.polyglot:api:1.1.1-SNAPSHOT"
100+
implementation "org.densy.polyglot:api:1.1.2-SNAPSHOT"
101101
```
102102

103103
Adding a library implementation:
104104
```groovy
105-
implementation "org.densy.polyglot:core:1.1.1-SNAPSHOT"
105+
implementation "org.densy.polyglot:core:1.1.2-SNAPSHOT"
106106
```

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ java {
1212

1313
allprojects {
1414
group = "org.densy.polyglot"
15-
version = "1.1.1-SNAPSHOT"
15+
version = "1.1.2-SNAPSHOT"
1616
}
1717

1818
subprojects {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.densy.polyglot.core.provider;
2+
3+
import org.densy.polyglot.api.language.Language;
4+
import org.densy.polyglot.api.language.LanguageStandard;
5+
import org.densy.polyglot.api.provider.TranslationProvider;
6+
7+
import java.io.File;
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStreamReader;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.HashMap;
13+
import java.util.HashSet;
14+
import java.util.Map;
15+
import java.util.Properties;
16+
import java.util.Set;
17+
18+
/**
19+
* Properties file translation provider.
20+
*/
21+
public class PropertiesFileProvider implements TranslationProvider {
22+
public static final Set<String> ALLOWED_EXTENSIONS = Set.of("properties", "props", "ini", "cfg", "conf", "env", "lang", "txt");
23+
24+
private final File folder;
25+
private final LanguageStandard languageStandard;
26+
private final Map<Language, Map<String, String>> translations;
27+
private final Set<String> allowedExtensions;
28+
29+
public PropertiesFileProvider(File folder, LanguageStandard languageStandard) {
30+
this(folder, languageStandard, ALLOWED_EXTENSIONS);
31+
}
32+
33+
public PropertiesFileProvider(File folder, LanguageStandard languageStandard, Set<String> allowedExtensions) {
34+
this.folder = folder;
35+
this.languageStandard = languageStandard;
36+
this.allowedExtensions = new HashSet<>(allowedExtensions);
37+
this.translations = new HashMap<>();
38+
this.loadTranslationsFromFolder();
39+
}
40+
41+
private void loadTranslationsFromFolder() {
42+
if (!folder.exists() || !folder.isDirectory()) {
43+
return;
44+
}
45+
46+
File[] propertiesFiles = folder.listFiles((dir, name) -> {
47+
String lowerName = name.toLowerCase();
48+
int dotIndex = lowerName.lastIndexOf('.');
49+
if (dotIndex == -1) {
50+
return false;
51+
}
52+
String extension = lowerName.substring(dotIndex + 1);
53+
return allowedExtensions.contains(extension);
54+
});
55+
56+
if (propertiesFiles == null) {
57+
return;
58+
}
59+
60+
for (File file : propertiesFiles) {
61+
try {
62+
String fileName = file.getName();
63+
String languageString = fileName.substring(0, fileName.lastIndexOf('.'));
64+
65+
if (languageStandard.matches(languageString)) {
66+
Language language = languageStandard.parseLanguage(languageString);
67+
Properties properties = new Properties();
68+
69+
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
70+
properties.load(reader);
71+
}
72+
73+
Map<String, String> translationMap = new HashMap<>();
74+
for (String key : properties.stringPropertyNames()) {
75+
translationMap.put(key, properties.getProperty(key));
76+
}
77+
78+
this.translations.put(language, translationMap);
79+
}
80+
} catch (IOException e) {
81+
throw new RuntimeException("Error loading translation file: " + file.getName(), e);
82+
} catch (Exception e) {
83+
throw new RuntimeException("Error parsing translation file: " + file.getName(), e);
84+
}
85+
}
86+
}
87+
88+
@Override
89+
public Map<Language, Map<String, String>> getTranslations() {
90+
return new HashMap<>(translations);
91+
}
92+
93+
@Override
94+
public Set<Language> getSupportedLanguages() {
95+
return new HashSet<>(translations.keySet());
96+
}
97+
}

0 commit comments

Comments
 (0)