0

I have multiple language files in one directory inside my project. Those language files are basically custom config-files, which I want to save as well inside a sub-folder in my plugin-folder.

I have edited my pom.xml to implement the .yml files into the compressed .jar

<resource>
    <directory>${basedir}/src/main/resources/languages</directory>
    <includes>
        <include>*.yml</include>
    </includes>
</resource>

This is working so far, but when I save the config-files,

File file = new File(Main.getPlugin().getDataFolder(),
                ConfigManager.getConf().getString("Settings.language") + "_lang.yml");
if (!file.exists()) {
   System.out.println("This language file does not exist!");
   file.getParentFile().mkdirs();
}
Main.getPlugin().saveResource(file.getName(), true);

conf = YamlConfiguration.loadConfiguration(file);

they won't be saved inside a subfolder, when the plugin loads.

I have tried to tell the File() function, that it should use a different location such as

File file = new File(Main.getPlugin().getDataFolder() + System.getProperty("file.separator") + "languages", 
ConfigManager.getConf().getString("Settings.language") + "_lang.yml");

but it didn't work.

I could use the current way, that the language-files won't get saved inside a sub-folder, but with the time and more lang-files, it'll get very confusing, if there are too many files.

If this method I'm using is "a stupid way", I'm fine, if somebody else could tell me a way how to write a better "language-switcher".

Dharman
  • 30,962
  • 25
  • 85
  • 135
LinuxSquare
  • 27
  • 1
  • 8
  • From what I know it depends on what else (which frameworks) you are already using. https://www.baeldung.com/spring-boot-internationalization explains how to e.g. use a `LocaleResolver` along with using `MessageSources`. Basically one just names properties-files in a certain manner and injects a `MessageSource` and that's it. You may customize it. All this is valid if you intend to use Spring-Boot. – Igor Dec 25 '19 at 16:46

1 Answers1

0

Okay, I found just found the solution by myself.

I had to add an additional maven-plugin to copy the language files into the language-directory according to this stackoverflow post

At the end, I will show you the changes I have made.

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/classes/languages</outputDirectory>
                <resources>
                    <resource>
                        <directory>${basedir}/src/main/resources/languages</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
LinuxSquare
  • 27
  • 1
  • 8