Vhoyon / Discord-Bot

A Bot that we make for learning stuff out and having fun!
MIT License
1 stars 3 forks source link

Implement a way to handle pluralization in language lines #87

Closed V-ed closed 6 years ago

V-ed commented 6 years ago

This could be heavily inspired by Laravel's Localization utility (https://laravel.com/docs/5.6/localization#retrieving-translation-strings) :

AppleCount=[0] There are none|[1,19] There are some|[20,*] There are many

A new method to get those types of lines would be added to the Translatable and Dictionary objects : langAmount().

This langAmount method would take some parameters :

/**
@param key Key of the lang to map from the lang() method.
@param amount Integer telling the amount of objects there is - used to determine which portion of the lang to pick from the list.
@param replacements Variable amount of replacements to replace text in the line found.
*/
String langAmount(String key, int amount, String... replacements)

This way, to further the example above, trying to access AppleCount would be like so :

String appleText = langAmount("AppleCount", list.size());

Another example would be this string :

VariableCounter=[0] There is no variable here!|[1] There is only 1 variable here...|[2,20] There are {0} variables here!|[21,*] There is too many variables in here, the maximum accepted is 20!

Accessed this way :

String variableCountText = langAmount("VariableCounter", variables.size(), variable.size());

In that example, if the number of variable is between 2 and 20, it will actually show the number of variables in the formatted text, and that in different languages if wanted!

int size;

size = 4;
String variableCountText = langAmount("VariableCounter", size, size);
// variableCountText = "There are 4 variables here!"

size = 1;
String variableCountText = langAmount("VariableCounter", size, size);
// variableCountText = "There is only 1 variable here..."

size = 24;
String variableCountText = langAmount("VariableCounter", size, size);
// variableCountText = "There is too many variables in here, the maximum accepted is 20!"

We could throw a custom NumberOutOfRangeException (that extends IndexOutOfBoundsException) if the number is not between the range the line allowed :

int size = -2;
String variableCountText = langAmount("VariableCounter", size, size);
// throws NumberOutOfRangeException
V-ed commented 6 years ago

I started thinking about how this will be implemented and came up with an object to handle lang lines : https://gist.github.com/V-ed/ffa57aec6bf646d2749a03a01c21d265

This code is not meant to be used in our Bot yet, but might be used as a template later on. This was just a proof of concept that such string could be handled by an object automatically.

This gist also doesn't include an idea I had while doing the code snippet : a LangAmountManager that would keep track of which range is used, which is available, handle different formats automatically, etc.

I'll say it again just in case : Please do not use this gist as-is, as it is not optimized, not entirely functionnal (works on a perfect scenario but as no error handling, etc) and repeats code.

V-ed commented 6 years ago

Issue moved to Vhoyon/Vramework #5 via ZenHub