Skip to content

Commit ffe0abb

Browse files
committed
Internals: added basic localization system (#5895)
1 parent c3d9f8e commit ffe0abb

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

imgui.cpp

+33-7
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ CODE
8080
// [SECTION] DRAG AND DROP
8181
// [SECTION] LOGGING/CAPTURING
8282
// [SECTION] SETTINGS
83-
// [SECTION] VIEWPORTS
83+
// [SECTION] LOCALIZATION
84+
// [SECTION] VIEWPORTS, PLATFORM WINDOWS
8485
// [SECTION] PLATFORM DEPENDENT HELPERS
8586
// [SECTION] METRICS/DEBUGGER WINDOW
8687
// [SECTION] DEBUG LOG WINDOW
@@ -4772,12 +4773,24 @@ void ImGui::NewFrame()
47724773
CallContextHooks(&g, ImGuiContextHookType_NewFramePost);
47734774
}
47744775

4776+
// IMPORTANT: ###xxx suffixes must be same in ALL languages
4777+
static const ImGuiLocEntry GLocalizationEntriesEnUS[] =
4778+
{
4779+
{ ImGuiLocKey_TableSizeOne, "Size column to fit###SizeOne" },
4780+
{ ImGuiLocKey_TableSizeAllFit, "Size all columns to fit###SizeAll" },
4781+
{ ImGuiLocKey_TableSizeAllDefault, "Size all columns to default###SizeAll" },
4782+
{ ImGuiLocKey_TableResetOrder, "Reset order###ResetOrder" },
4783+
{ ImGuiLocKey_WindowingMainMenuBar, "(Main menu bar)" },
4784+
{ ImGuiLocKey_WindowingPopup, "(Popup)" },
4785+
{ ImGuiLocKey_WindowingUntitled, "(Untitled)" },
4786+
};
4787+
47754788
void ImGui::Initialize()
47764789
{
47774790
ImGuiContext& g = *GImGui;
47784791
IM_ASSERT(!g.Initialized && !g.SettingsLoaded);
47794792

4780-
// Add .ini handle for ImGuiWindow type
4793+
// Add .ini handle for ImGuiWindow and ImGuiTable types
47814794
{
47824795
ImGuiSettingsHandler ini_handler;
47834796
ini_handler.TypeName = "Window";
@@ -4789,10 +4802,11 @@ void ImGui::Initialize()
47894802
ini_handler.WriteAllFn = WindowSettingsHandler_WriteAll;
47904803
AddSettingsHandler(&ini_handler);
47914804
}
4792-
4793-
// Add .ini handle for ImGuiTable type
47944805
TableSettingsAddSettingsHandler();
47954806

4807+
// Setup default localization table
4808+
LocalizeRegisterEntries(GLocalizationEntriesEnUS, IM_ARRAYSIZE(GLocalizationEntriesEnUS));
4809+
47964810
// Create default viewport
47974811
ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)();
47984812
g.Viewports.push_back(viewport);
@@ -11660,10 +11674,10 @@ static void ImGui::NavUpdateWindowing()
1166011674
static const char* GetFallbackWindowNameForWindowingList(ImGuiWindow* window)
1166111675
{
1166211676
if (window->Flags & ImGuiWindowFlags_Popup)
11663-
return "(Popup)";
11677+
return ImGui::LocalizeGetMsg(ImGuiLocKey_WindowingPopup);
1166411678
if ((window->Flags & ImGuiWindowFlags_MenuBar) && strcmp(window->Name, "##MainMenuBar") == 0)
11665-
return "(Main menu bar)";
11666-
return "(Untitled)";
11679+
return ImGui::LocalizeGetMsg(ImGuiLocKey_WindowingMainMenuBar);
11680+
return ImGui::LocalizeGetMsg(ImGuiLocKey_WindowingUntitled);
1166711681
}
1166811682

1166911683
// Overlay displayed when using CTRL+TAB. Called by EndFrame().
@@ -12593,6 +12607,18 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl
1259312607
}
1259412608

1259512609

12610+
//-----------------------------------------------------------------------------
12611+
// [SECTION] LOCALIZATION
12612+
//-----------------------------------------------------------------------------
12613+
12614+
void ImGui::LocalizeRegisterEntries(const ImGuiLocEntry* entries, int count)
12615+
{
12616+
ImGuiContext& g = *GImGui;
12617+
for (int n = 0; n < count; n++)
12618+
g.LocalizationTable[entries[n].Key] = entries[n].Text;
12619+
}
12620+
12621+
1259612622
//-----------------------------------------------------------------------------
1259712623
// [SECTION] VIEWPORTS, PLATFORM WINDOWS
1259812624
//-----------------------------------------------------------------------------

imgui_internal.h

+39
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Index of this file:
2626
// [SECTION] Docking support
2727
// [SECTION] Viewport support
2828
// [SECTION] Settings support
29+
// [SECTION] Localization support
2930
// [SECTION] Metrics, Debug tools
3031
// [SECTION] Generic context hooks
3132
// [SECTION] ImGuiContext (main imgui context)
@@ -121,6 +122,7 @@ struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDat
121122
struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup()
122123
struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box
123124
struct ImGuiLastItemData; // Status storage for last submitted items
125+
struct ImGuiLocEntry; // A localization entry.
124126
struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only
125127
struct ImGuiNavItemData; // Result of a gamepad/keyboard directional navigation move query result
126128
struct ImGuiMetricsConfig; // Storage for ShowMetricsWindow() and DebugNodeXXX() functions
@@ -144,8 +146,12 @@ struct ImGuiWindow; // Storage for one window
144146
struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame, in practice we currently keep it for each window)
145147
struct ImGuiWindowSettings; // Storage for a window .ini settings (we keep one of those even if the actual window wasn't instanced during this session)
146148

