Best way the handle multiple languages

Discussion in 'Plugin Development' started by GeorgeeeHD, Oct 21, 2014.

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

    GeorgeeeHD

    In a plugin I am making, I have made a way to choose a language for each player. This affects the messages the plugin sends to a player. The only problem is that I can't think of a way to do this well.

    I have an enum of all the languages I support and a method to the the enum value of the language the player has chosen so the first option I thought of was doing a switch over the enum values and sending a different message for each one depending on their chosen language. However this isn't very fun to code especially when you have already pretty much coded the whole plugin and need to go back changing all implementations of sending a message :confused:

    Then I thought of a config for all messages. This would make the code cleaner but I'm not sure it's the best option. Please let me know if you have any better ideas. Thanks
     
  2. Offline

    NovaGamingBrian

    My suggestion is make a public void whit the values message language whit a config
     
  3. Online

    timtower Administrator Administrator Moderator

    GeorgeeeHD I made a class for this stuff.
    Make an instance in the onEnable, it stores each value that the class contains in Language.yml, stop the server, edit the yml, start it again and you have different messages.
    Should work with any type of value as long as you can save it in the config.
    Just say it if you want it.
     
  4. Offline

    GeorgeeeHD

    Yeah would be great if you could link me to it
     
  5. Online

    timtower Administrator Administrator Moderator

    GeorgeeeHD
    Code:java
    1. package nl.timdebrouwer.plotlikeme;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.lang.reflect.Field;
    6. import java.util.Arrays;
    7. import java.util.List;
    8.  
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Lang {
    14. //List all your fields with values here, don't change the methods, make the fields public non static
    15.  
    16. public Lang(JavaPlugin plugin){
    17. try{
    18. CustomConfig file = new CustomConfig(new File(plugin.getDataFolder()+File.separator+"Language.yml"));
    19. file.saveDefaultConfig();
    20. FileConfiguration language = file.getConfig();
    21. Field[] fields = this.getClass().getFields();
    22. for(Field field:fields){
    23. String name = field.getName();
    24. if(!name.toUpperCase().equals(name))continue;
    25. field.setAccessible(true);
    26. if(!language.contains(name)){
    27. language.set(name, field.get(this));
    28. }
    29. field.set(this, language.get(name));
    30. }
    31. file.saveConfig();
    32. }catch(Exception e){
    33. e.printStackTrace();
    34. }
    35. }
    36. private class CustomConfig{
    37. private File configFile;
    38. private FileConfiguration fileConfiguration;
    39.  
    40. public CustomConfig(File file){
    41. this.configFile = file;
    42. }
    43.  
    44. public void reloadConfig() {
    45. fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
    46. }
    47.  
    48. public FileConfiguration getConfig() {
    49. if (fileConfiguration == null) {
    50. this.reloadConfig();
    51. }
    52. return fileConfiguration;
    53. }
    54.  
    55. public void saveConfig() {
    56. if (fileConfiguration == null || configFile == null) {
    57. return;
    58. } else {
    59. try {
    60. getConfig().save(configFile);
    61. } catch (IOException ex) {
    62. System.out.println("Could not save config to " + configFile);
    63. ex.printStackTrace();
    64. }
    65. }
    66. }
    67. public void saveDefaultConfig() {
    68. try{
    69. if (!configFile.exists()) {
    70. configFile.createNewFile();
    71. //this.plugin.saveResource(fileName, false);
    72. }
    73. }catch(Exception e){
    74. e.printStackTrace();
    75. }
    76. }
    77. }
    78. }
     
  6. Offline

    GeorgeeeHD

Thread Status:
Not open for further replies.

Share This Page