diff --git a/stepmania/src/LocalizedString.cpp b/stepmania/src/LocalizedString.cpp index fd5199abfe..acc7fc96b0 100644 --- a/stepmania/src/LocalizedString.cpp +++ b/stepmania/src/LocalizedString.cpp @@ -1,51 +1,74 @@ #include "global.h" #include "LocalizedString.h" #include "Foreach.h" -#include "LuaFunctions.h" -#include "LuaManager.h" -#include "ThemeMetric.h" #include "RageUtil.h" +#include "SubscriptionManager.h" -class LocalizedStringImpl : public ThemeMetric +static SubscriptionManager m_Subscribers; + +class LocalizedStringImplDefault: public ILocalizedStringImpl { public: - LocalizedStringImpl( const RString& sGroup, const RString& sName ) : - ThemeMetric( sGroup, sName ) + static ILocalizedStringImpl *Create() { return new LocalizedStringImplDefault; } + + void ILocalizedStringImpl::Load( const RString& sGroup, const RString& sName ) { - // Ugly. Our virtual method Read() isn't yet set up when Read gets - // called through the constructor. Read again explicitly. - Read(); + m_sValue = sName; } - virtual void Read() - { - if( m_sName != "" && THEME && THEME->IsThemeLoaded() ) - { - THEME->GetString( m_sGroup, m_sName, m_currentValue ); - m_bIsLoaded = true; - } - } + const RString &GetLocalized() const { return m_sValue; } +private: + CString m_sValue; }; +static LocalizedString::MakeLocalizer g_pMakeLocalizedStringImpl = LocalizedStringImplDefault::Create; + +void LocalizedString::RegisterLocalizer( MakeLocalizer pFunc ) +{ + g_pMakeLocalizedStringImpl = pFunc; + FOREACHS( LocalizedString*, *m_Subscribers.m_pSubscribers, l ) + { + LocalizedString *pLoc = *l; + pLoc->CreateImpl(); + } +} + LocalizedString::LocalizedString( const RString& sGroup, const RString& sName ) { - m_pImpl = new LocalizedStringImpl(sGroup,sName); + m_Subscribers.Subscribe( this ); + + m_sGroup = sGroup; + m_sName = sName; + m_pImpl = NULL; + + CreateImpl(); } LocalizedString::~LocalizedString() { + m_Subscribers.Unsubscribe( this ); + SAFE_DELETE( m_pImpl ); } +void LocalizedString::CreateImpl() +{ + SAFE_DELETE( m_pImpl ); + m_pImpl = g_pMakeLocalizedStringImpl(); + m_pImpl->Load( m_sGroup, m_sName ); +} + void LocalizedString::Load( const RString& sGroup, const RString& sName ) { - m_pImpl->Load( sGroup, sName ); + m_sGroup = sGroup; + m_sName = sName; + CreateImpl(); } const RString &LocalizedString::GetValue() const { - return m_pImpl->GetValue(); + return m_pImpl->GetLocalized(); } /* diff --git a/stepmania/src/LocalizedString.h b/stepmania/src/LocalizedString.h index 6e06a308d2..bf03c19912 100644 --- a/stepmania/src/LocalizedString.h +++ b/stepmania/src/LocalizedString.h @@ -3,7 +3,13 @@ #ifndef LocalizedString_H #define LocalizedString_H -class LocalizedStringImpl; +class ILocalizedStringImpl +{ +public: + virtual ~ILocalizedStringImpl() { } + virtual void Load( const RString& sGroup, const RString& sName ) = 0; + virtual const RString &GetLocalized() const = 0; +}; class LocalizedString { @@ -14,8 +20,13 @@ public: operator const RString &() const { return GetValue(); } const RString &GetValue() const; + typedef ILocalizedStringImpl *(*MakeLocalizer)(); + static void RegisterLocalizer( MakeLocalizer pFunc ); + private: - LocalizedStringImpl *m_pImpl; + void CreateImpl(); + CString m_sGroup, m_sName; + ILocalizedStringImpl *m_pImpl; }; #endif diff --git a/stepmania/src/ThemeManager.cpp b/stepmania/src/ThemeManager.cpp index 8657e4f1a3..4dd016b0e3 100644 --- a/stepmania/src/ThemeManager.cpp +++ b/stepmania/src/ThemeManager.cpp @@ -52,6 +52,34 @@ static IniFile *g_pIniMetrics; #include "SubscriptionManager.h" static SubscriptionManager g_Subscribers; +class LocalizedStringImplThemeMetric : public ILocalizedStringImpl, public ThemeMetric +{ +public: + static ILocalizedStringImpl *Create() { return new LocalizedStringImplThemeMetric; } + + void ILocalizedStringImpl::Load( const RString& sGroup, const RString& sName ) + { + ThemeMetric::Load( sGroup, sName ); + } + + virtual void Read() + { + if( m_sName != "" && THEME && THEME->IsThemeLoaded() ) + { + THEME->GetString( m_sGroup, m_sName, m_currentValue ); + m_bIsLoaded = true; + } + } + + const RString &GetLocalized() const + { + if( IsLoaded() ) + return GetValue(); + else + return m_sName; + } +}; + void ThemeManager::Subscribe( IThemeMetric *p ) { g_Subscribers.Subscribe( p ); @@ -318,6 +346,9 @@ void ThemeManager::SwitchThemeAndLanguage( const RString &sThemeName_, const RSt UpdateLuaGlobals(); } + // Use theme metrics for localization. + LocalizedString::RegisterLocalizer( LocalizedStringImplThemeMetric::Create ); + // reload subscribers if( g_Subscribers.m_pSubscribers ) {