Untitled

mail@pastecode.io avatar
unknown
csharp
a month ago
2.2 kB
1
Indexable
Never
using UnityEngine;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System;

[Serializable]
public class MoodleTranslationData
{
    public Dictionary<string, string> translations = new Dictionary<string, string>();
}

public class MoodleTranslator : MonoBehaviour
{
    public static MoodleTranslator Instance = null;
    public string selectedLanguage;
    private MoodleTranslationData translationData;

    private void Awake()
    {
        Instance = this;

        LoadLanguage("EN");
    }

    public void LoadLanguage(string language)
    {
        selectedLanguage = language;

        // Construct the path to the JSON file in the Resources folder
        string jsonFilePath = "Localization/UI_" + language;

        // Load the JSON file as a TextAsset
        TextAsset jsonTextAsset = Resources.Load<TextAsset>(jsonFilePath);
        Debug.Log(jsonTextAsset.text);

        if (jsonTextAsset != null)
        {
            translationData = JsonConvert.DeserializeObject<MoodleTranslationData>(jsonTextAsset.text);
            foreach (var entry in translationData.translations)
            {
                Debug.Log($"Key: {entry.Key}, Value: {entry.Value}");
            }
            //foreach doesnt run
        }
        else
        {
            Debug.LogError("Failed to load JSON file: " + jsonFilePath);
        }
    }

    // Create functions to access translations
    public string GetHungerName(int level)
    {
        return GetTranslation("moodle_hunger_level_" + level);
    }

    public string GetHungerDescription(int level)
    {
        return GetTranslation("moodle_hunger_desc_level_" + level);
    }

    public string GetThirstName(int level)
    {
        return GetTranslation("moodle_thirst_level_" + level);
    }

    public string GetThirstDescription(int level)
    {
        return GetTranslation("moodle_thirst_desc_level_" + level);
    }

    private string GetTranslation(string key)
    {
        if (translationData != null && translationData.translations.ContainsKey(key))
        {
            return translationData.translations[key];
        }
        else
        {
            return "Translation Not Found";
        }
    }
}