UnityLocalization
No description available
Install / Use
/learn @DFTGames/UnityLocalizationREADME
DFT Games Localization Solution
Table of Contents
- Introduction
- Features
- Quick Start
- Setup Guide
- Localization Scripts
- Language Selection System
- TextMeshPro Utility
- Best Practices
- Troubleshooting
Introduction
The DFT Games Localization Solution is a comprehensive Unity package that enables multi-language support for your Unity projects. It provides an easy-to-use system for localizing both text and images in your game's user interface.
Features
-
Text Localization
- Full support for Unity's UGUI Text components
- Full support for TextMeshPro (TMP) components
- Simple key-value pair format for translation files
- Support for multi-line text
-
UI Image Localization
- Language-specific 2D sprite support
- Automatic sprite swapping based on selected language
-
Language Management
- Automatic language detection based on system language
- Persistent language preference storage using Unity's PlayerPrefs
- Dynamic language switching at runtime
- Fallback to English if translation is missing
Quick Start
- Create language files in
Resources/localization/folder (e.g.,English.txt,Spanish.txt) - Add localization scripts to your UI components:
Localizefor UGUI TextLocalizeTMProfor TextMeshProLocalizeImagefor UI Images
- Set the localization key in the component inspector
- Run your game - the system will automatically load the appropriate language
Setup Guide
Text Localization Files
File Location
Create your language files in the following directory:
Resources/localization/
Naming Convention
Each language file must be named using Unity's SystemLanguage enum values followed by .txt:
English.txtSpanish.txtFrench.txtGerman.txtItalian.txtJapanese.txtChineseSimplified.txt- etc.
Important: Language names must match exactly with Unity's SystemLanguage enum.
File Format
Each line in the file contains a single key-value pair using one of these separators:
- Equal sign:
= - Tab character:
\t
Syntax:
key=value
key value
Special Characters:
- Use
\nfor line breaks within text - Keys are case-sensitive
- Empty lines are ignored
Example Language File
English.txt:
welcome=Welcome to the Game!
start_game=Start Game
exit_game=Exit
settings=Settings
confirm_exit=Are you sure you want to exit?
multiline_text=This is line one\nThis is line two\nThis is line three
player_score=Score: {0}
Spanish.txt:
welcome=¡Bienvenido al Juego!
start_game=Iniciar Juego
exit_game=Salir
settings=Configuración
confirm_exit=¿Estás seguro de que quieres salir?
multiline_text=Esta es la línea uno\nEsta es la línea dos\nEsta es la línea tres
player_score=Puntuación: {0}
UI Image Localization Files
Directory Structure
Create language-specific folders for your localized sprites:
Resources/localization/UI/<LanguageName>/
Example Structure
Resources/
??? localization/
??? UI/
??? English/
? ??? logo.png
? ??? button_start.png
? ??? icon_flag.png
??? Spanish/
? ??? logo.png
? ??? button_start.png
? ??? icon_flag.png
??? French/
??? logo.png
??? button_start.png
??? icon_flag.png
Important: Sprite files must have identical names across all language folders.
Localization Scripts
LocalizeImage
Purpose: Localizes UI Image components with language-specific sprites.
Usage:
- Add the
LocalizeImagecomponent to a GameObject with aUI.Imagecomponent - Set the
Image Namefield to match the sprite filename (without extension) - The component will automatically load the correct sprite based on the current language
Inspector Properties:
Image Name: The name of the sprite file (without path or extension)
Localize
Purpose: Localizes Unity's UGUI Text components.
Usage:
- Add the
Localizecomponent to a GameObject with aTextcomponent - Set the
Localization Keyfield to match a key from your language files - The text will automatically update to the translated value
Inspector Properties:
Localization Key: The key to look up in the language file
LocalizeTMPro
Purpose: Localizes TextMeshPro UGUI components.
Usage:
- Add the
LocalizeTMProcomponent to a GameObject with aTextMeshProUGUIcomponent - Set the
Localization Keyfield to match a key from your language files - The text will automatically update to the translated value
Inspector Properties:
Localization Key: The key to look up in the language file
Language Selection System
How Language is Determined
The localization system uses the following priority order:
-
Saved Preference (Highest Priority)
- Checks Unity's PlayerPrefs for a saved language preference
- Key:
Language(stored via PlayerPrefs)
-
System Language (Medium Priority)
- Detects the operating system language using
Application.systemLanguage - Only used if a corresponding translation file exists
- Detects the operating system language using
-
English Fallback (Lowest Priority)
- Defaults to English if no saved preference exists and system language file is not found
- English is the default fallback language
Changing Language at Runtime
To change the language programmatically:
using UnityEngine;
public class LanguageSelector : MonoBehaviour
{
public void SetLanguage(SystemLanguage language)
{
// Change the language
Locale.CurrentLanguage = language;
// Language preference is automatically saved to PlayerPrefs
// All localization components will update automatically
}
public void SetEnglish()
{
SetLanguage(SystemLanguage.English);
}
public void SetSpanish()
{
SetLanguage(SystemLanguage.Spanish);
}
}
Persistence
The selected language is automatically saved to Unity's PlayerPrefs system and will persist across game sessions. No additional code is required to save or load the language preference.
TextMeshPro Utility
mergeAllText.ps1 Script
Location:
Assets/DFTGames/Localization/Demo/Resources/localization/
Purpose:
- Merges all
.txtlanguage files into a singleAllText.txtfile - Useful for generating TextMeshPro font atlases with only the required characters
- Reduces font atlas size and improves performance
How to Use:
- Navigate to the script directory in Windows Explorer
- Right-click on
mergeAllText.ps1 - Select "Run with PowerShell"
- The script will create
AllText.txtin the same directory
Using with TMPro Font Asset Creator:
- Run the
mergeAllText.ps1script to generateAllText.txt - Open Window ? TextMeshPro ? Font Asset Creator
- In the "Character Set" dropdown, select "Characters from File"
- Select the generated
AllText.txtfile - Generate the font atlas - it will only include characters used in your localized text
Note: This script only works on Windows systems with PowerShell.
Best Practices
File Organization
- Keep all language files in
Resources/localization/ - Use consistent key names across all language files
- Document your keys in a shared spreadsheet or document
- Use descriptive key names (e.g.,
menu_start_gameinstead ofmsg1)
Key Naming Conventions
# Good examples
ui_button_start=Start
ui_button_exit=Exit
menu_title_main=Main Menu
dialog_confirm_quit=Are you sure you want to quit?
error_network_connection=Unable to connect to server
# Avoid
btn1=Start
txt2=Exit
msg=Main Menu
Testing Multiple Languages
- Create a debug menu with language selection buttons
- Test all UI elements in each language
- Check for text overflow or truncation issues
- Verify that all images load correctly for each language
Performance Tips
- Use the TextMeshPro utility to minimize font atlas size
- Avoid creating language files with duplicate keys
- Load only the necessary language files at runtime
- Cache frequently accessed localized strings
Troubleshooting
Text Not Updating
Problem: Text remains in English or doesn't change when language is switched.
Solutions:
- Verify that the localization key exists in all language files
- Check that the key name is spelled correctly (case-sensitive)
- Ensure the language file is named correctly (e.g.,
Spanish.txt, notspain.txt) - Verify the file is in the
Resources/localization/folder
Images Not Loading
Problem: UI images don't change or show as missing sprites.
Solutions:
- Verify that sprite files have identical names across all language folders
- Check that the folder structure matches:
Resources/localization/UI/<LanguageName>/ - Ensure the
Image Namefield matches the sprite filename exactly (without extension) - Verify that the sprites are imported correctly in Unity
Language File Not Found
Problem: Game defaults to English even though language files exist.
Solutions:
- Verify the language name matches Unity's
SystemLanguageenum exactly - Check for typos in the filename (e.g.,
Englsh.txtinstead ofEnglish.txt) - Ensure files are in the correct directory:
Resources/localization/ - Check that files have the
.txtextension
Special Characters Not Displaying
Problem: Special characters (accents, umlauts, etc.) show as boxes or que
