Skip to content

Instantly share code, notes, and snippets.

@YanisBft
Created February 20, 2020 00:13
Show Gist options
  • Save YanisBft/717150c7338cc7a5d8837c28d4ab888c to your computer and use it in GitHub Desktop.
Save YanisBft/717150c7338cc7a5d8837c28d4ab888c to your computer and use it in GitHub Desktop.
public static ArrayList<String> getAvailableLanguages() {
ArrayList<String> list = new ArrayList<String>();
File dir;
try {
dir = new File(Paths.get(I18n.class.getProtectionDomain().getCodeSource().getLocation().toURI()).resolve("lang").toString());
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir.toPath(), "*.json")) {
for (Path file : stream) {
String fileName = file.getFileName().toString().substring(0, file.getFileName().toString().length() - 5);
list.add(fileName);
try (InputStream inputStream = Files.newInputStream(new File(Paths.get(I18n.class.getProtectionDomain().getCodeSource().getLocation().toURI()).resolve("lang/" + file.getFileName()).toString()).toPath())) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
Map<?, ?> map = GSON.fromJson(reader, Map.class);
languageNames.put(fileName, map.get("language").toString());
}
}
}
}
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment