01 Feb

Extracting Localization Phrases from our Unity projects

In our Unity projects, we handle localization using the No Such Localization component, as detailed in a previous post. Specifically, we use the phrases version, where phrases act as keys to retrieve strings from a comprehensive table. Switching languages is as simple as changing the table for text (for images, we use a different method).

We maintain a large database of strings and extract a subset tailored to each game’s specific requirements. To do this, we compile a list of all the phrases used by the localization components within a given game. Our process is straightforward: we open each scene in the Unity project, retrieve all localization components, and store their phrases.

The following code demonstrates our approach. We’ve integrated this functionality into a Unity editor menu option, automating the entire process of retrieving phrases and writing them into a text file.

public class IKIGamesEditorTools
{
    private const string LocalizationMenuPath = "IKIGames/Localization/Extract All Phrases";
    private const string LocalizationFileName = "localization_keys.txt";

    [MenuItem(LocalizationMenuPath)]
    private static void ExtractAllLocalizationPhrasesInAllScenes()
    {
        HashSet<string> phrases = new HashSet<string>();
        List<string> scenePaths = GetScenePaths();

        foreach (var scenePath in scenePaths)
        {
            ExtractPhrasesFromScene(scenePath, phrases);
        }

        WritePhrases(phrases, LocalizationFileName);
    }

    private static List<string> GetScenePaths()
    {
        List<string> scenePaths = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            scenePaths.Add(scene.path);
        }
        return scenePaths;
    }

    private static void ExtractPhrasesFromScene(string scenePath, HashSet<string> phrases)
    {
        UnityEditor.SceneManagement.EditorSceneManager.OpenScene(scenePath, UnityEditor.SceneManagement.OpenSceneMode.Single);
        ExtractPhrasesFromLocalizationComponentes(phrases);
        // No need to close the new scene since OpenSceneMode.Single opens it as the only active scene
        Debug.Log($"Processed: {scenePath}");
    }

    private static void ExtractPhrasesFromLocalizationComponentes(HashSet<string> phrases)
    {
        foreach (NoSuchStudio.Localization.Localizers.TMProTextLocalizer localizer in Resources.FindObjectsOfTypeAll<NoSuchStudio.Localization.Localizers.TMProTextLocalizer>())
        {
            if (!string.IsNullOrEmpty(localizer.phrase))
            {
                phrases.Add(localizer.phrase);
            }
        }
        foreach (NoSuchStudio.Localization.Localizers.TextLocalizer localizer in Resources.FindObjectsOfTypeAll<NoSuchStudio.Localization.Localizers.TextLocalizer>())
        {
            if (!string.IsNullOrEmpty(localizer.phrase))
            {
                phrases.Add(localizer.phrase);
            }
        }
    }

    private static void WritePhrases(HashSet<string> hashSet, string filename)
    {
        string filePath = Path.Combine(Application.dataPath, filename);
        try
        {
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                foreach (string item in hashSet)
                {
                    writer.WriteLine(item);
                }
            }
            Debug.Log($"Phrases written successfully to: {filePath}");
        }
        catch (System.Exception ex)
        {
            Debug.LogError($"Failed to write phrases to file: {ex.Message}");
        }
    }
}
11 Dec

DragonScales 6 game: Solution for Level 5-8

Some players of the latest DragonScales game, DragonScales 6: Love and Redemption, have reached out to us asking about a solution for Level 5-8. That level might seem a bit intimidating at first look, but once you figure out the little tricks for several parts of the board you’ll agree it’s kind of easy. Let’s take a look at the initial configuration of the board.

DragonScales 6 game initial configuration of Level 5-8
Initial configuration

A typical DragonScales board. We have two goals only: capture the red and the green cross-scales. There are some novelties, though, specific to DragonScales 6: keys and doors. Cells with a door icon won’t accept scales on them, so you’ll first have to remove the doors. That means you won’t be able to clear the top-left red cross-scale, or the green cross-scale or activate the arrow until you manage to remove the respective doors. To remove a door just click on it, but you’ll obviously need to capture at least one key beforehand. To capture a key, just form a combination containing the cell with the key.

Read More