- Home /
Problem is not reproducible or outdated
(Localization System) Change Language Problem
Hello!
I made a localization system according to the lesson from Game Dev Guide. However, I ran into the problem of creating a button that changes the language during the game.
I created an event in which the current language is checked and changed, but for the language to change in the game in real time, I need to execute an event from another script, at this point I get an error:
Assets\Scripts\LocalisationSystem.cs(121,9): error CS0120: An object reference is required for the non-static field, method, or property 'TextLocaliserUI.Start()'
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LocalisationSystem : MonoBehaviour
{
public enum Language
{
English,
Russian
}
public static Language language = Language.English;
private static Dictionary<string, string> localisedEN;
private static Dictionary<string, string> localisedRU;
public static bool isInit;
public static CSVLoader csvLoader;
public static void Init()
{
csvLoader = new CSVLoader();
csvLoader.LoadCSV();
UpdateDictionaries();
isInit = true;
}
public static void UpdateDictionaries()
{
localisedEN = csvLoader.GetDictionaryValues("en");
localisedRU = csvLoader.GetDictionaryValues("ru");
}
public static Dictionary<string, string> GetDictionaryForEditor()
{
if(!isInit) { Init(); }
return localisedEN;
}
public static string GetLocalisedValue(string key)
{
if(!isInit) { Init(); }
string value = key;
switch(language)
{
case Language.English:
localisedEN.TryGetValue(key, out value);
break;
case Language.Russian:
localisedRU.TryGetValue(key, out value);
break;
}
return value;
}
public static void Add(string key, string value)
{
if(value.Contains("\""))
{
value.Replace('"', '\"');
}
if(csvLoader == null)
{
csvLoader = new CSVLoader();
}
csvLoader.LoadCSV();
csvLoader.Add(key, value);
csvLoader.LoadCSV();
UpdateDictionaries();
}
public static void Replace(string key, string value)
{
if(value.Contains("\""))
{
value.Replace('"', '\"');
}
if(csvLoader == null)
{
csvLoader = new CSVLoader();
}
csvLoader.LoadCSV();
csvLoader.Edit(key, value);
csvLoader.LoadCSV();
UpdateDictionaries();
}
public static void Remove(string key)
{
if(csvLoader == null)
{
csvLoader = new CSVLoader();
}
csvLoader.LoadCSV();
csvLoader.Remove(key);
csvLoader.LoadCSV();
UpdateDictionaries();
}
public static void SetLanguage(Language newLanguage)
{
language = newLanguage;
TextLocaliserUI.Start(); // Error
}
public void ChangeLang()
{
if(language == Language.English)
{
Debug.Log("Language is set to " + language);
LocalisationSystem.SetLanguage(LocalisationSystem.Language.Russian);
}
else
{
Debug.Log("Language is set to " + language);
LocalisationSystem.SetLanguage(LocalisationSystem.Language.English);
}
}
}
TextLocaliserUI.cs
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
[RequireComponent(typeof(TextMeshProUGUI))]
public class TextLocaliserUI : MonoBehaviour
{
TextMeshProUGUI textField;
public LocalisedString localisedString;
public void Start()
{
textField = GetComponent<TextMeshProUGUI>();
textField.text = localisedString.value;
}
}
LocalisedString.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public struct LocalisedString
{
public string key;
public LocalisedString(string key)
{
this.key = key;
}
public string value
{
get
{
return LocalisationSystem.GetLocalisedValue(key);
}
}
public static implicit operator LocalisedString(string key)
{
return new LocalisedString(key);
}
}
Follow this Question
Related Questions
How to check if BoxCollider2D collided with another BoxCollider2D? 2 Answers
Hello can anyone help me in my code i am converting some code JS to C# 1 Answer
Bullet speed changes depending on how close and far I click the mouse button 2 Answers
Destroy script turns objects into ghosts 2 Answers
Cannot change the value of this integer that I use for array,Cannot change the value of this one. 0 Answers