Demo, Examples/AppleMetal: add opt-in i18n runtime language switching with Simplified Chinese (zh_CN)#9438
Demo, Examples/AppleMetal: add opt-in i18n runtime language switching with Simplified Chinese (zh_CN)#9438xuk3r wants to merge 12 commits into
Conversation
- Add i18n/ module: imgui_i18n.h (Tr() macro + stub when disabled), imgui_i18n.cpp (lookup table, Meyers singleton, placeholder substitution), locale/zh_CN.cpp (Simplified Chinese translation table), tools/extract_strings.py (helper to extract translatable strings) - Opt-in via IMGUI_DEMO_ENABLE_I18N build flag; without it Tr(s) expands to (s) at compile time with zero overhead and no extra dependencies - See i18n/README.md for integration guide and how to add a new language
… example - imgui_demo.cpp: wrap all user-visible string literals with Tr(); add Language menu in DemoWindowMenuBar (guarded by IMGUI_DEMO_ENABLE_I18N); guard extern g_need_font_rebuild with IMGUI_DEMO_ENABLE_I18N + __APPLE__ - examples/example_apple_metal/main.mm: CJK font loading via RebuildFonts() (always loads 2-glyph stub in English, full range in zh_CN); Language menu triggers font atlas rebuild before next NewFrame(); wrap demo window strings with Tr(); fix v1.92 ImplicitRefSize assertion by passing explicit SizePixels to AddFontDefault() - examples/example_apple_metal/Makefile: add i18n sources and -DIMGUI_DEMO_ENABLE_I18N flag
…BLE_I18N Add imgui_i18n.cpp and locale/zh_CN.cpp to both iOS and macOS targets. Set IMGUI_DEMO_ENABLE_I18N=1 in GCC_PREPROCESSOR_DEFINITIONS for all four build configurations (iOS/macOS × Debug/Release).
…get opt-out - Wrap RebuildFonts(), g_need_font_rebuild, and font rebuild calls in #ifdef IMGUI_DEMO_ENABLE_I18N so main.mm compiles cleanly without i18n - Include imgui_i18n.h unconditionally (provides no-op Tr() stub when IMGUI_DEMO_ENABLE_I18N is not defined, enabling Tr() in the demo window) - Remove IMGUI_DEMO_ENABLE_I18N and i18n sources from iOS Xcode target (untested on iOS; macOS-only system font paths would not work on device)
- Do not translate 'Hello, world!' (universally recognized programming example) - Do not translate 'This is some useful text.' (standard example boilerplate) - Do not translate 'float' / 'counter = %d' (C++ type/variable labels) - Keep 'blah blah blah' as-is (intentional nonsense filler, not real content) - Keep 'dear imgui' brand name untranslated in greeting message - Fix 'Another bullet point' -> 正确翻译为项目符号而非叶节点 - Fix 'Metrics/Debugger' -> 指标/调试器 (Metrics = indicators, not performance) - Remove redundant identity entries for API function names (IsItemXxx etc.)
Document what to translate and what not to translate, with examples and quality rules (accuracy, format specifiers, redundant entries).
…nvention misc/ already hosts optional supplemental modules (cpp/imgui_stdlib, freetype/, fonts/). i18n fits the same pattern: opt-in, not part of the core library. Update all include paths, Makefile, Xcode project and README accordingly.
Wrap all BulletText() calls in ShowUserGuide() with Tr() and add Simplified Chinese translations for all 20 user guide strings.
This place seems wrong. You should never have the specify glyph ranges since 1.92. In fact they are ignored if you are an updated backend. Please remove it and use normal MergeMode. ImplicitRefSize assertion is to detect if you try to merge font B into font A and specify ref size only for font B. It doesn’t make sense, as font B need to use the size of font A multiplied by RefSizeB/RefSizeA. |
…around In v1.92+ with an updated backend, glyph ranges are handled dynamically and do not need to be specified. Remove GetGlyphRangesChineseSimplifiedCommon(), the zh_label_ranges English stub, and the explicit SizePixels on AddFontDefault() that was added to work around the ImplicitRefSize assertion. Use plain MergeMode instead.
…K font AddFontDefault() uses implicit ref size. Passing an explicit size (13.0f) to AddFontFromFileTTF() in MergeMode triggers the assertion because the two fonts use different ref size conventions. Pass 0.0f instead so both fonts share the same implicit ref size and scale together with style.FontSizeBase.
|
Fixed in the two latest commits:
Feel free to let me know if there's anything else to address! |
Adds a lightweight opt-in internationalization system for

imgui_demo.cpp, with afirst translation for Simplified Chinese (
zh_CN).Design
Fully opt-in via build flag. Define
IMGUI_DEMO_ENABLE_I18Nto activate. Withoutit,
Tr(s)expands to(s)at compile time — zero overhead, no extra dependencies,all existing examples build unchanged.
New directory:
misc/i18n/(alongside existing optional modulesmisc/cpp/,misc/fonts/,misc/freetype/)misc/i18n/imgui_i18n.hTr()macro; no-op stub whenIMGUI_DEMO_ENABLE_I18Nismisc/i18n/imgui_i18n.cppmisc/i18n/locale/zh_CN.cppmain()via static initializermisc/i18n/tools/extract_strings.pyimgui_demo.cppmisc/i18n/README.mdimgui_demo.cpp: all user-visible string literals wrapped withTr(). Languagemenu added to
DemoWindowMenuBar(), guarded by#ifdef IMGUI_DEMO_ENABLE_I18N.Strings intentionally kept in English per localization standards:
"Hello, world!"(universally recognized programming example), API function names, C++ type labels
(
"float"), intentional filler ("blah blah blah").example_apple_metal(macOS only):RebuildFonts()loads CJK glyphs on languageswitch. In English mode only 2 glyphs (
中文, U+4E2D U+6587) are loaded for theLanguage menu label; in
zh_CNmode the fullGetGlyphRangesChineseSimplifiedCommon()range is used. Requires passing explicit
SizePixelstoAddFontDefault()to avoidthe
ImplicitRefSizeassertion introduced in v1.92 when usingMergeMode. iOS targetdoes not enable i18n (macOS system font paths do not apply on device).
Adding a new language
misc/i18n/locale/zh_CN.cpp→misc/i18n/locale/<locale_id>.cppand fill intranslations
misc/i18n/imgui_i18n.cppMenuItemin the Language menu inDemoWindowMenuBar()(searchBeginMenu(Tr("Language")))RebuildFonts()in your platformmainfile with theappropriate font and glyph ranges
See
misc/i18n/README.mdfor full details and translation guidelines.Testing
example_apple_metalmacOS (Makefile + Xcode): builds and runs with i18n enabled;language switch works at runtime ✓
example_apple_metaliOS Simulator (Xcode): builds cleanly without i18n ✓example_null,example_glfw_opengl3,example_glfw_metal,example_glfw_opengl2,example_apple_opengl2,example_glut_opengl2: all build cleanly without any changes✓