Solved Per Player Localization

Discussion in 'Plugin Development' started by FrostDeveloper, Jun 2, 2019.

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

    FrostDeveloper

    Is there any way to get a players Minecraft locale? Im not sure where to start to go about this
     
  2. Offline

    KarimAKL

  3. Offline

    bwfcwalshy Retired Staff

    Player#getLocale
     
  4. Offline

    KarimAKL

  5. Offline

    bwfcwalshy Retired Staff

    @KarimAKL No idea, haven't ever used it. Just know it's there. Should be the same, I'd assume it calls it.
     
    FrostDeveloper and KarimAKL like this.
  6. Offline

    FrostDeveloper

    It seems to be returning the language of the server, but not the language of the individual player.
     
  7. Offline

    KarimAKL

    What do you mean?
    1. It's a method from the 'Player' class, not the 'Server' class.
    2. Does a server even have a specified language?
     
  8. Offline

    bwfcwalshy Retired Staff

    The players locale is sent when they first connect, it's sent in the Client Settings packet (ref: https://wiki.vg/Protocol#Client_Settings) the one CB returns is that one passed. So, you (or whoever tested) likely just didn't actually change the locale. If you think it's an MC issue then it may be an idea to put it on their bug tracker and I'm sure someone can explain what's happening.
     
    KarimAKL likes this.
  9. Offline

    FrostDeveloper

    Thanks for the feedback, But let me get into detail on my current progress, @bwfcwalshy @KarimAKL

    I am using resource bundles to get custom messages from within the jar file and translate messages based on an individual player's locale.

    For example:
    One player's default language is 'en' (English) and execute the example command /example,
    If the command is successfully executed it will send the success message in their default locale, 'en'.

    Another player's default language is 'es' (Spanish) and executes the same command, Their success message will be sent in the 'es' locale.

    Here is the code that I am trying to use to achieve this task, it takes the players locale and turns it into only the language code. Then it returns the resource bundle according to the returned locale
    Code:
    private static String get(CommandSender sender, String key)
    {
        Player player = (Player) sender;
    
        String[] rawLocale  = player.getLocale().split("_");
        String language     = rawLocale[0];
        Locale playerLocale = new Locale(language);
    
        ResourceBundle customBundle  = ResourceBundle.getBundle("translation/message", playerLocale);
        ResourceBundle defaultBundle = ResourceBundle.getBundle("translation/message");
    
        try
        {
            try {
                return format(customBundle.getString(key));
            }
            catch(MissingResourceException ex){
                return format(defaultBundle.getString(key));
            }
        }
        catch (MissingResourceException ex)
        {
            try {
                return format(customBundle.getString("translationNull"), key);
            }
            catch (MissingResourceException ex2){
                return format(defaultBundle.getString("translationNull"), key);
            }
        }
    }
    
    Then this method is called using this method
    Code:
    public static void sendMessage(CommandSender sender, String key)
    {
        sender.sendMessage(get(key));
    }
    
    The resource file names only contain the language code such as
    Code:
    message_en.properties
    message_es.properties
    etc.
    
    The issue is that even after all this, it insists on defaulting to the 'en' locale. Any Help will be appreciated!
     
    Last edited: Jun 12, 2019
  10. Offline

    KarimAKL

  11. Offline

    FrostDeveloper

    @KarimAKL

    I am attempting to get the players locale through the get() Method once the command is executed in a onCommand() method
     
  12. Offline

    KarimAKL

    @FrostDeveloper I have no clue. I asked because i thought you might need to run it some ticks later if you got it from the PlayerJoinEvent, but that shouldn't be a problem when you get it through the command.
     
  13. Offline

    FrostDeveloper

    This Thread Is Solved, Turns Out that the person testing this with me, didn't properly change their language in Minecraft and once corrected, the code worked as designed! Thanks for the help!
     
  14. Offline

    KarimAKL

    @FrostDeveloper Pretty sure that's what @bwfcwalshy said above. :p
    Glad you got it working though. :)
     
Thread Status:
Not open for further replies.

Share This Page