Make a simple interface class for localizers. Move the theme-based

localizer into ThemeManager.  Use simple MI to hook ThemeMetric::Read
to continue to automatically update when needed.
This commit is contained in:
Glenn Maynard
2006-01-13 00:40:12 +00:00
parent c827c9ec1e
commit 269c6f0ec7
3 changed files with 87 additions and 22 deletions
+43 -20
View File
@@ -1,51 +1,74 @@
#include "global.h" #include "global.h"
#include "LocalizedString.h" #include "LocalizedString.h"
#include "Foreach.h" #include "Foreach.h"
#include "LuaFunctions.h"
#include "LuaManager.h"
#include "ThemeMetric.h"
#include "RageUtil.h" #include "RageUtil.h"
#include "SubscriptionManager.h"
class LocalizedStringImpl : public ThemeMetric<RString> static SubscriptionManager<LocalizedString> m_Subscribers;
class LocalizedStringImplDefault: public ILocalizedStringImpl
{ {
public: public:
LocalizedStringImpl( const RString& sGroup, const RString& sName ) : static ILocalizedStringImpl *Create() { return new LocalizedStringImplDefault; }
ThemeMetric<RString>( sGroup, sName )
void ILocalizedStringImpl::Load( const RString& sGroup, const RString& sName )
{ {
// Ugly. Our virtual method Read() isn't yet set up when Read gets m_sValue = sName;
// called through the constructor. Read again explicitly.
Read();
} }
virtual void Read() const RString &GetLocalized() const { return m_sValue; }
{
if( m_sName != "" && THEME && THEME->IsThemeLoaded() )
{
THEME->GetString( m_sGroup, m_sName, m_currentValue );
m_bIsLoaded = true;
}
}
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 ) 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() LocalizedString::~LocalizedString()
{ {
m_Subscribers.Unsubscribe( this );
SAFE_DELETE( m_pImpl ); 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 ) 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 const RString &LocalizedString::GetValue() const
{ {
return m_pImpl->GetValue(); return m_pImpl->GetLocalized();
} }
/* /*
+13 -2
View File
@@ -3,7 +3,13 @@
#ifndef LocalizedString_H #ifndef LocalizedString_H
#define 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 class LocalizedString
{ {
@@ -14,8 +20,13 @@ public:
operator const RString &() const { return GetValue(); } operator const RString &() const { return GetValue(); }
const RString &GetValue() const; const RString &GetValue() const;
typedef ILocalizedStringImpl *(*MakeLocalizer)();
static void RegisterLocalizer( MakeLocalizer pFunc );
private: private:
LocalizedStringImpl *m_pImpl; void CreateImpl();
CString m_sGroup, m_sName;
ILocalizedStringImpl *m_pImpl;
}; };
#endif #endif
+31
View File
@@ -52,6 +52,34 @@ static IniFile *g_pIniMetrics;
#include "SubscriptionManager.h" #include "SubscriptionManager.h"
static SubscriptionManager<IThemeMetric> g_Subscribers; static SubscriptionManager<IThemeMetric> g_Subscribers;
class LocalizedStringImplThemeMetric : public ILocalizedStringImpl, public ThemeMetric<RString>
{
public:
static ILocalizedStringImpl *Create() { return new LocalizedStringImplThemeMetric; }
void ILocalizedStringImpl::Load( const RString& sGroup, const RString& sName )
{
ThemeMetric<RString>::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 ) void ThemeManager::Subscribe( IThemeMetric *p )
{ {
g_Subscribers.Subscribe( p ); g_Subscribers.Subscribe( p );
@@ -318,6 +346,9 @@ void ThemeManager::SwitchThemeAndLanguage( const RString &sThemeName_, const RSt
UpdateLuaGlobals(); UpdateLuaGlobals();
} }
// Use theme metrics for localization.
LocalizedString::RegisterLocalizer( LocalizedStringImplThemeMetric::Create );
// reload subscribers // reload subscribers
if( g_Subscribers.m_pSubscribers ) if( g_Subscribers.m_pSubscribers )
{ {