Skip to main content

Unreal Engine 本地化完整指南

从 FText 宏到区域设置回退链:使用 Localization Dashboard、String Table、C++、Blueprint 和自动化翻译本地化 UE5 游戏。

1

FText 与本地化管线

FText 是 Unreal Engine 可识别本地化的字符串类型。游戏中每个面向用户的字符串(UI 标签、对话、工具提示、通知)都必须使用 FText,才能进入本地化管线。FString 仅用于内部逻辑。

LOCTEXT 需要两个参数:键和源字符串。键在其命名空间中必须唯一。UE 的文本收集器使用这些键跟踪不同区域性中的翻译。NSLOCTEXT 允许显式指定命名空间;LOCTEXT 使用外围 LOCTEXT_NAMESPACE 宏定义的命名空间。
FText Basics
// FText is UE's localization-aware string type.
// ALWAYS use FText for user-facing text, never FString.

// LOCTEXT: localize a literal string (most common)
FText Title = LOCTEXT("MainMenuTitle", "Start Game");

// NSLOCTEXT: specify namespace explicitly
FText Msg = NSLOCTEXT("UI", "WelcomeMessage", "Welcome, adventurer!");

// INVTEXT: invariant text (never translated — debug/logging only)
FText Debug = INVTEXT("Debug overlay active");

// FText::Format: safe variable interpolation
FText Greeting = FText::Format(
    LOCTEXT("PlayerGreeting", "Hello, {PlayerName}!"),
    FText::FromString(PlayerName)
);
MainMenuWidget.cpp
// In a UMG Widget (C++)
void UMainMenuWidget::NativeConstruct()
{
    Super::NativeConstruct();

    // Bind localized text to UI elements
    if (TitleLabel)
    {
        TitleLabel->SetText(LOCTEXT("GameTitle", "My Epic Game"));
    }

    if (PlayButton)
    {
        PlayButton->SetText(LOCTEXT("PlayButtonLabel", "Play Now"));
    }
}

// IMPORTANT: Always use LOCTEXT for UMG widget text.
// Setting text via FString bypasses the localization pipeline.
切勿使用 FString::Printf 或字符串串联构建面向用户的文本。它们会完全绕过本地化管线,生成的文本无法被收集、翻译,也无法在 RTL 语言中正确显示。请始终改用带 LOCTEXT 模式的 FText::Format。
2

设置 Localization Dashboard

Localization Dashboard 是 UE 内置的翻译管理工具。它从源代码中收集所有 LOCTEXT 和 NSLOCTEXT 字符串,将其导出为 .po 文件供翻译,再将结果编译为 UE 在运行时加载的 .locres 二进制文件。

Localization Dashboard Workflow
# 1. Open the Localization Dashboard:
#    Window > Localization Dashboard

# 2. Add a localization target (e.g., "Game")

# 3. Add cultures:
#    Click "Add New Culture" > select languages (de, ja, fr, es, ko, zh-Hans...)

# 4. Gather text:
#    Click "Gather Text" — UE scans all LOCTEXT/NSLOCTEXT macros

# 5. Export translations:
#    Click "Export" to generate .po files for each culture

# 6. Import translations:
#    After translating .po files, click "Import"

# 7. Compile translations:
#    Click "Compile" to generate .locres binary files

# 8. Preview:
#    Editor Preferences > Region & Language > Preview Game Language
每次代码更改添加或修改 LOCTEXT 宏后,都应运行 'Gather Text'。漏掉此步骤意味着新字符串不会出现在 .po 文件中,译者也看不到它们。将收集步骤添加到构建自动化,以自动发现此问题。
3

使用 String Table 管理数据驱动文本

String Table 让您在集中式资源中定义本地化字符串,而不是将 LOCTEXT 宏分散在源文件中。它们非常适合 UI 文本、对话,以及设计师或文案人员需要在不接触代码的情况下编辑的任何字符串。String Table 可定义为 UE 资源,也可从 CSV 导入。

