Skip to content

Commit 98b74a7

Browse files
authored
Standardize translation related code (#9588)
1 parent edb8f03 commit 98b74a7

File tree

12 files changed

+155
-111
lines changed

12 files changed

+155
-111
lines changed

src/fheroes2/agg/agg_image.cpp

Lines changed: 108 additions & 72 deletions
Large diffs are not rendered by default.

src/fheroes2/editor/editor_castle_details_window.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ namespace Editor
365365

366366
// Build restrict mode button. We use center_center padding to make sure localized variable-width buttons are centered too.
367367
fheroes2::ButtonSprite buttonRestrictBuilding;
368-
background.renderTextAdaptedButtonSprite( buttonRestrictBuilding, gettext_noop( "RESTRICT" ), { 219, -32 }, fheroes2::StandardWindow::Padding::CENTER_CENTER );
368+
369+
const char * translatedText = fheroes2::getSupportedText( gettext_noop( "RESTRICT" ), fheroes2::FontType::buttonReleasedWhite() );
370+
background.renderTextAdaptedButtonSprite( buttonRestrictBuilding, translatedText, { 219, -32 }, fheroes2::StandardWindow::Padding::CENTER_CENTER );
369371

370372
const bool isNeutral = ( color == Color::NONE );
371373

src/fheroes2/editor/editor_map_specs_window.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,14 +2249,17 @@ namespace Editor
22492249
background.renderButton( buttonOk, buttonOkIcn, 0, 1, { 20 + buttonCancel.area().width + 10, 6 }, fheroes2::StandardWindow::Padding::BOTTOM_RIGHT );
22502250

22512251
fheroes2::ButtonSprite buttonRumors;
2252-
background.renderTextAdaptedButtonSprite( buttonRumors, gettext_noop( "RUMORS" ), { 20, 6 }, fheroes2::StandardWindow::Padding::BOTTOM_LEFT );
2252+
const char * translatedText = fheroes2::getSupportedText( gettext_noop( "RUMORS" ), fheroes2::FontType::buttonReleasedWhite() );
2253+
background.renderTextAdaptedButtonSprite( buttonRumors, translatedText, { 20, 6 }, fheroes2::StandardWindow::Padding::BOTTOM_LEFT );
22532254

22542255
fheroes2::ButtonSprite buttonEvents;
2255-
background.renderTextAdaptedButtonSprite( buttonEvents, gettext_noop( "EVENTS" ), { 20 + buttonRumors.area().width + 10, 6 },
2256+
translatedText = fheroes2::getSupportedText( gettext_noop( "EVENTS" ), fheroes2::FontType::buttonReleasedWhite() );
2257+
background.renderTextAdaptedButtonSprite( buttonEvents, translatedText, { 20 + buttonRumors.area().width + 10, 6 },
22562258
fheroes2::StandardWindow::Padding::BOTTOM_LEFT );
22572259

22582260
fheroes2::ButtonSprite buttonLanguage;
2259-
background.renderTextAdaptedButtonSprite( buttonLanguage, gettext_noop( "LANGUAGE" ), { 20 + buttonRumors.area().width + buttonEvents.area().width + 2 * 10, 6 },
2261+
translatedText = fheroes2::getSupportedText( gettext_noop( "LANGUAGE" ), fheroes2::FontType::buttonReleasedWhite() );
2262+
background.renderTextAdaptedButtonSprite( buttonLanguage, translatedText, { 20 + buttonRumors.area().width + buttonEvents.area().width + 2 * 10, 6 },
22602263
fheroes2::StandardWindow::Padding::BOTTOM_LEFT );
22612264

22622265
auto renderMapName = [&text, &mapFormat, &display, &scenarioBox, &mapNameRoi, &scenarioBoxRoi]() {

src/fheroes2/gui/ui_button.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ namespace fheroes2
837837
}
838838
}
839839

840-
void getTextAdaptedSprite( Sprite & released, Sprite & pressed, const char * untranslatedText, const int emptyButtonIcnID, const int buttonBackgroundIcnID )
840+
void getTextAdaptedSprite( Sprite & released, Sprite & pressed, const char * text, const int emptyButtonIcnID, const int buttonBackgroundIcnID )
841841
{
842842
FontColor buttonFont = FontColor::WHITE;
843843
Point textAreaMargins = { 0, 3 };
@@ -852,13 +852,8 @@ namespace fheroes2
852852

853853
getButtonSpecificValues( emptyButtonIcnID, buttonFont, textAreaMargins, minimumTextArea, maximumTextArea, backgroundBorders, releasedOffset, pressedOffset );
854854

855-
const FontType releasedButtonFont{ FontSize::BUTTON_RELEASED, buttonFont };
856-
857-
// TODO: do not do translations for button generation. We shouldn't assume that we receive a non-translated string.
858-
const char * supportedText = getSupportedText( untranslatedText, releasedButtonFont );
859-
860-
const Text releasedText( supportedText, releasedButtonFont );
861-
const Text pressedText( supportedText, { FontSize::BUTTON_PRESSED, buttonFont } );
855+
const Text releasedText( text, { FontSize::BUTTON_RELEASED, buttonFont } );
856+
const Text pressedText( text, { FontSize::BUTTON_PRESSED, buttonFont } );
862857

863858
// We need to pass an argument to width() so that it correctly accounts for multi-lined texts.
864859
const int32_t textWidth = releasedText.width( maximumTextArea.width );

src/fheroes2/gui/ui_button.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ namespace fheroes2
339339
void unsubscribeAll() const;
340340
};
341341

342+
// !!! IMPORTANT !!!
343+
// None of the functions below translate the text for a button.
344+
// It is the caller's responsibility to pass a translated text if required.
345+
// If you want to translate text call this function.
346+
const char * getSupportedText( const char * untranslatedText, const FontType font );
347+
342348
// Make transparent edges around buttons making the pressed state appear without parts of the released state
343349
void makeTransparentBackground( const Sprite & released, Sprite & pressed, const int backgroundIcnID );
344350

@@ -355,14 +361,11 @@ namespace fheroes2
355361

356362
// Generates released and pressed button sprites with the width and height necessary to fit a provided text using an empty button template ICN and a chosen background
357363
// ICN.
358-
void getTextAdaptedSprite( Sprite & released, Sprite & pressed, const char * untranslatedText, const int icnId, const int buttonBackgroundIcnID );
364+
void getTextAdaptedSprite( Sprite & released, Sprite & pressed, const char * text, const int icnId, const int buttonBackgroundIcnID );
359365

360366
// Generate custom-size released and pressed button sprites with text on them over a chosen background ICN.
361367
void makeButtonSprites( Sprite & released, Sprite & pressed, const std::string & text, const Size buttonSize, const bool isEvilInterface, const int backgroundIcnId );
362368

363-
// TODO: find a better place where to put this function.
364-
const char * getSupportedText( const char * untranslatedText, const FontType font );
365-
366369
void renderTextOnButton( Image & releasedState, Image & pressedState, const std::string & text, const Point & releasedTextOffset, const Point & pressedTextOffset,
367370
const Size & buttonSize, const FontColor fontColor );
368371
}

src/fheroes2/gui/ui_text.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
22
* fheroes2: https://github.com/ihhub/fheroes2 *
3-
* Copyright (C) 2021 - 2024 *
3+
* Copyright (C) 2021 - 2025 *
44
* *
55
* This program is free software; you can redistribute it and/or modify *
66
* it under the terms of the GNU General Public License as published by *
@@ -91,6 +91,11 @@ namespace fheroes2
9191
{
9292
return { FontSize::LARGE, FontColor::WHITE };
9393
}
94+
95+
static FontType buttonReleasedWhite()
96+
{
97+
return { FontSize::BUTTON_RELEASED, FontColor::WHITE };
98+
}
9499
};
95100

