A simple and efficient localization library for HaxeFlixel games.
- Easy-to-use translation system
- Support for multiple languages
- JSON-based translation files
- Automatic fallback to default language
- Variable interpolation in translations
- Hot-reload support for development
haxelib install langOr install from git:
haxelib git lang https://github.com/LeninAsto/langAdd to your Project.xml:
<haxelib name="lang" />import lang.Lang;
class Main extends FlxState {
override function create() {
super.create();
// Initialize with default language
Lang.init("en");
// Or set a specific language
Lang.setLanguage("es");
}
}Create JSON files in your assets folder following this structure:
assets/
lang/
en.json
es.json
fr.json
Example en.json:
{
"menu": {
"play": "Play",
"options": "Options",
"exit": "Exit"
},
"game": {
"score": "Score: {0}",
"level": "Level {0}"
}
}Example es.json:
{
"menu": {
"play": "Jugar",
"options": "Opciones",
"exit": "Salir"
},
"game": {
"score": "Puntuación: {0}",
"level": "Nivel {0}"
}
}// Simple translation
var playText = Lang.get("menu.play"); // "Play" or "Jugar"
// Translation with variables
var scoreText = Lang.get("game.score", [1000]); // "Score: 1000"
// Check available languages
var languages = Lang.getAvailableLanguages();
// Get current language
var current = Lang.getCurrentLanguage();Initialize the language system with a default language.
defaultLang: Default language code (e.g., "en")langPath: Optional custom path to language files (default: "assets/lang/")
Change the current language.
- Returns
trueif successful,falseif language not found
Get a translated string.
key: Translation key using dot notation (e.g., "menu.play")vars: Optional array of variables to interpolate
Check if a translation key exists.
Get the current language code.
Get list of all available language codes.
Reload all language files (useful for development).
Translation files use JSON format with nested objects:
{
"category1": {
"key1": "value1",
"key2": "value2"
},
"category2": {
"subcategory": {
"key3": "value3"
}
}
}Use {0}, {1}, {2}, etc. for variable placeholders:
{
"welcome": "Welcome, {0}!",
"stats": "Level {0} - Score: {1}"
}Lang.get("welcome", ["Player"]); // "Welcome, Player!"
Lang.get("stats", [5, 1000]); // "Level 5 - Score: 1000"- Organize keys by context: Group related translations together
- Use descriptive keys:
menu.playinstead ofbtn1 - Keep fallback: Always have English (or your default) as fallback
- Test with longest translations: Some languages need more space
- Use variables for dynamic content: Don't concatenate strings
Lang.init("en", "assets/translations/");// Detect system language (requires additional setup)
var systemLang = Sys.systemName(); // Implement your detection logic
Lang.setLanguage(systemLang);// Save to FlxSave
var save = new FlxSave();
save.bind("myGame");
save.data.language = Lang.getCurrentLanguage();
save.flush();
// Load preference
if (save.data.language != null) {
Lang.setLanguage(save.data.language);
}MyGame/
assets/
lang/
en.json
es.json
fr.json
pt.json
source/
Main.hx
states/
MenuState.hx
Contributions are welcome! Please feel free to submit pull requests.
Apache 2.0
Created for Shadow Mario for Psych Engine.