Cleanup localization behaviour

develop
kaetemi 4 years ago
parent 39fc4895d2
commit 2a7bf997b9

@ -84,8 +84,10 @@ namespace NLGUI
/// Set /// Set
void setText(const std::string &text); //< Not localized. Use setHardText to localize strings starting with "ui". TODO: Add a Localize flag, like title in group container. HardText then simply sets localize to true. void setText(const std::string &text);
void setTextLocalized(const std::string &text, bool localized);
void setTextAsUtf16 (const ucstring &text); void setTextAsUtf16 (const ucstring &text);
void setLocalized(bool localized);
void setFontName (const std::string &name); void setFontName (const std::string &name);
void setFontSize (sint nFontSize, bool coef = true); void setFontSize (sint nFontSize, bool coef = true);
void setEmbolden (bool nEmbolden); void setEmbolden (bool nEmbolden);
@ -114,9 +116,10 @@ namespace NLGUI
void disableStringSelection(); void disableStringSelection();
/// Get /// Get
std::string getText() const { return _Text; } std::string getText() const { return _HardText.empty() ? _Text : _HardText; }
ucstring getTextAsUtf16() const; ucstring getTextAsUtf16() const;
ucstring getHardTextAsUtf16() const; ucstring getHardTextAsUtf16() const;
bool isLocalized() const { return _Localized; }
sint getFontSize() const; sint getFontSize() const;
std::string getFontName() const { return _FontName; } std::string getFontName() const { return _FontName; }
bool getEmbolden() { return _Embolden; } bool getEmbolden() { return _Embolden; }
@ -224,6 +227,7 @@ namespace NLGUI
REFLECT_EXPORT_START(CViewText, CViewBase) REFLECT_EXPORT_START(CViewText, CViewBase)
REFLECT_STRING("text_raw", getText, setText); REFLECT_STRING("text_raw", getText, setText);
REFLECT_STRING("hardtext", getHardText, setHardText); REFLECT_STRING("hardtext", getHardText, setHardText);
REFLECT_BOOL ("localize", isLocalized, setLocalized);
// REFLECT_UCSTRING("uc_text", getTextAsUtf16, setTextAsUtf16); // Deprecate uc_ functions // REFLECT_UCSTRING("uc_text", getTextAsUtf16, setTextAsUtf16); // Deprecate uc_ functions
REFLECT_UCSTRING("uc_hardtext", getHardTextAsUtf16, setHardTextAsUtf16); REFLECT_UCSTRING("uc_hardtext", getHardTextAsUtf16, setHardTextAsUtf16);
REFLECT_UCSTRING("uc_hardtext_format", getTextAsUtf16, setTextFormatTagedAsUtf16); // FIXME: Name doesn't make sense REFLECT_UCSTRING("uc_hardtext_format", getTextAsUtf16, setTextFormatTagedAsUtf16); // FIXME: Name doesn't make sense
@ -248,6 +252,7 @@ namespace NLGUI
std::string _HardText; std::string _HardText;
std::string _Text; std::string _Text;
mutable sint _TextLength; mutable sint _TextLength;
bool _Localized;
/// index of the computed String associated to this text control /// index of the computed String associated to this text control
uint _Index; uint _Index;
/// info on the computed String associated to this text control /// info on the computed String associated to this text control
@ -443,6 +448,8 @@ namespace NLGUI
void setup (); void setup ();
void setupDefault (); void setupDefault ();
void setTextLocalized(const std::string &text);
void setStringSelectionSkipingSpace(uint stringId, const std::string &text, sint charStart, sint charEnd); void setStringSelectionSkipingSpace(uint stringId, const std::string &text, sint charStart, sint charEnd);
// void pushString(const ucstring &str, bool deleteSpaceAtStart = false); // void pushString(const ucstring &str, bool deleteSpaceAtStart = false);

@ -597,12 +597,7 @@ namespace NLGUI
if (prop) if (prop)
{ {
const char *propPtr = prop; const char *propPtr = prop;
std::string text; _ViewText->setTextLocalized(propPtr, true);
if (NLMISC::startsWith(propPtr, "ui"))
text = CI18N::get(propPtr);
else
text = propPtr;
_ViewText->setText(text);
} }
} }

