GenerateLocaleConfig.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Run: java tools/GenerateLocaleConfig.java
  2. import java.io.*;
  3. import java.nio.charset.StandardCharsets;
  4. import java.util.ArrayList;
  5. import java.util.stream.*;
  6. public class GenerateLocaleConfig{
  7. public static void main(String[] args) throws IOException{
  8. File dir=new File("../mastodon/src/main/res/");
  9. if(!dir.exists())
  10. dir=new File("mastodon/src/main/res");
  11. if(!dir.exists())
  12. throw new RuntimeException("Please run from project directory (can't find mastodon/src/main/res)");
  13. ArrayList<String> locales=new ArrayList<>(), rawLocales=new ArrayList<>();
  14. locales.add("en");
  15. for(File file:dir.listFiles()){
  16. String name=file.getName();
  17. if(file.isDirectory() && name.startsWith("values-")){
  18. if(new File(file, "strings.xml").exists()){
  19. locales.add(name.substring(name.indexOf('-')+1).replace("-r", "-"));
  20. rawLocales.add(name.substring(name.indexOf('-')+1));
  21. }
  22. }
  23. }
  24. locales.sort(String::compareTo);
  25. rawLocales.sort(String::compareTo);
  26. try(OutputStreamWriter writer=new OutputStreamWriter(new FileOutputStream(new File(dir, "xml/locales_config.xml")), StandardCharsets.UTF_8)){
  27. writer.write("""
  28. <?xml version="1.0" encoding="utf-8"?>
  29. <locale-config xmlns:android="http://schemas.android.com/apk/res/android">
  30. """);
  31. for(String locale : locales){
  32. writer.write("\t<locale android:name=\"");
  33. writer.write(locale);
  34. writer.write("\"/>\n");
  35. }
  36. writer.write("</locale-config>");
  37. }
  38. File buildGradle=new File(dir, "../../../build.gradle");
  39. ArrayList<String> buildGradleLines=new ArrayList<>();
  40. try(BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(buildGradle)))){
  41. String line;
  42. while((line=reader.readLine())!=null){
  43. if(line.trim().startsWith("resConfigs")){
  44. line=line.substring(0, line.indexOf('r'))+"resConfigs ";
  45. line+=rawLocales.stream().map(l->'"'+l+'"').collect(Collectors.joining(", "));
  46. }
  47. buildGradleLines.add(line);
  48. }
  49. }
  50. try(OutputStreamWriter writer=new OutputStreamWriter(new FileOutputStream(buildGradle))){
  51. for(String line:buildGradleLines){
  52. writer.write(line);
  53. writer.write('\n');
  54. }
  55. }
  56. }
  57. }