moshidon/tools/GenerateLocaleConfig.java

63 lines
2.1 KiB
Java
Raw Permalink Normal View History

// Run: java tools/GenerateLocaleConfig.java
2022-12-15 17:41:38 +01:00
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
2022-12-15 17:41:38 +01:00
import java.util.stream.*;
public class GenerateLocaleConfig{
public static void main(String[] args) throws IOException{
File dir=new File("../mastodon/src/main/res/");
if(!dir.exists())
dir=new File("mastodon/src/main/res");
if(!dir.exists())
throw new RuntimeException("Please run from project directory (can't find mastodon/src/main/res)");
2022-12-15 17:41:38 +01:00
ArrayList<String> locales=new ArrayList<>(), rawLocales=new ArrayList<>();
locales.add("en");
for(File file:dir.listFiles()){
String name=file.getName();
if(file.isDirectory() && name.startsWith("values-")){
if(new File(file, "strings.xml").exists()){
locales.add(name.substring(name.indexOf('-')+1).replace("-r", "-"));
2022-12-15 17:41:38 +01:00
rawLocales.add(name.substring(name.indexOf('-')+1));
}
}
}
locales.sort(String::compareTo);
2022-12-15 17:41:38 +01:00
rawLocales.sort(String::compareTo);
try(OutputStreamWriter writer=new OutputStreamWriter(new FileOutputStream(new File(dir, "xml/locales_config.xml")), StandardCharsets.UTF_8)){
writer.write("""
<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
""");
for(String locale : locales){
writer.write("\t<locale android:name=\"");
writer.write(locale);
writer.write("\"/>\n");
}
writer.write("</locale-config>");
}
2022-12-15 17:41:38 +01:00
File buildGradle=new File(dir, "../../../build.gradle");
ArrayList<String> buildGradleLines=new ArrayList<>();
try(BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(buildGradle)))){
String line;
while((line=reader.readLine())!=null){
if(line.trim().startsWith("resConfigs")){
line=line.substring(0, line.indexOf('r'))+"resConfigs ";
line+=rawLocales.stream().map(l->'"'+l+'"').collect(Collectors.joining(", "));
}
buildGradleLines.add(line);
}
}
try(OutputStreamWriter writer=new OutputStreamWriter(new FileOutputStream(buildGradle))){
for(String line:buildGradleLines){
writer.write(line);
writer.write('\n');
}
}
}
}