Added LocalesManager#get(String)

This is something I did in this commit 926473cd07 and I'm putting it back in develop branch
This commit is contained in:
Florian CUNY 2018-11-17 11:48:08 +01:00
parent d9528c6c9b
commit f0218d9f2a

View File

@ -35,21 +35,34 @@ public class LocalesManager {
} }
/** /**
* Gets the reference from the locale file for this user * Gets the translated String corresponding to the reference from the locale file for this user.
* @param user - the User * @param user the User
* @param reference - a reference that can be found in a locale file * @param reference a reference that can be found in a locale file
* @return the translated string, or if the translation does not exist, the default language version, or if that does not exist null * @return the translated String from the User's locale or from the server's locale or from the en-US locale, or null.
*/ */
public String get(User user, String reference) { public String get(User user, String reference) {
BentoBoxLocale locale = languages.get(user.getLocale()); // Make sure the user is not null
if (locale != null && locale.contains(reference)) { if (user != null) {
return locale.get(reference); BentoBoxLocale locale = languages.get(user.getLocale());
if (locale != null && locale.contains(reference)) {
return locale.get(reference);
}
} }
// Return the default // No translation could be gotten from the player's locale, trying more generic solutions
return get(reference);
}
/**
* Gets the translated String corresponding to the reference from the server's or the en-US locale file.
* @param reference a reference that can be found in a locale file
* @return the translated String from the server's locale or from the en-US locale, or null.
*/
public String get(String reference) {
// Get the translation from the server's locale
if (languages.get(Locale.forLanguageTag(plugin.getSettings().getDefaultLanguage())).contains(reference)) { if (languages.get(Locale.forLanguageTag(plugin.getSettings().getDefaultLanguage())).contains(reference)) {
return languages.get(Locale.forLanguageTag(plugin.getSettings().getDefaultLanguage())).get(reference); return languages.get(Locale.forLanguageTag(plugin.getSettings().getDefaultLanguage())).get(reference);
} }
// Or try in the en-US locale // Get the translation from the en-US locale
if (languages.get(Locale.forLanguageTag("en-US")).contains(reference)) { if (languages.get(Locale.forLanguageTag("en-US")).contains(reference)) {
return languages.get(Locale.forLanguageTag("en-US")).get(reference); return languages.get(Locale.forLanguageTag("en-US")).get(reference);
} }