package mezz.jei.util;

import java.util.IllegalFormatException;
import java.util.Locale;

import net.minecraft.client.Minecraft;
import net.minecraft.util.text.translation.I18n;

public final class Translator {
	private Translator() {
	}

	public static String translateToLocal(String key) {
		if (I18n.canTranslate(key)) {
			return I18n.translateToLocal(key);
		} else {
			return I18n.translateToFallback(key);
		}
	}

	public static String translateToLocalFormatted(String key, Object... format) {
		String s = translateToLocal(key);
		try {
			return String.format(s, format);
		} catch (IllegalFormatException e) {
			Log.get().error("Format error: {}", s, e);
			return "Format error: " + s;
		}
	}

	public static String toLowercaseWithLocale(String string) {

		return string.toLowerCase(getLocale());
	}

	private static Locale getLocale() {
		Minecraft minecraft = Minecraft.getMinecraft();
		//noinspection ConstantConditions
		if (minecraft != null) {
			return minecraft.getLanguageManager().getCurrentLanguage().getJavaLocale();
		} else {
			return Locale.getDefault();
		}
	}
}

@@ -123,16 +123,119 @@
 ecraft;%0A
+import net.minecraft.client.resources.Language;%0Aimport net.minecraft.client.resources.LanguageManager;%0A
 import n
@@ -912,16 +912,57 @@
 ));%0A%09%7D%0A%0A
+%09@SuppressWarnings(%22ConstantConditions%22)%0A
 %09private
@@ -1046,130 +1046,267 @@
 ;%0A%09%09
-//noinspection ConstantConditions%0A%09%09if (minecraft != null) %7B%0A%09%09%09return minecraft.getLanguageManager().getCurrentLanguage()
+if (minecraft != null) %7B%0A%09%09%09LanguageManager languageManager = minecraft.getLanguageManager();%0A%09%09%09if (languageManager != null) %7B%0A%09%09%09%09Language currentLanguage = languageManager.getCurrentLanguage();%0A%09%09%09%09if (currentLanguage != null) %7B%0A%09%09%09%09%09return currentLanguage
 .get
@@ -1317,34 +1317,37 @@
 Locale();%0A%09%09
-%7D else %7B%0A%09
+%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09return Loc
@@ -1364,17 +1364,13 @@
 ault();%0A
-%09%09%7D%0A
 %09%7D%0A%7D%0A

Fix #1038 Fix client crash when Minecraft language is null