Building a Localization Tool in Unity - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is matt gamble and welcome to game dev guide in today's video we're going to take a look at writing a localization tool in the unity editor you might be wondering what localization means or why you'd need to support it in your game let's take a quick look into it localization is the process of making something local in character or restricting it to a particular place in the context of game development this often relates to translating your game into different languages in practice is often achieved by creating a system that supports swapping a placeholder or identify a text out with the correct localized value so for instance if you wanted to display the text frog we might use an identifier or key such as ID frog and then depending on the language we're trying to support we would swap that out for the translated or localized value of that key supporting this early in your pipeline is extremely useful as most games rely heavily on text having to go back through all instances of text in your game and updating it to use a dynamic key instead of a preset fixed value is incredibly tedious and time consuming so when starting a project and working with text if you know you're working with multiple languages or may want to support other languages in the future it'll save you a lot of time and effort to build a localization system into your project I've built a localization system for my current game based around the demands we have for the project I wanted to write a tool that makes it quick and easy to view add edit and remove entries from within unity ok so first things first we're going to need our CSV to read and write from the idea is that we would have the keys and values in this file on each new line there's a couple of ways we could approach the design we could have a separate CSV for each language but that requires maintenance when adding or removing keys should we need to work with multiple languages at once so here we're going to have a master sheet with all our translated values in one place separated by a comma let's create a new file in Visual Studio or your text editor of choice and name it localization now we'll quickly populate it with a couple of test keys and values and save it as a CSV file in the resources folder next we'll create a new class called csv loader this is going to be the class we use to interface directly with our data we'll create a texture set variable could csv file which will be a reference to our file then we'll define some variables for how to pass the sheet properly we'll set the character for the line separator and a character we use to define an entry and finally the separator we'll use for defining each value we encapsulate it like this because we don't want the parser to break up text with commas in it now we'll write a function to load the CSV file and assign it then we want to write our parser function in engine we'll be breaking the values up into each of their languages so we want to pass an attribute ID which will define in the first line of our CSV the plan here is to be able to call this function for each language we want and the function will return a list for each value in that language so we'll create a dictionary to store our elements in will break the text file up into its lines then we get the ID headers we add it to the top of our CSV so our function can identify the location of each language value in our list again this method allows us to add more languages easily by simply adding a new value to the end of each line and an identifier at the head of our file then we write this regular expression to define the parameters of splitting the line up and we go through each line in the list breaking the lines into fields and trimming off the quotation marks surrounding our values we then make sure there's enough fields in the line and if there is we assign the first field in the line is the key and check if the dictionary already contains that key if it doesn't we assign the element at the attribute index as the value and then add both of these to the dictionary and then return the dictionary now we've set up a way to read the CSV we can start working on the localization system itself let's create a new class called localization then let's write a public enum and to find a couple of languages for now we'll just use English and French we'll also create a static reference to the current language and set it to English next we'll create two dictionaries one for our English values and one for our French values with that set up we need to write two functions one for initializing all of the values and another for returning a localized value to a text element so first we'll make a new static boolean called is in it then we'll write a function called init and we'll use this to initialize our localization system and read the data from the CSV once the CSV is loaded we simply assign the dictionary to each of our languages using their attribute IDs so for English it's en and for French fr and then set an it to true to tell the system we've loaded the values finally we'll write the get localized value function to return the value based on the key passed into the function will tell the system to initialize if it hasn't already and then set the correct value based on the currently selected language and return it and that's it we now have a functional localization system while almost part of the usefulness of the design of this system is how it will work automatically across your project ideally we want all of the text elements in our project to display the correct localized value so we need to write a quick script which will apply to any text element we want localized let's create a new mono behavior I'm forcing it to require tex-mex tis SDF but this would work fine with standard text rendering if you're still using that so we set a string as the key and then when the object is started tell the text box to set itself to the localized value returned from the localization system based on the key so I've thrown together a number of different text boxes representing some values in the system and as you can see when we run the project the text from the localization system displays accordingly and if we swap the language over it updates to show the selected language instead so that's the basic framework for a localization system in your project hopefully you can see how flexible this can be and find it useful this is just the tip of the iceberg though in the next part I'll go through expanding the system to allow us to edit remove and replace values inside of unity which means we'll also be able to write a couple of neat editor tools for easily navigating values as well as a custom class we can use in our project specifically for localized text if you've enjoyed this video be sure to hit the like button and leave a comment and of course if you're interested in more game dev tips tricks and tutorials be sure to subscribe I'll see you next time thanks for watching
Info
Channel: Game Dev Guide
Views: 39,625
Rating: undefined out of 5
Keywords: unity, learn, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games, localisation, localization, translating text, text m
Id: c-dzg4M20wY
Channel Id: undefined
Length: 8min 24sec (504 seconds)
Published: Sun Mar 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.