96101
struct TextLineInfo

src/fheroes2/monster/monster.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
22
* fheroes2: https://github.com/ihhub/fheroes2 *
3-
* Copyright (C) 2019 - 2024 *
3+
* Copyright (C) 2019 - 2025 *
44
* *
55
* Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
66
* Copyright (C) 2009 by Andrey Afletdinov <[email protected]> *
@@ -781,18 +781,18 @@ uint32_t Monster::GetDwelling() const
781781

782782
const char * Monster::GetName() const
783783
{
784-
return _( fheroes2::getMonsterData( id ).generalStats.name );
784+
return _( fheroes2::getMonsterData( id ).generalStats.untranslatedName );
785785
}
786786

787787
const char * Monster::GetMultiName() const
788788
{
789-
return _( fheroes2::getMonsterData( id ).generalStats.pluralName );
789+
return _( fheroes2::getMonsterData( id ).generalStats.untranslatedPluralName );
790790
}
791791

792792
const char * Monster::GetPluralName( uint32_t count ) const
793793
{
794794
const fheroes2::MonsterGeneralStats & generalStats = fheroes2::getMonsterData( id ).generalStats;
795-
return count == 1 ? _( generalStats.name ) : _( generalStats.pluralName );
795+
return count == 1 ? _( generalStats.untranslatedName ) : _( generalStats.untranslatedPluralName );
796796
}
797797

798798
const char * Monster::getRandomRaceMonstersName( const uint32_t building )

src/fheroes2/monster/monster_info.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
22
* fheroes2: https://github.com/ihhub/fheroes2 *
3-
* Copyright (C) 2021 - 2024 *
3+
* Copyright (C) 2021 - 2025 *
44
* *
55
* This program is free software; you can redistribute it and/or modify *
66
* it under the terms of the GNU General Public License as published by *
@@ -759,8 +759,8 @@ namespace fheroes2
759759

