Skip to content

Commit

Permalink
#5231: Start reworking the LanguageManager framework. The goal is to …
Browse files Browse the repository at this point in the history
…create a globally available LanguageManager in the core binary, which the UI module can attach to.
  • Loading branch information
codereader committed Aug 14, 2020
1 parent 850a0a1 commit ff12745
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
54 changes: 48 additions & 6 deletions include/i18n.h
@@ -1,23 +1,65 @@
#pragma once

#include <string>
#include "imodule.h"

class ILanguageManager :
public RegisterableModule
{
public:
virtual ~ILanguageManager() {}

/**
* Returns the localized version of the given input string
* or the unmodified string if no suitable localization provider
* was found.
*/
virtual std::string getLocalizedString(const char* stringToLocalise) = 0;
};

const char* const MODULE_LANGUAGEMANAGER("LanguageManager");

// This is the accessor for the global language manager module
inline ILanguageManager& GlobalLanguageManager()
{
// Cache the reference locally
static ILanguageManager& _instance(
*std::static_pointer_cast<ILanguageManager>(
module::GlobalModuleRegistry().getModule(MODULE_LANGUAGEMANAGER)
)
);
return _instance;
}

#define GETTEXT_PACKAGE "darkradiant"

// Redefine the _() macro to return a std::string for convenience
#ifndef WXINTL_NO_GETTEXT_MACRO
#define WXINTL_NO_GETTEXT_MACRO
#endif
#endif

#include <wx/intl.h>
#include <string>
//#include <wx/intl.h>

// Custom translation macros
// Custom translation macros _, N_ and C_

inline std::string _(const char* s)
{
return wxGetTranslation((s)).ToStdString();
if (!module::IsGlobalModuleRegistryAvailable())
{
return s; // it's still too early for this call, return the unmodified string
}

if (!module::GlobalModuleRegistry().moduleExists(MODULE_LANGUAGEMANAGER))
{
return s; // still too early
}

return GlobalLanguageManager().getLocalizedString(s);
}

//#define _(s) (wxGetTranslation((s)).ToStdString())
// Macro used to decorate a string as localizable, such that it is recognised
// by the xgettext parser, but without triggering an actual function call
// at the place it is used. Can be used to e.g. decorate constants in the code.
#define N_(str) str

#ifndef C_
Expand Down
24 changes: 19 additions & 5 deletions include/imodule.h
Expand Up @@ -340,19 +340,27 @@ namespace module
IModuleRegistry* _registry;
public:
RegistryReference() :
_registry(NULL)
_registry(nullptr)
{}

inline void setRegistry(IModuleRegistry& registry) {
inline void setRegistry(IModuleRegistry& registry)
{
_registry = &registry;
}

inline IModuleRegistry& getRegistry() {
assert(_registry); // must not be NULL
inline IModuleRegistry& getRegistry() const
{
assert(_registry); // must not be empty
return *_registry;
}

static RegistryReference& Instance() {
inline bool isEmpty() const
{
return _registry == nullptr;
}

static RegistryReference& Instance()
{
static RegistryReference _registryRef;
return _registryRef;
}
Expand All @@ -366,6 +374,12 @@ namespace module
return RegistryReference::Instance().getRegistry();
}

// Returns true if we have a registry instance known to this binary
inline bool IsGlobalModuleRegistryAvailable()
{
return !RegistryReference::Instance().isEmpty();
}

// Exception thrown if the module being loaded is incompatible with the main binary
class ModuleCompatibilityException :
public std::runtime_error
Expand Down
8 changes: 7 additions & 1 deletion radiantcore/settings/LanguageManager.cpp
Expand Up @@ -14,6 +14,7 @@
#include "os/file.h"
#include "os/fs.h"
#include "registry/registry.h"
#include "module/StaticModule.h"
#include "string/case_conv.h"

namespace language
Expand Down Expand Up @@ -162,7 +163,7 @@ void LanguageManager::init(const ApplicationContext& ctx)
// Hand that over to the module registry
module::GlobalModuleRegistry().registerModule(instancePtr);

// Initialise the module manually
// Pre-initialise the module manually
instancePtr->initFromContext(ctx);
}

Expand All @@ -185,6 +186,11 @@ void LanguageManager::initFromContext(const ApplicationContext& ctx)
_wxLocale->AddCatalog(GETTEXT_PACKAGE);
}

std::string LanguageManager::getLocalizedString(const char* stringToLocalise)
{
return stringToLocalise; // TODO
}

std::string LanguageManager::loadLanguageSetting()
{
std::string language = DEFAULT_LANGUAGE;
Expand Down
5 changes: 4 additions & 1 deletion radiantcore/settings/LanguageManager.h
Expand Up @@ -2,6 +2,7 @@

#include "iregistry.h"
#include "imodule.h"
#include "i18n.h"
#include <vector>
#include <map>
#include <memory>
Expand All @@ -12,7 +13,7 @@ namespace language
{

class LanguageManager :
public RegisterableModule
public ILanguageManager
{
private:
// The current language string (e.g. "en_US")
Expand Down Expand Up @@ -60,6 +61,8 @@ class LanguageManager :
// This is called by main() even before any other modules are loaded
static void init(const ApplicationContext& ctx);

std::string getLocalizedString(const char* stringToLocalise) override;

private:
void initFromContext(const ApplicationContext& ctx);

Expand Down

0 comments on commit ff12745

Please sign in to comment.