en_messages.yml for different languages

Discussion in 'Plugin Development' started by Dreeass, Aug 7, 2012.

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

    Dreeass

    I'm working on a small project and I want to add multiple languages using _messages.yml, like en_messages.yml would be English. And the language is editable in the config.yml. This is the code that I'm using from a previous project, but it doesn't seem to work on this current project.

    Code:java
    1. public FileConfiguration MsgConfig = null;
    2. public File MsgConfigFile = null;
    3.  
    4.  
    5. public FileConfiguration getMsgConfig() {
    6. if(MsgConfig == null) {
    7. reloadMsgConfig();
    8. }
    9. return MsgConfig;
    10. }
    11. public void reloadMsgConfig() {
    12.  
    13. if (MsgConfigFile == null) {
    14. MsgConfigFile = new File(System.getProperty("me.Dreeass.RegionSpawn"), getConfig().getString("Language") + "_messages.yml" );
    15. }
    16. MsgConfig = YamlConfiguration.loadConfiguration(MsgConfigFile);
    17.  
    18. InputStream defConfigStream = Main.class.getResourceAsStream(getConfig().getString("Language") + "_messages.yml");
    19.  
    20. if(defConfigStream != null) {
    21. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    22. MsgConfig.setDefaults(defConfig);
    23. }
    24. }


    I'm getting an NPE error here on the second line.
    Code:java
    1. if(args.length == 0) {
    2. player.sendMessage(plugin.Msg("More-Arguments-Needed", player.getName()));
    3. return false;
    4. }


    Which refers to the 6th line (I have a 2 methods for an easy replacement, e.g: %regex-> replacement):
    Code:java
    1. public String Msg(String input, String playername) {
    2. if(playername == null){
    3. playername = "console";
    4. }
    5.  
    6. String config = getMsgConfig().getString(input);
    7. String result = config.replaceAll("(&([a-f0-9]))", "\u00A7$2").replaceAll("%player", playername);
    8.  
    9. return result;
    10. };
    11.  
    12. public String Msg(String input, String playername, String regex, String replacement) {
    13. String result = Msg(input, playername).replaceAll("%" + regex, replacement);
    14. return result;
    15. };


    Anyone? Else I'll have to implement it without multilang for now.

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

    Sagacious_Zed Bukkit Docs

    I fell back to using java ResourceBundles.
     
  3. Offline

    pzxc

    If this is the line that's causing the NPE:

    String config = getMsgConfig().getString(input);

    then getMsgConfig() must be what is null. Where is that function defined?
     
  4. Offline

    Dreeass

    Check the first code part. But this code worked fine in my other plugin, I just edited a bit.
     
Thread Status:
Not open for further replies.

Share This Page