LanguageSystem Pro
  • Welcome
  • Getting Started
    • Quickstart
    • Importing and Exporting
    • InternalDatabase Mode
    • ExternalFiles Mode
    • GlobalExternal Mode
    • ComponentBased Mode
    • Importing Translated Text into Custom Script
    • LanguageAudioManager
    • AudioWarper Component
    • Importing Audio into Custom Script
    • Switching Languages
    • LanguageManagerEditor
Powered by GitBook
On this page
  1. Getting Started

Importing Translated Text into Custom Script

Importing Translated Text into Custom Scripts

Overview

The LanguageManager system not only handles in-game text translations for UI elements, but it also allows you to fetch translated text directly into your custom scripts. This can be useful when you need to handle translated content programmatically. In this section, we’ll show you how to integrate translated text into your custom scripts, focusing on retrieving the translations using the LanguageManager's GetTextEntryByID() method.

Example: Displaying Translated Error Messages

The following example demonstrates how to use the LanguageManager to retrieve a translated error message based on the current language and display it in a Text or TextMeshProUGUI component in Unity. This is useful for displaying dynamic content like error messages, notifications, or other in-game feedback that needs to adapt to the player's selected language.

Here’s how you can integrate text translations into a custom script:

Step-by-Step Breakdown

  1. Assign Text Components: Start by adding a reference to a Text or TextMeshProUGUI component in your script. These components will display the text retrieved from the LanguageManager.

  2. Get the Text ID: The LanguageManager stores each text entry using a unique textID. In this example, we retrieve the translated text based on the textID provided.

  3. Retrieve the Translation: Use the LanguageManager method GetTextEntryByID(textID) to fetch the translated text. This function returns the text corresponding to the provided textID in the player's currently selected language.

  4. Display the Translation: After fetching the translated text, assign it to your UI element (such as Text or TextMeshProUGUI), and it will update automatically based on the current language.

Script Example

Here’s a script that demonstrates how to import and use translated text in a custom Unity script:

using System.Collections;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

namespace LanguageManager
{
    /// <summary>
    /// This script is used to display an error message from the LanguageManager system
    /// on a Text or TextMeshProUGUI component.
    /// </summary>
    public class ErrorMessageTest : MonoBehaviour
    {
        public TextMeshProUGUI textMeshProUGUI; // The TextMeshPro component to display the error message
        public UnityEngine.UI.Text text; // The Unity Text component to display the error message
        public string textId; // The ID of the text in the LanguageManager

        /// <summary>
        /// Retrieves the error message from LanguageManager using the provided textId
        /// and displays it on the specified Text or TextMeshProUGUI component.
        /// </summary>
        public void ShowErrorMessage()
        {
            try
            {
                // Get the error message from the LanguageManager using the textId
                string errorMessage = LanguageManager.Instance.GetTextEntryByID(textId);

                // Check if TextMeshProUGUI is assigned and set the error message
                if (textMeshProUGUI != null)
                {
                    textMeshProUGUI.text = errorMessage;
                    textMeshProUGUI.color = Color.red; // Example: Set the text color to red
                }
                // If not, check if Unity Text is assigned and set the error message
                else if (text != null)
                {
                    text.text = errorMessage;
                    text.color = Color.red; // Example: Set the text color to red
                }
                else
                {
                    Debug.LogError("No Text or TextMeshProUGUI component is assigned to display the error message.");
                    return;
                }
            }
            catch (System.Exception ex)
            {
                Debug.LogError("Failed to retrieve or display the error message: " + ex.Message);
            }
        }
    }
}

Key Concepts

  • Using GetTextEntryByID(): This is the core method for retrieving translations. Simply pass the textID associated with the text you want, and LanguageManager will return the correct translation for the current language.

    string translatedText = LanguageManager.Instance.GetTextEntryByID(textId);
  • Displaying in Text or TextMeshProUGUI: You can directly assign the retrieved text to either Text (Unity UI) or TextMeshProUGUI components, depending on your project setup. This allows you to use either Unity's built-in text system or TextMeshPro, both of which are compatible with LanguageManager.

Usage Scenario

Imagine you are creating an in-game error popup or notification. Instead of hard-coding the error message in a single language, you can use LanguageManager to ensure that the message appears in the player’s selected language, enhancing the user experience.

For example, if a player tries to perform an invalid action, you can fetch a translated error message using GetTextEntryByID("error_invalid_action") and display it dynamically on the UI.

PreviousComponentBased ModeNextLanguageAudioManager

Last updated 8 months ago