StringTableUsage.cpp
// StringTables provide a data-driven approach to localization.
// Define strings in a CSV asset instead of scattering LOCTEXT across code.

// 1. Create a String Table asset:
//    Content Browser > Right-click > Miscellaneous > String Table

// 2. Or define via CSV (importable into UE):
// Key,SourceString
// MainMenu_Title,Start Game
// MainMenu_Continue,Continue
// MainMenu_Settings,Settings
// MainMenu_Quit,Quit to Desktop
// HUD_Health,Health
// HUD_Ammo,Ammo: {0}
// Dialog_Merchant_Greeting,Welcome to my shop!

// 3. Reference in C++:
FText Title = FText::FromStringTable(
    FName(TEXT("/Game/Localization/ST_MainMenu")),
    TEXT("MainMenu_Title")
);

// 4. Reference in Blueprints:
//    Use the "Make Text from String Table" node
//    Set Table ID and Key
Localization Dashboard 会自动收集 String Table。对于在 String Table 中定义的字符串,无需使用 LOCTEXT 宏,只需在 C++ 或 Blueprint 中通过表 ID 和键引用。
4

C++ 本地化模式

在 C++ 中,于每个 .cpp 文件顶部定义 LOCTEXT_NAMESPACE,并对所有面向用户的字符串使用 LOCTEXT。对于带变量的动态内容,请使用 FText::Format。始终在文件末尾取消定义命名空间,避免泄漏。

MainMenuWidget.cpp
// Define a namespace at the top of each .cpp file
// All LOCTEXT calls in this file use this namespace
#define LOCTEXT_NAMESPACE "MyGame.MainMenu"

#include "MainMenuWidget.h"

void UMainMenuWidget::NativeConstruct()
{
    Super::NativeConstruct();

    // These use the "MyGame.MainMenu" namespace automatically
    TitleText->SetText(LOCTEXT("Title", "Main Menu"));
    PlayText->SetText(LOCTEXT("PlayButton", "Play"));
    SettingsText->SetText(LOCTEXT("SettingsButton", "Settings"));
    QuitText->SetText(LOCTEXT("QuitButton", "Quit"));
}

// CRITICAL: Always undefine at the end of the file
#undef LOCTEXT_NAMESPACE
FText::Format Examples
// FText::Format — the safe way to build localized strings
// NEVER use FString::Printf or string concatenation for user-facing text.

// Named arguments (recommended)
FText ItemPickup = FText::Format(
    LOCTEXT("ItemPickup", "You picked up {ItemName} x{Count}"),
    FText::FromString(ItemName),
    FText::AsNumber(Count)
);

// FText::AsNumber respects locale formatting:
//   English: 1,234,567
//   German:  1.234.567
//   French:  1 234 567

// FText::AsCurrency for prices:
FText Price = FText::AsCurrency(
    9.99,
    TEXT("USD"),
    &FInternationalization::Get().GetCurrentCulture().Get()
);

// FText::AsDate and FText::AsTime for dates:
FText DateStr = FText::AsDate(FDateTime::Now());
FText::Format 参数也必须是 FText,而非原始 FString。使用 FText::FromString() 转换 FString 值,使用 FText::AsNumber() 进行可识别区域设置的数字格式化,使用 FText::AsCurrency() 格式化价格。原始 FString 串联生成的文本不遵循区域设置格式规则。
5

Blueprint 本地化

Blueprint 中的所有 Text 属性默认都是 FText,因此已可用于本地化。在属性详细信息面板中设置 Key 和 Namespace,使字符串可被收集。对于带变量的动态内容,请使用 Format Text 节点。

