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

Introduce an in-game string to support multiple languages #9330

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions VisualStudio/fheroes2/sources.props
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<ClCompile Include="src\fheroes2\game\game_scenarioinfo.cpp" />
<ClCompile Include="src\fheroes2\game\game_startgame.cpp" />
<ClCompile Include="src\fheroes2\game\game_static.cpp" />
<ClCompile Include="src\fheroes2\game\game_string.cpp" />
<ClCompile Include="src\fheroes2\game\game_video.cpp" />
<ClCompile Include="src\fheroes2\game\highscores.cpp" />
<ClCompile Include="src\fheroes2\gui\cursor.cpp" />
Expand Down Expand Up @@ -343,6 +344,7 @@
<ClInclude Include="src\fheroes2\game\game_mode.h" />
<ClInclude Include="src\fheroes2\game\game_over.h" />
<ClInclude Include="src\fheroes2\game\game_static.h" />
<ClInclude Include="src\fheroes2\game\game_string.h" />
<ClInclude Include="src\fheroes2\game\game_video.h" />
<ClInclude Include="src\fheroes2\game\game_video_type.h" />
<ClInclude Include="src\fheroes2\game\highscores.h" />
Expand Down
5 changes: 3 additions & 2 deletions src/fheroes2/castle/castle_tavern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "castle.h" // IWYU pragma: associated
#include "dialog.h"
#include "game_delays.h"
#include "game_string.h"
#include "icn.h"
#include "translations.h"
#include "ui_dialog.h"
Expand All @@ -37,14 +38,14 @@

void Castle::_openTavern() const
{
auto [rumor, language] = world.getCurrentRumor();
auto rumor = world.getCurrentRumor();

std::string body( _( "A generous tip for the barkeep yields the following rumor:" ) );
body += "\n\n";

auto text = std::make_shared<fheroes2::MultiFontText>();
text->add( fheroes2::Text{ std::move( body ), fheroes2::FontType::normalWhite() } );
text->add( fheroes2::Text{ std::move( rumor ), fheroes2::FontType::normalWhite(), language } );
text->add( fheroes2::Text{ std::move( rumor.text ), fheroes2::FontType::normalWhite(), rumor.language } );

const fheroes2::AnimationDialogElement imageUI( ICN::TAVWIN, { 0, 1 }, 0, Game::getAnimationDelayValue( Game::CASTLE_TAVERN_DELAY ) );
const fheroes2::TextDialogElement textBodyUI( text );
Expand Down
56 changes: 56 additions & 0 deletions src/fheroes2/game/game_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/***************************************************************************
* fheroes2: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2024 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include "game_string.h"

#include "serialize.h"

OStreamBase & operator<<( OStreamBase & stream, const fheroes2::LocalizedString & string )
{
stream << string.text;

stream << string.language.has_value();

if ( string.language.has_value() ) {
stream << string.language.value();
}

return stream;
}

IStreamBase & operator>>( IStreamBase & stream, fheroes2::LocalizedString & string )
{
stream >> string.text;

bool hasValue = false;
stream >> hasValue;

if ( hasValue ) {
fheroes2::SupportedLanguage language;

stream >> language;
string.language = language;
}
else {
string.language.reset();
}

return stream;
}
44 changes: 44 additions & 0 deletions src/fheroes2/game/game_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/***************************************************************************
* fheroes2: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2024 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#pragma once

#include <cstdint>
#include <optional>
#include <string>

class IStreamBase;
class OStreamBase;

namespace fheroes2
{

enum class SupportedLanguage : uint8_t;

struct LocalizedString
{
std::string text;

std::optional<SupportedLanguage> language;
};
}

OStreamBase & operator<<( OStreamBase & stream, const fheroes2::LocalizedString & string );
IStreamBase & operator>>( IStreamBase & stream, fheroes2::LocalizedString & string );
3 changes: 2 additions & 1 deletion src/fheroes2/world/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <array>
#include <cassert>
#include <limits>
#include <optional>
#include <ostream>
#include <set>
#include <tuple>
Expand Down Expand Up @@ -707,7 +708,7 @@ void World::MonthOfMonstersAction( const Monster & mons )
}
}

std::pair<std::string, std::optional<fheroes2::SupportedLanguage>> World::getCurrentRumor() const
fheroes2::LocalizedString World::getCurrentRumor() const
{
const uint32_t standardRumorCount = 10;
const uint32_t totalRumorCount = static_cast<uint32_t>( _customRumors.size() ) + standardRumorCount;
Expand Down
5 changes: 2 additions & 3 deletions src/fheroes2/world/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>
Expand All @@ -37,7 +36,7 @@
#include "army_troop.h"
#include "artifact_ultimate.h"
#include "castle.h"
#include "game_language.h"
#include "game_string.h"
#include "heroes.h"
#include "kingdom.h"
#include "maps.h"
Expand Down Expand Up @@ -358,7 +357,7 @@ class World : protected fheroes2::Size
void NewWeek();
void NewMonth();

std::pair<std::string, std::optional<fheroes2::SupportedLanguage>> getCurrentRumor() const;
fheroes2::LocalizedString getCurrentRumor() const;

int32_t NextTeleport( const int32_t index ) const;
MapsIndexes GetTeleportEndPoints( const int32_t index ) const;
Expand Down
Loading