Creating a Unity Multilingual System
Creating a Unity Multilingual System
Hello everyone, in this article I will talk about how you can create a multilingual system in Unity. I'll explain how to automatically select the language according to the device's language in your applications or how to show the language selected by the user or player.
Working Principle
First, I want to talk about how the system works.
This system works with a very simple logic, creating classes for word groups and then fetching these word groups in the language system class we've created, placing the words according to the system language or the language selected by the player.
In the Unity scene, we prevent this file from being deleted and retrieve all the texts we use in every scene through this system.
In fact, if you assign a tag to the object where you place the language system in Unity, it may be easier to access the language system by that tag.
Codes
// We import our libraries
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Our language system class
public class DilSistemi : MonoBehaviour
{
#region Variables
public const int kacDilVar = 2; // I added such a variable in case we need to know the number of languages in the future
public KelimelerGirisEkrani kelimelerGirisEkrani = new KelimelerGirisEkrani(); // We publicly fetch the word class we created below here
#endregion
#region System Functions
void Awake()
{
SistemDilAlgilama(); // Calls the function that detects the system language
DilGuncelle(); // Updates and fetches the selected language
DontDestroyOnLoad(this.gameObject); // Instead of redeploying the object in every scene, we prevent it from being destroyed during scene transitions
}
#endregion
#region Special Functions
public void DilGuncelle()
{
/*
* We get the selected language and call the function that places the words in that language
*/
Debug.Log(PlayerPrefs.GetInt("Dil")); // We fetched the selected language
if (PlayerPrefs.GetInt("Dil") == 0) // If the selected language is 0
{
Turkce(); // Set the words to Turkish
}
else if(PlayerPrefs.GetInt("Dil") == 1) // If the selected language is 1
{
Ingilizce(); // Set the words to English
}
}
// Code snippet that detects the system language once
public void SistemDilAlgilama()
{
if (PlayerPrefs.GetInt("FTDD") == 0) // If FTDD is 0, we detect the language (FTDD is my weird attempt at combining Turkish and English words, stands for First Time Dil Detect)
{
// With Switch / Case we fetch the system language
switch (Application.systemLanguage)
{
case SystemLanguage.English: // If system language is English
PlayerPrefs.SetInt("Dil", 1); // Set PlayerPrefs "Dil" to 1, 1 stands for English
PlayerPrefs.SetInt("FTDD", 1); // Set FTDD to 1 so the language detection does not run every time the game opens. Otherwise, if the player changes the language in-game and restarts the game, it would revert to the system language.
Debug.Log("First time language detection system running.." + "\nDetected language: " + Application.systemLanguage.ToString()); // Output to console
break;
case SystemLanguage.Turkish:
PlayerPrefs.SetInt("Dil", 0); // If the system language is Turkish, set "Dil" to 0, 0 stands for Turkish
PlayerPrefs.SetInt("FTDD", 1); // Set FTDD to 1 so the language detection does not run every time the game opens. Otherwise, if the player changes the language in-game and restarts the game, it would revert to the system language.
Debug.Log("First time language detection system running.." + "\nDetected language: " + Application.systemLanguage.ToString()); // Output to console
break;
default:
PlayerPrefs.SetInt("Dil", 1); // If the system language is not one of the above, set the default to English
PlayerPrefs.SetInt("FTDD", 1); // Set FTDD to 1 so the language detection does not run every time the game opens. Otherwise, if the player changes the language in-game and restarts the game, it would revert to the system language.
Debug.Log("First time language detection system running.." + "\nDetected language: " + Application.systemLanguage.ToString()); // Output to console
break;
}
}
else
{
Debug.Log("The language has already been automatically detected before."); // Output this to console if the language system has run before
}
}
#endregion
#region Turkish Words
// Set Turkish words
private void Turkce()
{
kelimelerGirisEkrani.girisYap = "Giriş Yap";
kelimelerGirisEkrani.ePostaAdresiniz = "E-Posta Adresiniz";
kelimelerGirisEkrani.sifreniz = "Şifreniz";
kelimelerGirisEkrani.digerGirisSecenekleri = "Diğer Giriş Seçenekleri";
kelimelerGirisEkrani.hesapOlustur = "Hesap Oluştur";
kelimelerGirisEkrani.girisYapilirkenSorunileKarsilasildi = "Giriş yapılırken bir sorun ile karşılaşıldı!";
kelimelerGirisEkrani.hesapOlusturulurkenSorunileKarsilasildi = "Hesap oluşturulurken bir sorun ile karşılaşıldı!";
kelimelerGirisEkrani.lutfenBosYerBirakmayin = "Lütfen boş alan bırakmayın!";
kelimelerGirisEkrani.hesapOlusturuldu = "Başarılı bir şekilde hesap oluşturuldu.";
kelimelerGirisEkrani.girisBasarili = "Başarılı bir şekilde hesaba giriş yapıldı.";
}
#endregion
#region English Words
// Set English words
private void Ingilizce()
{
kelimelerGirisEkrani.girisYap = "Sign in";
kelimelerGirisEkrani.ePostaAdresiniz = "E-mail";
kelimelerGirisEkrani.sifreniz = "Password";
kelimelerGirisEkrani.digerGirisSecenekleri = "Other sign in options";
kelimelerGirisEkrani.hesapOlustur = "Create Account";
kelimelerGirisEkrani.girisYapilirkenSorunileKarsilasildi = "A problem occurred during sign in!";
kelimelerGirisEkrani.hesapOlusturulurkenSorunileKarsilasildi = "A problem occurred while creating the account!";
kelimelerGirisEkrani.lutfenBosYerBirakmayin = "Please do not leave blanks!";
kelimelerGirisEkrani.hesapOlusturuldu = "Account was created successfully.";
kelimelerGirisEkrani.girisBasarili = "Signed in to account successfully.";
}
#endregion
}
#region Word Basics
/*
I prefer to group words by scene or usage area to keep them from mixing with each other.
When we structure them as classes, it allows us to use the language system a bit like coding in Java.
*/
public class KelimelerGirisEkrani
{
public string girisYap;
public string ePostaAdresiniz;
public string sifreniz;
public string digerGirisSecenekleri;
public string hesapOlustur;
public string girisYapilirkenSorunileKarsilasildi;
public string hesapOlusturulurkenSorunileKarsilasildi;
public string lutfenBosYerBirakmayin;
public string hesapOlusturuldu;
public string girisBasarili;
}
#endregionUsage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DilSistemiKelimeCekme : MonoBehaviour
{
DilSistemi dilSistemi; // We declare a variable to fetch the language system
private void Awake()
{
dilSistemi = GameObject.FindGameObjectWithTag("DilSistemi").GetComponent<DilSistemi>(); // We fetch the language system by its tag
}
private void Start()
{
Debug.Log(DilSistemi.kacDilVar.ToString()); // Will bring the number of languages. Small note here, since kacDilVar is const, you can't assign to it after definition.
KonsolaGirisYapYazdir(); // Since my computer language is English, the first detected language will be English, but this output will update after the language is changed.
DilDegistir(0); // Sets the language to Turkish (0 stands for Turkish)
KonsolaGirisYapYazdir(); // Will output in Turkish since the language has now changed.
}
// Fetching words
void KonsolaGirisYapYazdir()
{
Debug.Log(dilSistemi.kelimelerGirisEkrani.girisYap); // Printed 'Sign in' to the screen.
// If you want to print something else: dilSistemi.+Word class.+Word variable
}
// Changing language
void DilDegistir(int secilenDil)
{
PlayerPrefs.SetInt("Dil", secilenDil); // You can switch to the desired language by passing numbers to 'secilenDil' when calling the function.
dilSistemi.DilGuncelle(); // We updated the language data.
}
}


Yorum Gönder