Blueprint Localization Patterns
// Blueprint Localization Basics:
//
// 1. All Text properties in Blueprints are FText by default
//    (already localization-ready)
//
// 2. Set the Text property in the Details panel
//    Expand the dropdown to set:
//    - Key: unique identifier for translation
//    - Namespace: grouping for organization
//    - Source String: the text to display/translate
//
// 3. For dynamic text, use the "Format Text" node:
//    Format: "Hello, {PlayerName}!"
//    Connect a "Find" pin named "PlayerName" to your variable
//
// 4. For plurals, use "Text Format with Arguments":
//    Pattern: "{Count}|plural(one=item,other=items)"
//
// 5. Culture switching at runtime:
//    Use "Set Current Culture" node
//    Input: culture code string (e.g., "de", "ja", "fr")
在 Blueprint Details 面板中展开文本属性下拉菜单,查看 Key、Namespace 和 Source String 字段。设置有意义的 Key,可让译者更容易识别 .po 文件中的字符串。
6

处理复数和性别

Unreal Engine 支持用于复数和性别相关文本的 ICU 消息格式。在源字符串中定义复数规则,UE 会根据当前区域性的 CLDR 规则自动选择正确形式。

ICU Plural & Gender Rules
// UE uses ICU message format for plurals.
// Define plural rules in your .po or String Table:

// English source:
// "{Count}|plural(one=You have # item,other=You have # items)"

// German translation:
// "{Count}|plural(one=Du hast # Gegenstand,other=Du hast # Gegenstaende)"

// Arabic translation (6 forms):
// "{Count}|plural(zero=لا عناصر,one=عنصر واحد,two=عنصران,few=# عناصر,many=# عنصرًا,other=# عنصر)"

// Japanese (1 form):
// "{Count}|plural(other=アイテム#個)"

// In C++:
FText ItemCount = FText::Format(
    LOCTEXT("ItemCount",
        "{Count}|plural(one=You have {Count} item,other=You have {Count} items)"),
    ItemCount
);

// Gender-dependent text:
// "{Gender}|gender(masculine=Il est,feminine=Elle est) prêt{Gender}|gender(masculine=,feminine=e)"
切勿硬编码 count == 1 来检测单数。法语将 0 视为单数,俄语对 'few' 和 'many' 使用不同形式,阿拉伯语有 6 种复数形式。请让 ICU 复数规则处理逻辑,定义所有必需形式,由 UE 为每种区域性选择正确形式。
7

打包和测试本地化

发布前,请验证所有目标区域性都有已编译的 .locres 文件,并确认文本可在运行时正确呈现。使用编辑器的区域性预览、命令行区域性覆盖和自动检查,发现缺失或损坏的翻译。

Packaging & Testing Workflow
# Compile and test localization before packaging:

# 1. Editor Preferences > Region & Language > Preview Game Language
#    Set to each target language and verify all text renders correctly.

# 2. Command-line culture override for testing:
MyGame.exe -culture=ja

# 3. Packaging settings:
#    Project Settings > Packaging > Localizations to Package
#    Select all cultures you want to include in the build.

# 4. Verify .locres files exist after packaging:
MyGame/Content/Localization/Game/
├── en/
│   └── Game.locres
├── de/
│   └── Game.locres
├── ja/
│   └── Game.locres
├── fr/
│   └── Game.locres
└── ko/
    └── Game.locres

# 5. Runtime culture switching:
#    FInternationalization::Get().SetCurrentCulture(TEXT("ja"));
#    This reloads all FText strings from the new culture's .locres files.
如果某个区域性未列在 Project Settings > Packaging > Localizations to Package 中,其 .locres 文件会被排除在构建之外。玩家在运行时选择该语言后会看到回退文本或空字符串。请始终确认打包设置与支持的区域性一致。
8

添加区域设置回退链

Unreal Engine 的默认本地化仅沿 IETF 子标签层次结构回退。pt-BR 用户缺少翻译时会看到英语,而不是完全可用的 pt-PT 翻译。locale-chain-ue 通过 FTextLocalizationManager 添加可配置的横向回退链,使地区用户始终看到最接近的可用翻译。

locale-chain-ue (C++)
// locale-chain-ue: Smart fallback chains for UE5
// Problem: UE falls back to default when a locale is missing.
// pt-BR user gets English instead of pt-PT translations.

// Solution: One function call at startup.
#include "LocaleChain.h"

void UMyGameInstance::Init()
{
    Super::Init();
    ULocaleChain::Configure();  // Load 75 built-in fallback chains
}

