Last active
January 2, 2022 21:03
-
-
Save naxmefy/8519ae9dde7857a26585 to your computer and use it in GitHub Desktop.
Unity3D Internationalization (I18N)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
public class English { | |
public static var lang : Hashtable = { | |
// General | |
"game_name": "My Awesome Game" | |
// Menus | |
, "new_game" : "New Game" | |
, "save_game" : "Save Game" | |
, "load_game" : "Load Game" | |
, "quit_game" : "Quit Game" | |
, "end_game" : "End Game" | |
, "settings" : "Settings" | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
function Start() { | |
Debug.Log(I18N.Get('game_name')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
public class German { | |
public static var lang : Hashtable = { | |
// General | |
"game_name": "Mein ehrfürchtiges Spiel" | |
// Menus | |
, "new_game" : "Neues Spiel" | |
, "save_game" : "Spiel speichern" | |
, "load_game" : "Spiel laden" | |
, "quit_game" : "Spiel verlassen" | |
, "end_game" : "Spiel beenden" | |
, "settings" : "Einstellungen" | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
public class I18N { | |
private static var defaultLang : String = "en"; | |
private static var gameLang : String = ""; | |
private static var noTranslationText : String = "Translation missing for {0}"; | |
private static var langs : Hashtable = { | |
"en": English.lang, | |
"de": German.lang | |
}; | |
private static function CheckAndSetUserLanguage() : boolean { | |
switch(Application.systemLanguage) { | |
case SystemLanguage.Afrikaans: SetLang("af"); break; | |
case SystemLanguage.Arabic: SetLang("ar"); break; | |
case SystemLanguage.Basque: SetLang("eu"); break; | |
case SystemLanguage.Belarusian: SetLang("be"); break; | |
case SystemLanguage.Bulgarian: SetLang("bg"); break; | |
case SystemLanguage.Catalan: SetLang("ca"); break; | |
case SystemLanguage.Chinese: SetLang("zh"); break; | |
case SystemLanguage.Czech: SetLang("cs"); break; | |
case SystemLanguage.Danish: SetLang("da"); break; | |
case SystemLanguage.Dutch: SetLang("nl"); break; | |
case SystemLanguage.English: SetLang("en"); break; | |
case SystemLanguage.Estonian: SetLang("et"); break; | |
case SystemLanguage.Faroese: SetLang("fo"); break; | |
case SystemLanguage.Finnish: SetLang("fu"); break; | |
case SystemLanguage.French: SetLang("fr"); break; | |
case SystemLanguage.German: SetLang("de"); break; | |
case SystemLanguage.Greek: SetLang("el"); break; | |
case SystemLanguage.Hebrew: SetLang("he"); break; | |
case SystemLanguage.Icelandic: SetLang("is"); break; | |
case SystemLanguage.Indonesian: SetLang("id"); break; | |
case SystemLanguage.Italian: SetLang("it"); break; | |
case SystemLanguage.Japanese: SetLang("ja"); break; | |
case SystemLanguage.Korean: SetLang("ko"); break; | |
case SystemLanguage.Latvian: SetLang("lv"); break; | |
case SystemLanguage.Lithuanian: SetLang("lt"); break; | |
case SystemLanguage.Norwegian: SetLang("nn"); break; // TODO: Check | |
case SystemLanguage.Polish: SetLang("pl"); break; | |
case SystemLanguage.Portuguese: SetLang("pt"); break; | |
case SystemLanguage.Romanian: SetLang("ro"); break; | |
case SystemLanguage.Russian: SetLang("ru"); break; | |
case SystemLanguage.SerboCroatian: SetLang("sr"); break; // TODO: Check | |
case SystemLanguage.Slovak: SetLang("sk"); break; | |
case SystemLanguage.Slovenian: SetLang("sl"); break; | |
case SystemLanguage.Spanish: SetLang("es"); break; | |
case SystemLanguage.Swedish: SetLang("sv"); break; | |
case SystemLanguage.Thai: SetLang("th"); break; | |
case SystemLanguage.Turkish: SetLang("tr"); break; | |
case SystemLanguage.Ukrainian: SetLang("uk"); break; | |
case SystemLanguage.Vietnamese: SetLang("vi"); break; | |
case SystemLanguage.ChineseSimplified: SetLang("zh_Hans"); break; | |
case SystemLanguage.ChineseTraditional: SetLang("zh_Hant"); break; | |
case SystemLanguage.Unknown: SetLang(defaultLang); break; // Unknow Fallback to defaultLang | |
case SystemLanguage.Hungarian: SetLang("hu"); break; | |
}; | |
if(gameLang.length > 0) { | |
return true; | |
} | |
return false; | |
} | |
public static function GetCurrentLang() : String { | |
CheckAndSetUserLanguage(); | |
return gameLang; | |
} | |
public static function SetLang(lang:String) : void { | |
if(langs.ContainsKey(lang)) { | |
gameLang = lang; | |
} else { | |
gameLang = defaultLang; | |
} | |
} | |
/** | |
* Example use: I18N.Get("game_name") -> "My Awesome Game" | |
*/ | |
public static function Get(key:String) : String { | |
return Get(key, {}); | |
} | |
public static function Get(key:String, args:Hashtable) : String { | |
var lang : Hashtable = langs[GetCurrentLang()] as Hashtable; | |
var value : String = lang[key] as String; | |
if(value.length == 0) { | |
return String.Format(noTranslationText, key); | |
} else { | |
return StringManipulation.Format(value, args); | |
} | |
} | |
} |
Author
naxmefy
commented
Mar 13, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment