Graindcafe's Locale system, easly add internationalization to your plugin

Discussion in 'Resources' started by Graindcafe, Aug 8, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Graindcafe

    Hi,

    First of all, why internationalization (i18n) ?
    Because, guys, you're not alone ! English is very useful as almost everyone understand it, so it's fine for the bukkit's board and others things like that but users and server administrator should like to use minecraft in their own language. So, yes, Minecraft is only in english for the moment, but Minecraft is not very verbose so it's not really a problem. Bukkit's plugins are ! So, by using my system - mine because I think I'm the first to post a language system and I think it should not be others, I'm open to all proposal, all I want is to keep it simple to use - you offer a big improvement to Minecraft. Another improvement is that this let administrators choose what your plugin say.

    My goal is simple, improves this system till bukkit add it, so it will be 1 system (and you may think "ooh ok this guy want its own system, it may be very egocentric.", no! All I want is one single system, as I said previously).

    How it works ?
    For the server administrator
    In the plugin directory you have the folder "languages" place your language file there. The plugin should let you choose your language, probably in the config file.​
    You can customize it by creating a "custom.yml" setting "Default:" to your default language and replacing keys you want.​
    For the translaters
    You have a default YAML file. For the first translation, just change values of each key. For next translations, running the plugin should add in the header the missings sentences.You can use colors (&A or §a). Set the key "Author" by your name. If your language is used (as main, or default) you will appear as one of the language author (if the plugin developper display it).​
    For the developpers
    The big problem is to implements the system when you already have a plugin, it will be tedious!​
    You have two classes, "Language" and "DefaultLanguage" . DefaultLanguage is where you should set every sentences you want. There is a hashmap, keys are YAML keys, so you can use categories (see examples).​
    Then, when enabling your plugin, you should instanciate a new Language and use the methods get(key) to get a sentence. It's all, this will give you the first sentence it can found, if a language haven't the specified key, it ask its default language, and if none language have, it ask your "DefaultLanguage", so you should ever have a sentence and never "null".​


    Download sources
    Github

    Examples :

    Init the language system :
    Code:
    DefaultLanguage.setAuthor("Graindcafe");
    DefaultLanguage.setName("English");
    DefaultLanguage.setVersion(Constants.LanguageFileVersion);
    DefaultLanguage.setLanguagesFolder(getDataFolder().getPath()+"/languages/");
    DefaultLanguage.setLocales(new HashMap<String, String>() {
        {
               put("Key", "Value");
        }
    }
    Language lang = Language.init(log, getConfiguration().getString("PluginMode.Language", "english"));
    
    Get a locale :
    Code:
    private String getLocale(String key){
            // assuming lang is a language
            String r = lang.get(key);
            if (r == null) {
                log.warning(key + " not found");
                r = ChatColor.RED + "An error occured while getting this message";
            }
            return r;
    }
    Example Language file :
    (Consider that this is the file "example.yml")
    Code:
    Author: Graindcafe
    Version: 1
    Info:
        -ChosenLanguage: "Chosen language : %s. Provided by %s."
    Warning:
        -LanguageFileMissing: "The specified language file is missing!"
        -LanguageOutdated: "Your language file is outdated!"
    Message:
        -AnExample: "&aThis is a green example!"
        -AnotherExample: "§cThis is another example, that is red"
    
    
    Example customized language file :
    (Consider it's the file "custom.yml")
    Code:
    Default: example
    Author: TheAuthor
    Message:
       -AnExample: "&eThis is a yellow example, not green!"
    
    if you init the language with "custom" then,
    lang.get("AnExample") will give: This is a yellow example, not green!
    lang.get("AnotherExample") will give : This is another example, that is red
    lang.getAuthor() will give : TheAuthor, Graindcafe
    lang.getVersion() : 1

    What do you think of that ?

    The code is there ;-)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  2. Offline

    Graindcafe

    Updated, now it's fully usable
     
Thread Status:
Not open for further replies.

Share This Page