Skip to content

VRLabs/Unity-Localization-Core

 
 

Repository files navigation

Note: this fork goes against Dreadrith's, the Original Author, wishes. Is is however compliant with the original OS license.

Unity-Localization-Core

Generic badge Generic badge Generic badge Generic badge

Generic badge Generic badge

A framework allowing for easy Localization of scripts in Unity.

Preview


What it does

Unity-Localization-Core is framework for Unity script localization. It comes with a handy UI and clean interface for localization.

It can be used in two ways:

  • Bundled with your script
  • As a dependency

To allow for a nice overview during translation, you can use the Unity-Localization-Inspector package.

Features

  • Single method call to create a language selection dropdown
  • Localization keys as enum values, so you'll never misspell them
  • Language setting saved across projects

How to use

Bundling

There are two options here, either:

As a dependency:

  • Add this package as a VPM dependency/UnityPackage Dependency
  • Add a reference to the VRLabs.Unity-Localization-Core.Editor assembly definition

or bundled:

  • Copy this entire package into your code
  • Regenerate all the GUIDs, probably using some sort of tool
  • Rename the Assembly Definition file and name
  • Add a reference to this new assembly definition to your own assembly definition

Code

To start off, create a class that inherits from LocalizationScriptableBase, with a title in the "hostTitle" field.

public class AV3ManagerLocalization : LocalizationScriptableBase
{
    public override string hostTitle => "Avatar 3.0 Manager Localization";
}

Then, create an enum containing the values you want for every category (for now we'll only do one).

public class AV3ManagerLocalization : LocalizationScriptableBase
{
    public override string hostTitle => "Avatar 3.0 Manager Localization";

    public enum Keys
    {
        Merger_AnimatorMode,
        Merger_Animator,
        Merger_Parameters,
        Merger_Suffix,
        Merger_SelfMergeWarning,
        ...
    }
}

Afterwards, return a list of KeyCollections with their names and values for the keyCollections property.

public class AV3ManagerLocalization : LocalizationScriptableBase
{
    public override string hostTitle => "Avatar 3.0 Manager Localization";

    public override KeyCollection[] keyCollections => new[] { new KeyCollection("Avatar 3.0 Manager Localization", typeof(Keys)) };
    
    public enum Keys
    {
        Merger_AnimatorMode,
        Merger_Animator,
        Merger_Parameters,
        Merger_Suffix,
        Merger_SelfMergeWarning,
        ...
    }
}

Now, you can use it in the code (here the class is instantiated as a singleton, but you can do this however you want):

        private static LocalizationHandler<AV3ManagerLocalization> _localizationHandler;
        public static LocalizationHandler<AV3ManagerLocalization> LocalizationHandler
        {
            get
            {
                if (_localizationHandler == null)
                    _localizationHandler = new LocalizationHandler<AV3ManagerLocalization>();
                return _localizationHandler;
            }
        }
        
        ...
            
        public void OnGui()
        {
            EditorGUILayout.Label(LocalizationHandler.Get(Merger_AnimatorMode).text);    
        }

Now to let the user choose their own language, you can call LocalizationHandler.DrawField() on your LocalizationHandler, and the dropdown will be drawn for you.

Initial Language Setup

For this part, you have to use Unity-Localization-Inspector package.

  • Create a new Localization file by right-clicking the Assets window and clicking Create -> DreadScripts -> Localization File.
  • Select your Class in the list of Classes to Translate.
    • If you don't see your class show up and you are using the Bundled option, you can temporarily change your Assembly Definition to point to the VRLabs install of Unity-Localization-Core, not your own, as Unity-Localization-Inspector only works with the default install.
  • Fill in the language name (in that language, so e.g. Dutch becomes Nederlands, etc.) and start filling in values.

License

Unity-Localization-Core is available as-is under MIT. For more information see LICENSE.

About

The core part of the localization. Can be used on its own.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 98.9%
  • Batchfile 1.1%