@ -2144,24 +2144,20 @@ namespace NLGUI
ptr = xmlGetProp (cur, (xmlChar*)"title"); ptr = xmlGetProp (cur, (xmlChar*)"title");
if (ptr) if (ptr)
{ {
if (_Localize) _TitleTextOpened = CI18N::get(string((const char*)ptr)); _TitleTextOpened = (const char *)ptr;
else _TitleTextOpened = string((const char*)ptr); _TitleTextClosed = (const char *)ptr;
if (_Localize) _TitleTextClosed = CI18N::get(string((const char*)ptr));
else _TitleTextClosed = string((const char*)ptr);
} }
ptr = xmlGetProp (cur, (xmlChar*)"title_opened"); ptr = xmlGetProp (cur, (xmlChar*)"title_opened");
if (ptr) if (ptr)
{ {
if (_Localize) _TitleTextOpened = CI18N::get(string((const char*)ptr)); _TitleTextOpened = (const char*)ptr;
else _TitleTextOpened = string((const char*)ptr);
} }
ptr = xmlGetProp (cur, (xmlChar*)"title_closed"); ptr = xmlGetProp (cur, (xmlChar*)"title_closed");
if (ptr) if (ptr)
{ {
if (_Localize) _TitleTextClosed = CI18N::get(string((const char*)ptr)); _TitleTextClosed = (const char *)ptr;
else _TitleTextClosed = string((const char*)ptr);
} }
ptr = xmlGetProp (cur, (xmlChar*)"header_active"); ptr = xmlGetProp (cur, (xmlChar*)"header_active");
@ -3950,8 +3946,14 @@ namespace NLGUI
void CGroupContainer::setLocalize(bool localize) void CGroupContainer::setLocalize(bool localize)
{ {
_Localize = localize; _Localize = localize;
setTitledOpenedViewText(); if (_TitleOpened)
setTitledClosedViewText(); {
_TitleOpened->setLocalized(localize);
}
if (_TitleClosed)
{
_TitleClosed->setLocalized(localize);
}
invalidateCoords(); invalidateCoords();
} }
@ -4001,10 +4003,7 @@ namespace NLGUI
{ {
if (_TitleOpened != NULL) if (_TitleOpened != NULL)
{ {
if (_Localize && NLMISC::startsWith(_TitleTextOpened, "ui")) _TitleOpened->setTextLocalized(_TitleTextOpened, _Localize);
_TitleOpened->setHardText(_TitleTextOpened);
else
_TitleOpened->setText(_TitleTextOpened);
} }
} }
@ -4013,10 +4012,7 @@ namespace NLGUI
{ {
if (_TitleClosed != NULL) if (_TitleClosed != NULL)
{ {
if (_Localize && NLMISC::startsWith(_TitleTextClosed, "ui")) _TitleClosed->setTextLocalized(_TitleTextClosed, _Localize);
_TitleClosed->setHardText(_TitleTextClosed);
else
_TitleClosed->setText(_TitleTextClosed);
} }
} }

@ -1580,7 +1580,7 @@ namespace NLGUI
_ViewText->setParent( this ); _ViewText->setParent( this );
_ViewText->setIdRecurse( "edit_text" ); _ViewText->setIdRecurse( "edit_text" );
_ViewText->setHardText( "" ); _ViewText->setTextLocalized( "", false );
_ViewText->setPosRef( Hotspot_ML ); _ViewText->setPosRef( Hotspot_ML );
_ViewText->setParentPosRef( Hotspot_ML ); _ViewText->setParentPosRef( Hotspot_ML );
addView( _ViewText ); addView( _ViewText );