// Now resolve strings with per-key fallback:
FString Greeting = ULocaleChain::Resolve(
    TEXT("greeting"), TEXT("MyNamespace")
);
// pt-BR user: tries pt-BR -> pt-PT -> pt -> default

// Custom overrides:
TMap<FString, FString> Overrides;
Overrides.Add(TEXT("pt-BR"), TEXT("pt"));       // Simplify chain
Overrides.Add(TEXT("sv-FI"), TEXT("sv"));        // Add new chain
ULocaleChain::ConfigureWithOverrides(Overrides);

// Full control (C++ only):
TMap<FString, TArray<FString>> Custom;
Custom.Add(TEXT("pt-BR"), {TEXT("pt-PT"), TEXT("pt")});
Custom.Add(TEXT("es-MX"), {TEXT("es-419"), TEXT("es")});
ULocaleChain::ConfigureCustom(Custom, false);
启动时调用一次 ULocaleChain::Configure(),加载涵盖 11 个语系的 75 条内置回退链。使用 ConfigureWithOverrides 进行便于 Blueprint 使用的自定义,或在 C++ 中使用 ConfigureCustom 完全控制回退行为。

自动保证翻译质量

使用 i18n-validate 在发布前发现缺失键和损坏的占位符。真实译文完成前,可使用 i18n-pseudo 生成伪译文来测试 UI。

常见问题

对面向用户的文本使用 FString

FString 会绕过整个本地化管线。使用 FString::Printf 或字符串串联构建的文本无法被收集、翻译,也无法在 RTL 语言中正确显示。请始终对用户可见字符串使用带 LOCTEXT 宏和 FText::Format 的 FText。

缺少 #undef LOCTEXT_NAMESPACE

忘记在 .cpp 文件末尾使用 #undef LOCTEXT_NAMESPACE,会导致命名空间泄漏到后续翻译单元。这会在无提示的情况下为其他文件中的字符串分配错误命名空间,使翻译出现在错误上下文中。

硬编码复数逻辑

编写 'count == 1 ? singular : plural' 会忽略 CLDR 规则。法语将 0 视为单数,俄语有 4 种复数形式,阿拉伯语有 6 种。请在 FText 模式中使用 ICU 复数语法,让 UE 按区域性处理规则。

导入后忘记编译

导入 .po 文件会更新文本数据,但不会生成 .locres 二进制文件。游戏在运行时仍会加载旧的已编译翻译。导入后请始终在 Localization Dashboard 中运行 'Compile',或将此步骤添加到构建自动化。

推荐的项目结构

Project Structure
MyUnrealProject/
├── Config/
│   └── Localization/
│       └── Game.ini                  # Localization target config
├── Content/
│   └── Localization/
│       ├── Game/
│       │   ├── Game.manifest         # Gather manifest
│       │   ├── en/
│       │   │   ├── Game.po           # Source strings (.po)
│       │   │   └── Game.locres       # Compiled binary
│       │   ├── de/
│       │   │   ├── Game.po
│       │   │   └── Game.locres
│       │   ├── ja/
│       │   │   ├── Game.po
│       │   │   └── Game.locres
│       │   └── fr/
│       │       ├── Game.po
│       │       └── Game.locres
│       └── StringTables/
│           ├── ST_MainMenu.uasset    # String Table asset
│           └── ST_HUD.uasset
├── Plugins/
│   └── LocaleChain/                  # locale-chain-ue plugin
│       ├── LocaleChain.uplugin
│       └── Source/
│           └── LocaleChain/
├── Source/
│   └── MyGame/
│       ├── UI/
│       │   ├── MainMenuWidget.h
│       │   └── MainMenuWidget.cpp    # LOCTEXT macros here
│       └── MyGameInstance.cpp        # ULocaleChain::Configure()
└── MyUnrealProject.uproject

立即试用 i18n Agent

将翻译文件拖放到此处

JSON, YAML, PO, XML, CSV, Markdown, Properties

或点击选择文件

目标语言

无需注册即时估价

常见问题