Solved Replacing Objects with Strings

Discussion in 'Plugin Development' started by FrostDevStudios, Jun 20, 2017.

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

    FrostDevStudios


    Code:

    What I am using to replace the objects
    Code:
        public static String getProperty(String key, Object[] obj)
        {
            Properties prop = new Properties(); 
            return prop.getProperty(key).replace("{0}", objects.toString());
        }
    What is Being Translated/Replaced
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            String zero = "This";
            String one = "Is";
            String two = "A";
            String three = "Test";
          
            if(cmd.getName().equalsIgnoreCase("test")){
                sender.sendMessage(Locale.getProperty("test", new Object[]{zero,one,two, three}));
            }
          
            return true;
        }
    Problem:. I am Trying to replace the objects placeholders such as {0}, {1}, {2}, etc.. using "new Object[]{zero, one, two, three}" inside of the send message method

    I Do not receive any errors besides the objects not being replaced
     
    Last edited: Jun 20, 2017
  2. Offline

    RcExtract

    String inherits Object so simply do String[]. Besides, please show the code of getProperty method.

    If you want to build a Placeholder functioning method, you should loop all the strings inside the array and replace the equal characters inside the string.

    Sent from my LG-H860 using Tapatalk
     
  3. Offline

    FrostDevStudios

    @RcExtract
    I Already Gave you the getProperty method, This is located in my Locale.java Class. Hence where Locale.getProperty(key) came from
    Code:
        public static String getProperty(String key, Object[] obj)
        {
            Properties prop = new Properties();
            return prop.getProperty(key).replace("{0}", objects.toString());
        }

    Also, does anyone have any suggestions as to what to replace {0} so that i can replace multiple of these? Example {0} {1} {2} etc.
    Code:
          return prop.getProperty(key).replace("{0}", objects.toString());
    
    I Solved The Issue,

    After a long search on google, i found out that it actually is not that complicated to do this, I can use the MessageFormat Class from Java to replace the objects

    Thanks anyways for your help!


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Jun 21, 2017
Thread Status:
Not open for further replies.

Share This Page