760760
std::ostringstream os;
761761
os << "----------" << std::endl;
762-
os << "Name: " << monster.generalStats.name << std::endl;
763-
os << "Plural name: " << monster.generalStats.pluralName << std::endl;
762+
os << "Name: " << monster.generalStats.untranslatedName << std::endl;
763+
os << "Plural name: " << monster.generalStats.untranslatedPluralName << std::endl;
764764
os << "Base growth: " << monster.generalStats.baseGrowth << std::endl;
765765
os << "Race: " << Race::String( monster.generalStats.race ) << std::endl;
766766
os << "Level: " << monster.generalStats.level << std::endl;

src/fheroes2/monster/monster_info.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ namespace fheroes2
178178

179179
struct MonsterGeneralStats
180180
{
181-
const char * name;
182-
const char * pluralName;
181+
const char * untranslatedName;
182+
const char * untranslatedPluralName;
183183

184184
uint32_t baseGrowth;
185185
uint32_t race;

src/fheroes2/resource/artifact.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
22
* fheroes2: https://github.com/ihhub/fheroes2 *
3-
* Copyright (C) 2019 - 2024 *
3+
* Copyright (C) 2019 - 2025 *
44
* *
55
* Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
66
* Copyright (C) 2009 by Andrey Afletdinov <[email protected]> *
@@ -92,7 +92,7 @@ namespace
9292

9393
const char * Artifact::GetName() const
9494
{
95-
return _( fheroes2::getArtifactData( id ).name );
95+
return _( fheroes2::getArtifactData( id ).untranslatedName );
9696
}
9797

9898
bool Artifact::isUltimate() const
@@ -501,7 +501,7 @@ Artifact Artifact::getArtifactFromMapSpriteIndex( const uint32_t index )
501501

502502
const char * Artifact::getDiscoveryDescription( const Artifact & art )
503503
{
504-
return _( fheroes2::getArtifactData( art.GetID() ).discoveryEventDescription );
504+
return _( fheroes2::getArtifactData( art.GetID() ).untranslatedDiscoveryEventDescription );
505505
}
506506

507507
OStreamBase & operator<<( OStreamBase & stream, const Artifact & art )

src/fheroes2/resource/artifact_info.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
22
* fheroes2: https://github.com/ihhub/fheroes2 *
3-
* Copyright (C) 2022 - 2024 *
3+
* Copyright (C) 2022 - 2025 *
44
* *
55
* This program is free software; you can redistribute it and/or modify *
66
* it under the terms of the GNU General Public License as published by *
@@ -958,9 +958,9 @@ namespace fheroes2
958958

959959
std::string ArtifactData::getDescription( const int extraParameter ) const
960960
{
961-
std::string description( _( baseDescription ) );
961+
std::string description( _( untranslatedBaseDescription ) );
962962

963-
StringReplace( description, "%{name}", _( name ) );
963+
StringReplace( description, "%{name}", _( untranslatedName ) );
964964

965965
std::vector<ArtifactBonus>::const_iterator foundBonus = std::find( bonuses.begin(), bonuses.end(), ArtifactBonus( ArtifactBonusType::ADD_SPELL ) );
966966
if ( foundBonus != bonuses.end() ) {
@@ -1012,11 +1012,11 @@ namespace fheroes2
10121012

10131013
std::ostringstream os;
10141014
os << "----------" << std::endl;
1015-
os << "Name: " << data.name << std::endl;
1015+
os << "Name: " << data.untranslatedName << std::endl;
10161016
os << "Description: " << data.getDescription( Spell::RANDOM ) << std::endl;
10171017

1018-
if ( data.discoveryEventDescription != nullptr ) {
1019-
os << "Discovery event description: " << data.discoveryEventDescription << std::endl;
1018+
if ( data.untranslatedDiscoveryEventDescription != nullptr ) {
1019+
os << "Discovery event description: " << data.untranslatedDiscoveryEventDescription << std::endl;
10201020
}
10211021
else {
10221022
os << "No discovery event description" << std::endl;

src/fheroes2/resource/artifact_info.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
22
* fheroes2: https://github.com/ihhub/fheroes2 *
3-
* Copyright (C) 2022 - 2023 *
3+
* Copyright (C) 2022 - 2025 *
44
* *
55
* This program is free software; you can redistribute it and/or modify *
66
* it under the terms of the GNU General Public License as published by *
@@ -181,12 +181,12 @@ namespace fheroes2
181181

182182
struct ArtifactData
183183
{
184-
const char * name;
184+
const char * untranslatedName;
185185

186186
// Do not use this member directly. Use getDescription() method.
187-
const char * baseDescription;
187+
const char * untranslatedBaseDescription;
188188

189-
const char * discoveryEventDescription;
189+
const char * untranslatedDiscoveryEventDescription;
190190

191191
std::vector<ArtifactBonus> bonuses;
192192
std::vector<ArtifactCurse> curses;

0 commit comments

Comments
 (0)