carb/l10n/IL10n.h

File members: carb/l10n/IL10n.h

// Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#pragma once
#include "../Defines.h"

#include "../logging/Log.h"

namespace carb
{
namespace l10n
{

using StringIdentifier = uint64_t;

struct LanguageTable
{
};

struct LanguageIdentifier
{
};

const LanguageTable* const kLanguageTableMain = nullptr;

const LanguageIdentifier* const kLanguageCurrent = nullptr;

using localizeStringFn = const char*(CARB_ABI*)(const LanguageTable* table,
                                                StringIdentifier id,
                                                const LanguageIdentifier* language);

const LanguageIdentifier* const kLanguageDefault = reinterpret_cast<const LanguageIdentifier*>(0xFFFFFFFFFFFFFFFF);

const LanguageIdentifier* const kLanguageUnknown = reinterpret_cast<const LanguageIdentifier*>(0xFFFFFFFFFFFFFFFE);

struct LanguageTableData
{
    size_t languagesLength;

    size_t keysLength;

    const char* const* languages;

    const uint64_t* keys;

    const char* const* languageTable;
};

enum class LocalizedName
{
    eUsEnglish,

    eLocalized,
};

struct IL10n
{
    CARB_PLUGIN_INTERFACE("carb::l10n::IL10n", 1, 0)

    StringIdentifier(CARB_ABI* getHashFromKeyString)(const char* keyString) noexcept;

    const char*(CARB_ABI* getLocalizedStringFromHash)(const LanguageTable* table,
                                                      StringIdentifier id,
                                                      const LanguageIdentifier* language) noexcept;

    const LanguageIdentifier*(CARB_ABI* getSystemLanguage)() noexcept;

    const LanguageIdentifier*(CARB_ABI* enumLanguageIdentifiers)(const LanguageTable* table, size_t index) noexcept;

    const LanguageIdentifier*(CARB_ABI* getLanguageIdentifier)(const LanguageTable* table, const char* language) noexcept;

    const char*(CARB_ABI* getLanguageName)(const LanguageTable* table,
                                           const LanguageIdentifier* language,
                                           LocalizedName retrieveIn) noexcept;

    const char*(CARB_ABI* getTerritoryName)(const LanguageTable* table,
                                            const LanguageIdentifier* language,
                                            LocalizedName retrieveIn) noexcept;

    const char*(CARB_ABI* getLocaleIdentifierName)(const LanguageTable* table,
                                                   const LanguageIdentifier* language) noexcept;

    bool(CARB_ABI* setCurrentLanguage)(const LanguageTable* table, const LanguageIdentifier* language) noexcept;

    bool(CARB_ABI* setCurrentLanguageFromString)(const LanguageTable* table, const char* language) noexcept;

    const LanguageIdentifier*(CARB_ABI* getCurrentLanguage)() noexcept;

    bool(CARB_ABI* setMainLanguageTable)(const LanguageTable* table) noexcept;

    LanguageTable*(CARB_ABI* loadLanguageTable)(const LanguageTableData* data) noexcept;

    LanguageTable*(CARB_ABI* loadLanguageTableFromFile)(const char* fileName) noexcept;

    void(CARB_ABI* unloadLanguageTable)(LanguageTable* table) noexcept;

    bool(CARB_ABI* addLanguageSearchPath)(const char* searchPath) noexcept;

    bool(CARB_ABI* removeLanguageSearchPath)(const char* searchPath) noexcept;

    const char*(CARB_ABI* enumLanguageSearchPaths)(size_t index) noexcept;
};

inline const char* CARB_ABI getLocalizedStringFromHashNoPlugin(const LanguageTable* table,
                                                               StringIdentifier id,
                                                               const LanguageIdentifier* language) noexcept
{
    CARB_UNUSED(table, id, language);
    CARB_LOG_ERROR("localization is being used with carb.l10n.plugin not loaded");
    return nullptr;
}

} // namespace l10n
} // namespace carb

CARB_WEAKLINK CARB_HIDDEN carb::l10n::IL10n* g_carbLocalization;

CARB_WEAKLINK CARB_HIDDEN carb::l10n::localizeStringFn g_localizationFn = carb::l10n::getLocalizedStringFromHashNoPlugin;

/* Exhale can't handle these for some reason and they aren't meant to be used directly */
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace carb
{
namespace l10n
{
inline const char* getLocalizedString(StringIdentifier id, const char* string) noexcept
{
    const char* s = g_localizationFn(kLanguageTableMain, id, kLanguageCurrent);
    return (s != nullptr) ? s : string;
}

} // namespace l10n
} // namespace carb
#endif

#define CARB_LOCALIZE(string) carb::l10n::getLocalizedString(CARB_HASH_STRING(string), string)