[Tutorial] Languages File

Discussion in 'Resources' started by jackwilsdon, Jul 30, 2013.

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

    jackwilsdon

    Having a languages file for you plugin can be useful for you (the plugin developer), and the end user of your plugin. A language file (aka a Captions file) allows you to change all of the strings in your plugin easily, without messing around with code. This helps you, as you can perfect chat messages after finishing your plugin, and it helps the end user, as they can customise it to fit their server (heck, they can even change the language!). In this tutorial, I'll be showing you how to create a class to handle a custom language file.

    Our language file will because lang.yml, and we are using the YAML format just for ease-of-use (Bukkit has a built in parser). By the end, our lang.yml will contain text like the following;
    Code:text
    1. MsgNotEnoughMoney: '&cHey, you don''t have enough &a$$$&f!'
    2. MsgPaymentCompleted: '&aLooks like that payment has completed!'


    So, let's get started with our caption handler. This tutorial expects you to know at least basic Java, and it would help to be familiar with the Bukkit API.
    Code:java
    1. package me.jackwilsdon.gconomy;
    2.  
    3. import java.io.File;
    4. import java.io.InputStream;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9. import org.bukkit.plugin.Plugin;
    10.  
    11. public class CaptionHandler {
    12. private Plugin plugin = null;
    13.  
    14. private File configFile = null;
    15. private FileConfiguration config = null;
    16.  
    17. public CaptionHandler(Plugin plugin)
    18. {
    19. this.plugin = plugin;
    20. this.configFile = new File(this.plugin.getDataFolder(), "lang.yml");
    21.  
    22. this.reload();
    23. this.saveDefault();
    24. }
    25.  
    26. public void reload()
    27. {
    28. this.config = YamlConfiguration.loadConfiguration(this.configFile);
    29.  
    30. InputStream defaultConfigStream = this.plugin.getResource("lang.yml");
    31. if (defaultConfigStream != null)
    32. {
    33. YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(defaultConfigStream);
    34. this.config.setDefaults(defaultConfig);
    35. }
    36. }
    37.  
    38. public void saveDefault()
    39. {
    40. if (!this.configFile.exists())
    41. {
    42. this.plugin.saveResource("lang.yml", false);
    43. }
    44. }
    45.  
    46. public String getCaption(String name)
    47. {
    48. return this.getCaption(name, false);
    49. }
    50.  
    51. public String getCaption(String name, boolean color)
    52. {
    53. String caption = this.config.getString(name);
    54. if (caption == null)
    55. {
    56. this.plugin.getLogger().warning("Missing caption: " + name);
    57. caption = "&c[missing caption]";
    58. }
    59.  
    60. if (color)
    61. {
    62. caption = ChatColor.translateAlternateColorCodes('&', caption);
    63. }
    64. return caption;
    65. }
    66. }
    67.  
    So, let's go through this class line by line. First of all, we simply create a few variables that hold our plugin reference, along with configuration information. Next up, we set up a constructor for the class, that allows for the class to take an instance of the plugin to use. Inside the constructor, we set the plugin instance, as well as creating the lang.yml file and running the reload() and saveDefault() methods we will look at after.

    Now comes the reload() method. It's a lot less complex than it sounds, it just loads the YamlConfiguration using Bukkit's built-in class, but also creates and loads the defaults from the lang.yml inside out plugin. Our saveDefaults() method is even simpler. It just checks if the lang.yml exists, and if not, writes a new lang.yml file there from inside our plugin. Finally, we have our getCaption() methods, which I will explain below.

    We have 2 getCaption() methods, of which one calls the other. This is called method chaining, and can be useful under certain circumstances where you want 'default' arguments. In our case, we use it to say "unless otherwise specified, we want our messages in color". Our second getCaption() takes a name in for an argument, similarly to the first one. This one however actually manages the captions themselves, in a similar way to the default Bukkit configuration API. We simply get a string from the config file, and if color is true, then we translate the alternate color codes into chat colors.

    And that's it really. You can use our newly created class as follows;
    Code:java
    1. CaptionHandler handler = new CaptionHandler(this); // 'this' must be replaced with a JavaPlugin reference when used from any other classes.
    2. System.out.println(handler.getCaption("MsgPaymentCompleted"));

    I hope you found my first tutorial useful, and if you have any questions, please don't hesitate to ask here or PM me :)
     
Thread Status:
Not open for further replies.

Share This Page