Search Unity

Unity3D Tutorial - Localisation of text and graphics in C#

Discussion in 'Scripting' started by NerdRageStudios, Dec 26, 2016.

  1. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Hi all, I have just completed the first couple of tutorials in my new series and thought I would share them with you here. This one is around localisation and takes a simple approach to making localisation easy in your games. I really hope it is of some help to someone out there :)

    http://www.nerdrage.co.uk/tutorials/localisation-text-graphics-unity3d/

    Merry christmas and a happy new year to you all :)
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    That's a very wet code you wrote there, You basically have these lines:
    Code (CSharp):
    1. string dialogString = dialogLoaded.ToString();
    2.             dialogLines = dialogString.Split("\n"[0]);
    3.  
    4.             string interfaceString = interfaceLoaded.ToString();
    5.             interfaceLines = interfaceString.Split("\n"[0]);
    6.  
    7.             string messageString = messagesLoaded.ToString();
    8.             messagesLines = messageString.Split("\n"[0]);
    9.  
    10.             string levelString = levelsLoaded.ToString();
    11.             levelLines = levelString.Split("\n"[0]);
    repeated for each language in your code, you should solve that, then if you're already there, you could use some trickery, to get rid of the whole thing, eg. like this:
    Code (CSharp):
    1. if language is english then
    2.     curDialog = englishDialog
    3.     curInterface = englishInterface
    4.     ...
    5. else if language is spanish then
    6.     ...
    The you can just simply change every Resouces.Load("Languages/" + englishInterface) to ("Languages/" + curInteface)

    Same with texture loading
     
    Last edited: Dec 26, 2016
    NerdRageStudios likes this.
  3. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Now that you point it out, its so obvious :) thank you, thats a really helpful suggestion and I shall change it to reflect that :D
    Thanks for taking the time to read it and comment!
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    It would be even better, if you would do something like this:
    variables:
    public string dialogPrefix, levelPrefix, etc; // = "Dialog", "Level"

    Then you give the enums base value:
    enum Languages {
    English = "English",
    French = "French",
    etc...
    }

    then you can simply get the correct file with:
    Resources.Load(dialogPrefix + "_" + (string)language)
     
    NerdRageStudios likes this.
  5. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Thanks! Thats super helpful, and I have now just removed a block of totally unecessary code from the game :) Every day is a school day :) I'll now update my tutorial :D
     
    gorbit99 likes this.