Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Localization #4067

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
started localization
  • Loading branch information
MidyGamy committed Jan 30, 2025
commit 98d8c35c54aa12fe8f5633322c3a1ae89e35b586
7 changes: 7 additions & 0 deletions hmm.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"dependencies": [
{
"name": "firetongue",
"type": "git",
"dir": null,
"ref": "a6d8cd3927885aa7b0de881ddb202a74da2603b2",
"url": "https://github.com/larsiusprime/firetongue.git"
},
{
"name": "FlxPartialSound",
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions project.hxp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ class Project extends HXProject {
addHaxelib('flixel-text-input'); // Improved text field rendering for HaxeUI

addHaxelib('polymod'); // Modding framework
addHaxelib('firetongue'); // Localization framework
addHaxelib('flxanimate'); // Texture atlas rendering

addHaxelib('json2object'); // JSON parsing
Expand Down Expand Up @@ -693,6 +694,10 @@ class Project extends HXProject {
var shouldEmbedFonts = true;
addAssetPath("assets/fonts", null, "default", ["*"], exclude, shouldEmbedFonts);

// Localization asset
var shouldPreloadDefault = true;
addAssetPath("assets/locales", "assets/locales", "default", ["*"], exclude, shouldEmbed);

// Shared asset libraries
addAssetLibrary("songs", shouldEmbed, shouldPreload);
addAssetPath("assets/songs", "assets/songs", "songs", ["*"], exclude, shouldEmbed);
Expand Down
24 changes: 24 additions & 0 deletions source/funkin/Localization.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package funkin;

import firetongue.FireTongue;
import firetongue.Replace;

class Localization
{
public static var tongue(get, never):FireTongue;
static var _tongue:FireTongue;

static function get_tongue():FireTongue
{
if (_tongue == null)
{
_tongue = new FireTongue();
}
return _tongue;
}

/**
* Loads the user's preferences for the localization and loads it.
*/
public static function init():Void {}
}
29 changes: 29 additions & 0 deletions source/funkin/Preferences.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ import funkin.save.Save;
*/
class Preferences
{
/**
* The display language/localization for texts.
* @default `en-US`
*/
public static var locale(get, set):String;

static function get_locale():String
{
return Save?.instance?.options?.locale ?? 'en-US';
}

static function set_locale(value:String):String
{
var save:Save = Save.instance;
save.options.locale = value;
save.flush();
Localization.tongue.initialize(
{
locale: value,
checkMissing: true
});
return value;
}

/**
* FPS
* @default `60`
Expand Down Expand Up @@ -211,6 +235,11 @@ class Preferences
#if web
toggleFramerateCap(Preferences.unlockedFramerate);
#end
Localization.tongue.initialize(
{
locale: locale,
checkMissing: true
});
}

static function toggleFramerateCap(unlocked:Bool):Void
Expand Down
7 changes: 7 additions & 0 deletions source/funkin/save/Save.hx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Save
options:
{
// Reasonable defaults.
locale: "en-US",
framerate: 60,
naughtyness: true,
downscroll: false,
Expand Down Expand Up @@ -1297,6 +1298,12 @@ typedef SaveScoreTallyData =
*/
typedef SaveDataOptions =
{
/**
* The display language/localization for texts.
* @default `en-US`
*/
var locale:String;

/**
* FPS
* @default `60`
Expand Down
48 changes: 7 additions & 41 deletions source/funkin/ui/options/ControlsMenu.hx
Original file line number Diff line number Diff line change
Expand Up @@ -96,52 +96,18 @@ class ControlsMenu extends funkin.ui.options.OptionsState.Page
{
var control = controlList[i];
var name = control.getName();
if (currentHeader != "UI_" && name.indexOf("UI_") == 0)
var controlHeader:String = name.substr(0, name.indexOf("_"));
if (controlHeader != currentHeader && name.contains("_"))
{
currentHeader = "UI_";
headers.add(new AtlasText(0, y, "UI", AtlasFont.BOLD)).screenCenter(X);
y += spacer;
}
else if (currentHeader != "NOTE_" && name.indexOf("NOTE_") == 0)
{
currentHeader = "NOTE_";
headers.add(new AtlasText(0, y, "NOTES", AtlasFont.BOLD)).screenCenter(X);
y += spacer;
}
else if (currentHeader != "CUTSCENE_" && name.indexOf("CUTSCENE_") == 0)
{
currentHeader = "CUTSCENE_";
headers.add(new AtlasText(0, y, "CUTSCENE", AtlasFont.BOLD)).screenCenter(X);
y += spacer;
}
else if (currentHeader != "FREEPLAY_" && name.indexOf("FREEPLAY_") == 0)
{
currentHeader = "FREEPLAY_";
headers.add(new AtlasText(0, y, "FREEPLAY", AtlasFont.BOLD)).screenCenter(X);
y += spacer;
}
else if (currentHeader != "WINDOW_" && name.indexOf("WINDOW_") == 0)
{
currentHeader = "WINDOW_";
headers.add(new AtlasText(0, y, "WINDOW", AtlasFont.BOLD)).screenCenter(X);
y += spacer;
}
else if (currentHeader != "VOLUME_" && name.indexOf("VOLUME_") == 0)
{
currentHeader = "VOLUME_";
headers.add(new AtlasText(0, y, "VOLUME", AtlasFont.BOLD)).screenCenter(X);
y += spacer;
}
else if (currentHeader != "DEBUG_" && name.indexOf("DEBUG_") == 0)
{
currentHeader = "DEBUG_";
headers.add(new AtlasText(0, y, "DEBUG", AtlasFont.BOLD)).screenCenter(X);
currentHeader = controlHeader;
headers.add(new AtlasText(0, y, Localization.tongue.get("$CATEGORY_" + currentHeader, "option_controls"), AtlasFont.BOLD)).screenCenter(X);
y += spacer;
}

if (currentHeader != null && name.indexOf(currentHeader) == 0) name = name.substr(currentHeader.length);
// if (currentHeader != null && name.indexOf(currentHeader) == 0) name = name.substr(currentHeader.length);

var formatName = name.replace('_', ' ');
// var formatName = name.replace('_', ' ');
var formatName = Localization.tongue.get("$" + name, "controls");
var label = labels.add(new AtlasText(100, y, formatName, AtlasFont.BOLD));
label.alpha = 0.6;
for (i in 0...COLUMNS)
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/ui/story/StoryMenuState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class StoryMenuState extends MusicBeatState

highScoreLerp = Std.int(MathUtil.smoothLerp(highScoreLerp, highScore, elapsed, 0.25));

scoreText.text = 'LEVEL SCORE: ${Math.round(highScoreLerp)}';
scoreText.text = Localization.tongue.get("$LEVEL_SCORE", "menus") + ' ${Math.round(highScoreLerp)}';

levelTitleText.text = currentLevel.getTitle();
levelTitleText.x = FlxG.width - (levelTitleText.width + 10); // Right align.
Expand Down