Tutorial [1.10] Multiple custom language files

Discussion in 'Resources' started by Kevinzuman22, Jul 26, 2016.

Thread Status:
Not open for further replies.
  1. In this tutorial I will be showing you how you can enable the ability for multiple languages for your plugin, as well as your users to create their own custom language files and define which one to use in the configuration file.

    First of all, credits to gomeow's Language files tutorial I used as a base for this tutorial, which can be found here.

    This tutorial is assuming you have already done everything of gomeow's tutorial.

    First, replace your 'loadLang()' method with this:
    Code:
    public void loadLang(String file) {
        File lang = new File(getDataFolder(), "lang" + File.separator + file);
    
        if (!lang.exists()) {
            try {
                getDataFolder().mkdir();
                lang.createNewFile();
                InputStream defConfigStream = this.getResource("en_US.yml");
                if (defConfigStream != null) {
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    defConfig.save(lang);
                    Lang.setFile(defConfig);
                    return;
                }
            } catch(IOException e) {
                e.printStackTrace(); // So they notice
                log.severe("[ClockSign] Couldn't create language file.");
                log.severe("[ClockSign] This is a fatal error. Now disabling");
                this.setEnabled(false); // Without it loaded, we can't send them messages
            }
        }
    
        YamlConfiguration conf = YamlConfiguration.loadConfiguration(lang);
        for(Lang item:Lang.values()) {
            if (conf.getString(item.getPath()) == null) {
                conf.set(item.getPath(), item.getDefault());
            }
        }
    
        Lang.setFile(conf);
        LANG = conf;
        LANG_FILE = lang;
        try {
            conf.save(getLangFile());
        } catch(IOException e) {
            log.log(Level.WARNING, "ClockSign: Failed to save " + getConfig().getString("settings.language") + ".yml.");
            log.log(Level.WARNING, "ClockSign: Report this stack trace to the plugin developer.");
            e.printStackTrace();
        }
    
    }

    Then, add this to your onEnable().
    Code:
    File lang = new File(getDataFolder()+ File.separator + "lang", getConfig().getString("settings.language") + ".yml");
    File defaultLang = new File(getDataFolder() + File.separator + "lang",  "en_US.yml");
    if(!lang.exists()) {
        log.severe("[PLUGINNAME] Language file '" + getConfig().getString("settings.languageFile") + ".yml' doesn't exist. Attempting to use default 'en_US.yml' language file...");
        if(!defaultLang.exists()) {
            log.severe("[PLUGINNAME] Default language file not found! Plugin cannot be enabled if language file doesn't exist. Please re-download the plugin to restore the language file.");
            this.getServer().getPluginManager().disablePlugin(this);
        } else {
            log.info("[PLUGINNAME] Default language file found. Continuing plugin startup...");
            loadLang("en_US.yml");
            log.info("[PLUGINNAME] Using locale '" + getConfig().getString("settings.language") + "'.");
        }
    } else {
        log.info("[PLUGINNAME] Using locale '" + getConfig().getString("settings.language") + "'.");
        loadLang(getConfig().getString("settings.language") + ".yml");
    }
    Be sure to replace 'PLUGINNAME' with your plugin's name.

    Finally, create a default configuration setting where you can define the to use language file. As default, set it to 'en_US'. When done, replace the setting's name with all the 'settings.languageFile' in the code above.

    That's it! All the language files will now be saved in the 'lang' directory in your plugin's folder, containing the default language file.


    Now, to create a new language file, just copy the default one, rename it to another language's code, such as nl_NL, fr_FR etc. and then translate & edit all the phrases in the file.

    To change the language file to use, just change the setting you created for it in the configuration to the wanted language file's name, such as nl_NL etc. or whatever you called the language file.

    Hope this helps!

    PS: This is my first tutorial ever. And also, I haven't been coding for long, so any errors in there, please tell me so I can learn from them. Thx! :)
     
    Last edited: Jul 27, 2016
    ChipDev likes this.
Thread Status:
Not open for further replies.

Share This Page