149+
// Enumerations
147150
// Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists.
151+
enum ImGuiLocKey : int; // -> enum ImGuiLocKey // Enum: a localization entry for translation.
148152
typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical
153+
154+
// Flags
149155
typedef int ImGuiActivateFlags; // -> enum ImGuiActivateFlags_ // Flags: for navigation/focus function (will be for ActivateItem() later)
150156
typedef int ImGuiDebugLogFlags; // -> enum ImGuiDebugLogFlags_ // Flags: for ShowDebugLogWindow(), g.DebugLogFlags
151157
typedef int ImGuiInputFlags; // -> enum ImGuiInputFlags_ // Flags: for IsKeyPressed(), IsMouseClicked(), SetKeyOwner(), SetItemKeyOwner() etc.
@@ -1608,6 +1614,30 @@ struct ImGuiSettingsHandler
16081614
ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); }
16091615
};
16101616

1617+
//-----------------------------------------------------------------------------
1618+
// [SECTION] Localization support
1619+
//-----------------------------------------------------------------------------
1620+
1621+
// This is experimental and not officially supported, it'll probably fall short of features, if/when it does we may backtrack.
1622+
enum ImGuiLocKey : int
1623+
{
1624+
ImGuiLocKey_TableSizeOne,
1625+
ImGuiLocKey_TableSizeAllFit,
1626+
ImGuiLocKey_TableSizeAllDefault,
1627+
ImGuiLocKey_TableResetOrder,
1628+
ImGuiLocKey_WindowingMainMenuBar,
1629+
ImGuiLocKey_WindowingPopup,
1630+
ImGuiLocKey_WindowingUntitled,
1631+
ImGuiLocKey_COUNT
1632+
};
1633+
1634+
struct ImGuiLocEntry
1635+
{
1636+
ImGuiLocKey Key;
1637+
const char* Text;
1638+
};
1639+
1640+
16111641
//-----------------------------------------------------------------------------
16121642
// [SECTION] Metrics, Debug Tools
16131643
//-----------------------------------------------------------------------------
@@ -1938,6 +1968,9 @@ struct ImGuiContext
19381968
ImVector<ImGuiContextHook> Hooks; // Hooks for extensions (e.g. test engine)
19391969
ImGuiID HookIdNext; // Next available HookId
19401970

1971+
// Localization
1972+
const char* LocalizationTable[ImGuiLocKey_COUNT];
1973+
19411974
// Capture/Logging
19421975
bool LogEnabled; // Currently capturing
19431976
ImGuiLogType LogType; // Capture target
@@ -2109,6 +2142,8 @@ struct ImGuiContext
21092142
SettingsDirtyTimer = 0.0f;
21102143
HookIdNext = 0;
21112144

2145+
memset(LocalizationTable, 0, sizeof(LocalizationTable));
2146+
21122147
LogEnabled = false;
21132148
LogType = ImGuiLogType_None;
21142149
LogNextPrefix = LogNextSuffix = NULL;
@@ -2725,6 +2760,10 @@ namespace ImGui
27252760
IMGUI_API void RemoveSettingsHandler(const char* type_name);
27262761
IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name);
27272762

2763+
// Localization
2764+
IMGUI_API void LocalizeRegisterEntries(const ImGuiLocEntry* entries, int count);
2765+
inline const char* LocalizeGetMsg(ImGuiLocKey key) { ImGuiContext& g = *GImGui; const char* msg = g.LocalizationTable[key]; return msg ? msg : "*Missing Text*"; }
2766+
27282767
// Scrolling
27292768
IMGUI_API void SetScrollX(ImGuiWindow* window, float scroll_x);
27302769
IMGUI_API void SetScrollY(ImGuiWindow* window, float scroll_y);

imgui_tables.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -3070,15 +3070,15 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table)
30703070
if (column != NULL)
30713071
{
30723072
const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsEnabled;
3073-
if (MenuItem("Size column to fit###SizeOne", NULL, false, can_resize))
3073+
if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableSizeOne), NULL, false, can_resize)) // "###SizeOne"
30743074
TableSetColumnWidthAutoSingle(table, column_n);
30753075
}
30763076

30773077
const char* size_all_desc;
30783078
if (table->ColumnsEnabledFixedCount == table->ColumnsEnabledCount && (table->Flags & ImGuiTableFlags_SizingMask_) != ImGuiTableFlags_SizingFixedSame)
3079-
size_all_desc = "Size all columns to fit###SizeAll"; // All fixed
3079+
size_all_desc = LocalizeGetMsg(ImGuiLocKey_TableSizeAllFit); // "###SizeAll" All fixed
30803080
else
3081-
size_all_desc = "Size all columns to default###SizeAll"; // All stretch or mixed
3081+
size_all_desc = LocalizeGetMsg(ImGuiLocKey_TableSizeAllDefault); // "###SizeAll" All stretch or mixed
30823082
if (MenuItem(size_all_desc, NULL))
30833083
TableSetColumnWidthAutoAll(table);
30843084
want_separator = true;
@@ -3087,7 +3087,7 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table)
30873087
// Ordering
30883088
if (table->Flags & ImGuiTableFlags_Reorderable)
30893089
{
3090-
if (MenuItem("Reset order", NULL, false, !table->IsDefaultDisplayOrder))
3090+
if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableResetOrder), NULL, false, !table->IsDefaultDisplayOrder))
30913091
table->IsResetDisplayOrderRequest = true;
30923092
want_separator = true;
30933093
}

0 commit comments

Comments
 (0)