@ -111,6 +111,8 @@ namespace NLGUI
_ClampRight = true; // clamp on the right of the text _ClampRight = true; // clamp on the right of the text
_OverflowText = "..."; _OverflowText = "...";
_Localized = true;
_LetterColors = NULL; _LetterColors = NULL;
_Setuped= false; _Setuped= false;
_AutoClampOffset = 0; _AutoClampOffset = 0;
@ -211,7 +213,7 @@ namespace NLGUI
_Index = 0xFFFFFFFF; _Index = 0xFFFFFFFF;
_ModulateGlobalColor= vt._ModulateGlobalColor; _ModulateGlobalColor= vt._ModulateGlobalColor;
_Localized = vt._Localized;
// remove previous lines // remove previous lines
clearLines(); clearLines();
@ -244,6 +246,11 @@ namespace NLGUI
std::string CViewText::getTextProperty( const std::string &name ) const std::string CViewText::getTextProperty( const std::string &name ) const
{ {
if( name == "localize" )
{
return toString(_Localized);
}
else
if( name == "color" ) if( name == "color" )
{ {
return toString( _Color ); return toString( _Color );
@ -418,6 +425,18 @@ namespace NLGUI
bool CViewText::setTextProperty( const std::string &name, const std::string &value ) bool CViewText::setTextProperty( const std::string &name, const std::string &value )
{ {
if( name == "localize" )
{
bool b;
if (fromString(value, b))
{
_Localized = b;
setTextLocalized(_HardText.empty() ? _Text : _HardText); // FIXME: setCase?
_TextLength = 0;
}
return true;
}
else
if( name == "color" ) if( name == "color" )
{ {
CRGBA c; CRGBA c;
@ -649,28 +668,19 @@ namespace NLGUI
return true; return true;
} }
else else
if( name == "hardtext" ) if( name == "text" )
{
#if 1
if (NLMISC::startsWith(value, "ui"))
{ {
_Text = CI18N::get(value); setTextLocalized(value); // FIXME: setCase?
_TextLength = 0; _TextLength = 0;
_HardText = value; invalidateContent();
return true;
} }
else else
if( name == "hardtext" )
{ {
_Text = value; _Localized = true;
_TextLength = 0; setTextLocalized(value); // FIXME: setCase?
_HardText.clear();
}
#else
_Text = value;
_TextLength = 0; _TextLength = 0;
_HardText.clear();
if (NLMISC::startsWith(value, "ui"))
_HardText = _Text;
#endif
invalidateContent(); invalidateContent();
return true; return true;
} }
@ -705,6 +715,8 @@ namespace NLGUI
bool CViewText::serializeTextOptions( xmlNodePtr node ) const bool CViewText::serializeTextOptions( xmlNodePtr node ) const
{ {
xmlSetProp( node, BAD_CAST "localize", BAD_CAST toString( _Localized ).c_str() );
xmlSetProp( node, BAD_CAST "color", BAD_CAST toString( _Color ).c_str() ); xmlSetProp( node, BAD_CAST "color", BAD_CAST toString( _Color ).c_str() );
xmlSetProp( node, BAD_CAST "global_color", BAD_CAST toString( _ModulateGlobalColor ).c_str() ); xmlSetProp( node, BAD_CAST "global_color", BAD_CAST toString( _ModulateGlobalColor ).c_str() );
@ -784,7 +796,7 @@ namespace NLGUI
serializeTextOptions( node ); serializeTextOptions( node );
xmlSetProp( node, BAD_CAST "hardtext", BAD_CAST _Text.c_str() ); xmlSetProp( node, BAD_CAST "text", BAD_CAST (_HardText.empty() ? _Text.c_str() : _HardText.c_str()) );
xmlSetProp( node, BAD_CAST "hardtext_format", BAD_CAST _HardTextFormat.c_str() ); xmlSetProp( node, BAD_CAST "hardtext_format", BAD_CAST _HardTextFormat.c_str() );
return node; return node;
@ -795,6 +807,9 @@ namespace NLGUI
{ {
CXMLAutoPtr prop; CXMLAutoPtr prop;
prop = xmlGetProp (cur, (xmlChar*)"localize");
if (prop) _Localized = convertBool((const char*)prop);
prop= (char*) xmlGetProp( cur, (xmlChar*)"color" ); prop= (char*) xmlGetProp( cur, (xmlChar*)"color" );
_Color = CRGBA(255,255,255,255); _Color = CRGBA(255,255,255,255);
if (prop) if (prop)
@ -1000,18 +1015,8 @@ namespace NLGUI
if (prop) if (prop)
{ {
const char *propPtr = prop; const char *propPtr = prop;
if (NLMISC::startsWith(propPtr, "ui")) _Localized = true;
{ setTextLocalized(propPtr);
_HardText = propPtr;
_Text = CI18N::get(propPtr);
_TextLength = 0;
}
else
{
_HardText.clear();
_Text = propPtr;
_TextLength = 0;
}
setCase(_Text, _CaseMode); setCase(_Text, _CaseMode);
_TextLength = 0; _TextLength = 0;
} }
@ -1427,18 +1432,70 @@ namespace NLGUI
} }
// *************************************************************************** // ***************************************************************************
void CViewText::setText(const std::string &text) void CViewText::setTextLocalized(const std::string &text, bool localized)
{
if (localized != _Localized)
{
_Localized = localized;
// Always recompute if localization and text changed
setTextLocalized(text);
setCase(_Text, _CaseMode);
_TextLength = 0;
invalidateContent();
}
else
{
setText(text);
}
}
// ***************************************************************************
void CViewText::setLocalized(bool localized)
{
if (localized != _Localized)
{
const std::string &text = _HardText.empty() ? _Text : _HardText;
_Localized = localized;
if (!text.empty() && NLMISC::startsWith(text, "ui"))
{
setTextLocalized(text);
setCase(_Text, _CaseMode);
_TextLength = 0;
invalidateContent();
}
}
nlassert(_Text.empty() || ((_Localized && (NLMISC::startsWith(getText(), "ui"))) == (_HardText.empty() == _Text.empty())));
}
// ***************************************************************************
void CViewText::setTextLocalized(const std::string &text)
{
if (_Localized && NLMISC::startsWith(text, "ui"))
{
_HardText = text;
_Text = CI18N::get(text);
}
else
{ {
_Text = text;
_HardText.clear(); _HardText.clear();
}
}
// ***************************************************************************
void CViewText::setText(const std::string &text)
{
// common case: no special format, no case mode => easy cache test // common case: no special format, no case mode => easy cache test
if (_FormatTags.empty() && _CaseMode==CaseNormal) if (_FormatTags.empty() && _CaseMode == CaseNormal)
{ {
if (text != _Text) if (_HardText.empty() ? text != _Text : text != _HardText)
{ {
_Text = text; setTextLocalized(text);
_TextLength = 0; _TextLength = 0;
// no need to call "setCase (_Text, _CaseMode);" since CaseNormal: // no need to call "setCase (_Text, _CaseMode);" since CaseNormal:
invalidateContent (); invalidateContent();
} }
} }
else else
@ -1446,26 +1503,34 @@ namespace NLGUI
// if the view text had some format before, no choice, must recompute all // if the view text had some format before, no choice, must recompute all
if (!_FormatTags.empty()) if (!_FormatTags.empty())
{ {
_Text = text; setTextLocalized(text);
setCase (_Text, _CaseMode); setCase(_Text, _CaseMode);
_TextLength = 0; _TextLength = 0;
invalidateContent (); invalidateContent();
} }
// else test if after the case change the cache succeed // else test if after the case change the cache succeed
else else
{ {
// compute the temp cased text // compute the temp cased text
std::string tempText = text; std::string holdText, holdHardText;
setCase (tempText, _CaseMode); holdText.swap(_Text);
if (tempText != _Text) holdHardText.swap(_HardText);
setTextLocalized(text);
setCase(_Text, _CaseMode);
if (holdText != _Text)
{ {
_Text = tempText;
_TextLength = 0; _TextLength = 0;
invalidateContent(); invalidateContent();
} }
else
{
holdText.swap(_Text);
}
} }
} }
nlassert(_Text.empty() || ((_Localized && (NLMISC::startsWith(text, "ui"))) == (_HardText.empty() == _Text.empty())));
// clear format tags if any // clear format tags if any
_FormatTags.clear(); _FormatTags.clear();
} }
@ -2396,16 +2461,13 @@ namespace NLGUI
// *************************************************************************** // ***************************************************************************
void CViewText::setHardText (const std::string &ht) void CViewText::setHardText (const std::string &ht)
{ {
if (NLMISC::startsWith(ht, "ui")) if (!_Localized)
{ {
setText(CI18N::get(ht)); setText(std::string());
_HardText = ht; _Localized = true;
} }
else
{
setText(ht); setText(ht);
} }
}
// *************************************************************************** // ***************************************************************************
ucstring CViewText::getTextAsUtf16() const ucstring CViewText::getTextAsUtf16() const
@ -3367,6 +3429,7 @@ namespace NLGUI
// Copy to Text (preserve Memory) // Copy to Text (preserve Memory)
contReset(_Text); contReset(_Text);
_Text = tempText; _Text = tempText;
_HardText.clear();
_TextLength = 0; _TextLength = 0;
CInterfaceGroup *parent = getParent(); CInterfaceGroup *parent = getParent();
@ -3477,6 +3540,7 @@ namespace NLGUI
// Copy to Text (preserve Memory) // Copy to Text (preserve Memory)
contReset(_Text); contReset(_Text);
_Text = tempText; _Text = tempText;
_HardText.clear();
_TextLength = 0; _TextLength = 0;
invalidateContent (); invalidateContent ();
} }
@ -3560,6 +3624,11 @@ namespace NLGUI
#define SERIAL_UINT(val) { uint32 tmp = (uint32) val; f.serial(tmp); val = (uint) tmp; } #define SERIAL_UINT(val) { uint32 tmp = (uint32) val; f.serial(tmp); val = (uint) tmp; }
#define SERIAL_SINT(val) { sint32 tmp = (sint32) val; f.serial(tmp); val = (sint) tmp; } #define SERIAL_SINT(val) { sint32 tmp = (sint32) val; f.serial(tmp); val = (sint) tmp; }
CViewBase::serial(f); CViewBase::serial(f);
int version = f.serialVersion(101);
nlassert(version >= 100);
f.serial(_Localized);
SERIAL_SINT(_FontSize); SERIAL_SINT(_FontSize);
SERIAL_UINT(_FontWidth); SERIAL_UINT(_FontWidth);
SERIAL_UINT(_FontHeight); SERIAL_UINT(_FontHeight);

@ -1195,6 +1195,7 @@ namespace NLGUI
sint32 backupX = groupOver->getX(); sint32 backupX = groupOver->getX();
// Copy all aspects to the view // Copy all aspects to the view
vtDst->setLocalized (vtSrc->isLocalized());
vtDst->setText (vtSrc->getText()); vtDst->setText (vtSrc->getText());
vtDst->setFontSize (vtSrc->getFontSize()); vtDst->setFontSize (vtSrc->getFontSize());
vtDst->setColor (vtSrc->getColor()); vtDst->setColor (vtSrc->getColor());

Loading…
Cancel
Save