# Display dynamic values in a localized string

> Drive a Smart String entry with a local variable from a script.

Drive a Smart String entry with a local variable from a script.

This example builds a score readout that localizes its text and updates its number from gameplay: `Score: 120` in English, `Punkte: 120` in German. The collection table entry owns the sentence, while an associated script owns the number. For the format syntax, refer to [Smart Strings](/engine/6000.7/manual/scripting/generate-dynamic-text/smart-strings.md).

## Prerequisites

Before you start, make sure you have the following:

* A table collection. Refer to [Get started with localization](/engine/6000.7/manual/get-started.md).

## Author the entry

To author a Smart String entry:

1. Open **Window** > **Localization** > **Resource Tables** and select the collection.
2. Add a key named `score-display` with the entry type **String**.
3. Enable the key's **Smart string** toggle from the row's actions menu (**⋮**).
4. Give each locale a value that uses a placeholder named `score`, for example `Score: {score}` in English.

Because each placeholder has a name, each translation can place it wherever the language needs it.

## Update the value from a script

Add a `LocalizedString` field, register an [`IntVariable`](/engine/6000.7/script-reference/unity/smartstrings/persistentvariables/intvariable.md) named `score` in its local variables, and refresh when the value changes:

```cs
public class ScoreDisplay : MonoBehaviour
    {
        public LocalizedString scoreText = new();

        IntVariable m_Score = new();

        void OnEnable()
        {
            scoreText.LocalVariables.Add(m_Score, "score");
            scoreText.StringChanged += OnStringChanged;
        }

        void OnDisable()
        {
            scoreText.StringChanged -= OnStringChanged;
            scoreText.LocalVariables.Remove("score");
        }

        void OnStringChanged(string value) => GetComponent<TextMesh>().text = value;

        public void AddPoints(int points)
        {
            m_Score.Value += points;
            scoreText.RefreshString();
        }
    }
```

In the Editor window, set the `scoreText` field to the `score-display` entry. Each call to `AddPoints` updates the variable and calls [`RefreshString`](/engine/6000.7/script-reference/unity/localization/localizedstring/refreshstring.md), which re-raises [`StringChanged`](/engine/6000.7/script-reference/unity/localization/localizedstring/stringchanged.md) with the formatted value. A language change re-raises it too, so the readout stays correct after a value change and after a language change.

## Extend the example

For more information about how you can configure dynamic text in your application using Smart Strings, refer to the following links:

* Handle pluralization in the entry itself with the [plural formatter](/engine/6000.7/manual/scripting/generate-dynamic-text/formatters/plural-formatter.md), so each language picks the right word form from the count.
* Share values across many strings with [persistent variables](/engine/6000.7/manual/scripting/generate-dynamic-text/sources/persistent-variables-source.md) instead of per-reference local variables.
* Pass one-off values as arguments to [`GetLocalizedString`](/engine/6000.7/script-reference/unity/localization/localizedstring/getlocalizedstring.md) instead of registering variables.

## Additional resources

* [Localize strings](/engine/6000.7/manual/localization/translations/localize-strings.md)
* [Generate dynamic text with Smart Strings](/engine/6000.7/manual/scripting/generate-dynamic-text/smart-strings.md)
* [Persistent variables source](/engine/6000.7/manual/scripting/generate-dynamic-text/sources/persistent-variables-source.md)
