Skip to content

Commit

Permalink
Move language setting variables from strings.csv into their respectiv…
Browse files Browse the repository at this point in the history
…e language JSON files
  • Loading branch information
cjhoward committed Dec 6, 2023
1 parent 2f779f6 commit 591ff8b
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
bin
build
res/data
.~lock*
8 changes: 7 additions & 1 deletion res/localization/languages/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// SPDX-FileCopyrightText: 2023 C. J. Howard
// SPDX-License-Identifier: CC-BY-SA-4.0
{
"tag":"en",
"name":"English",
"direction":"ltr"
"direction":"ltr",

"font_serif":"castoro-regular.woff",
"font_sans_serif":"noto-sans-regular.woff",
"font_monospace":"iosevka-regular.woff",
"font_dyslexia":"open-dyslexic-3-regular.woff"
}
8 changes: 7 additions & 1 deletion res/localization/languages/zh-Hans.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// SPDX-FileCopyrightText: 2023 C. J. Howard
// SPDX-License-Identifier: CC-BY-SA-4.0
{
"tag":"zh-Hans",
"name":"简体中文",
"direction":"ltr"
"direction":"ltr",

"font_serif":"noto-serif-sc-regular.woff",
"font_sans_serif":"noto-sans-sc-regular.woff",
"font_monospace":"iosevka-regular.woff",
"font_dyslexia":null
}
8 changes: 7 additions & 1 deletion res/localization/languages/zh-Hant.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// SPDX-FileCopyrightText: 2023 C. J. Howard
// SPDX-License-Identifier: CC-BY-SA-4.0
{
"tag":"zh-Hant",
"name":"繁體中文",
"direction":"ltr"
"direction":"ltr",

"font_serif":"noto-serif-tc-regular.woff",
"font_sans_serif":"noto-sans-tc-regular.woff",
"font_monospace":"iosevka-regular.woff",
"font_dyslexia":null
}
10 changes: 0 additions & 10 deletions res/localization/strings.csv
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
key,context,en,zh-Hans,zh-Hant
,,,,
language_name_english,English name of the language.,English,Chinese (Simplified),Chinese (Traditional)
language_name_native,Native name of the language.,English,简体中文,繁體中文
language_tag,IETF language tag of the language.,en,zh-Hans,zh-Hant
language_direction,"Text direction of the language (“ltr” for left-to-right, or “rtl” for right-to-left).",ltr,ltr,ltr
,,,,
font_serif,Filename of a serif font which supports the target language.,castoro-regular.woff,noto-serif-sc-regular.woff,noto-serif-tc-regular.woff
font_sans_serif,Filename of a sans-serif font which supports the target language.,noto-sans-regular.woff,noto-sans-sc-regular.woff,noto-sans-tc-regular.woff
font_monospace,Filename of a monospace font which supports the Basic Latin unicode block (U+0000-007f).,iosevka-regular.woff,iosevka-regular.woff,iosevka-regular.woff
font_dyslexia,Filename of a dyslexia-friendly font which supports the target language (if any).,open-dyslexic-3-regular.woff,,
,,,,
window_title,Text displayed in the title of the game window.,Antkeeper,Antkeeper,Antkeeper
,,,,
title_antkeeper,Title screen text displaying the title of the game.,Antkeeper,Antkeeper,Antkeeper
Expand Down
17 changes: 11 additions & 6 deletions src/game/fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ static void build_bitmap_font(const type::typeface& typeface, float size, const

void load_fonts(::game& ctx)
{
const auto& language = (*ctx.languages)[ctx.language_tag];

// Load dyslexia-friendly typeface (if enabled)
bool dyslexia_font_loaded = false;
if (ctx.dyslexia_font)
{
const auto dyslexia_font_path = get_string(ctx, "font_dyslexia");
ctx.typefaces["dyslexia"] = ctx.resource_manager->load<type::typeface>(dyslexia_font_path);
dyslexia_font_loaded = true;
const auto& dyslexia_font_path = language["font_dyslexia"];
if (!dyslexia_font_path.is_null())
{
ctx.typefaces["dyslexia"] = ctx.resource_manager->load<type::typeface>(dyslexia_font_path);
dyslexia_font_loaded = true;
}
}

// Load typefaces
Expand All @@ -74,9 +79,9 @@ void load_fonts(::game& ctx)
else
{
// Load standard typefaces
const auto serif_font_path = get_string(ctx, "font_serif");
const auto sans_serif_font_path = get_string(ctx, "font_sans_serif");
const auto monospace_font_path = get_string(ctx, "font_monospace");
const auto serif_font_path = language["font_serif"];
const auto sans_serif_font_path = language["font_sans_serif"];
const auto monospace_font_path = language["font_monospace"];

ctx.typefaces["serif"] = ctx.resource_manager->load<type::typeface>(serif_font_path);
ctx.typefaces["sans_serif"] = ctx.resource_manager->load<type::typeface>(sans_serif_font_path);
Expand Down
23 changes: 13 additions & 10 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ game::game(int argc, const char* const* argv)
setup_window();
setup_audio();
setup_input();
load_strings();
load_language();
setup_rendering();
setup_scenes();
setup_animation();
Expand Down Expand Up @@ -608,30 +608,33 @@ void game::setup_input()
*/
}

void game::load_strings()
void game::load_language()
{
debug::log_trace("Loading strings...");
debug::log_trace("Loading language...");

// Default strings settings
// Default language tag setting
language_tag = "en";

// Read strings settings
// Read language tag setting
read_or_write_setting(*this, "language_tag", language_tag);

// Load string map
string_map = resource_manager->load<i18n::string_map>(std::format("localization/strings.{}.json", language_tag));

// Log language info
// Log language tag
debug::log_info("Language tag: {}", language_tag);

// Load languages
languages = resource_manager->load<json>("localization/languages.json");

// Load language string map
string_map = resource_manager->load<i18n::string_map>(std::format("localization/strings.{}.json", language_tag));

// Change window title
const std::string window_title = get_string(*this, "window_title");
window->set_title(window_title);

// Update window title setting
(*settings)["window_title"] = window_title;

debug::log_trace("Loaded strings");
debug::log_trace("Loaded language");
}

void game::setup_rendering()
Expand Down
4 changes: 3 additions & 1 deletion src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <engine/type/bitmap-font.hpp>
#include <engine/type/typeface.hpp>
#include <engine/utility/dict.hpp>
#include <engine/utility/json.hpp>
#include <engine/math/vector.hpp>
#include <engine/utility/state-machine.hpp>
#include <engine/utility/frame-scheduler.hpp>
Expand Down Expand Up @@ -164,6 +165,7 @@ class game
bool gamepad_active;

// Localization and internationalization
std::shared_ptr<json> languages;
std::string language_tag;
std::shared_ptr<i18n::string_map> string_map;

Expand Down Expand Up @@ -418,7 +420,7 @@ class game
void load_settings();
void setup_window();
void setup_input();
void load_strings();
void load_language();
void setup_rendering();
void setup_audio();
void setup_scenes();
Expand Down
34 changes: 9 additions & 25 deletions src/game/states/language-menu-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,8 @@ language_menu_state::language_menu_state(::game& ctx):
{
debug::log_trace("Entering language menu state...");

// Load language manifest
language_manifest = ctx.resource_manager->load<json>("localization/languages.json");
if (!language_manifest)
{
throw std::runtime_error("Failed to load language manifest");
}

// Determine index of current language
for (language_it = language_manifest->begin(); language_it != language_manifest->end(); ++language_it)
{
if (ctx.language_tag == language_it.key())
{
break;
}
}

// Check if language was found
if (language_it == language_manifest->end())
// Get iterator to current language
if (language_it = ctx.languages->find(ctx.language_tag); language_it == ctx.languages->end())
{
throw std::runtime_error("Language not found");
}
Expand Down Expand Up @@ -72,10 +56,10 @@ language_menu_state::language_menu_state(::game& ctx):
// Load language strings
ctx.string_map = ctx.resource_manager->load<i18n::string_map>(std::format("localization/strings.{}.json", ctx.language_tag));

// Update language settings
// Update language tag settings
(*ctx.settings)["language_tag"] = ctx.language_tag;

// Log language change
// Log language tag
debug::log_info("Language tag: {}", ctx.language_tag);

// Reload fonts
Expand All @@ -93,22 +77,22 @@ language_menu_state::language_menu_state(::game& ctx):
// Construct menu item callbacks
auto next_language_callback = [this, &ctx, change_language]()
{
if (++language_it; language_it == language_manifest->end())
if (++language_it; language_it == ctx.languages->end())
{
language_it = language_manifest->begin();
language_it = ctx.languages->begin();
}

change_language();
};
auto previous_language_callback = [this, &ctx, change_language]()
{
if (language_it != language_manifest->begin())
if (language_it != ctx.languages->begin())
{
--language_it;
}
else
{
language_it = language_manifest->end();
language_it = ctx.languages->end();
--language_it;
}

Expand Down Expand Up @@ -181,6 +165,6 @@ void language_menu_state::update_text_content()
auto [back_name, back_value] = ctx.menu_item_texts[1];

language_name->set_content(get_string(ctx, "language_menu_language"));
language_value->set_content(get_string(ctx, "language_name_native"));
language_value->set_content((*ctx.languages)[ctx.language_tag]["name"]);
back_name->set_content(get_string(ctx, "back"));
}

0 comments on commit 591ff8b

Please sign in to comment.