Using the native Unity Package Manager introduced in 2017.2, you can add this library as a package by modifying your
manifest.json
file found at /ProjectName/Packages/manifest.json
to include it as a dependency. See the example below
on how to reference it.
The package is available on the npmjs registry.
{
"dependencies": {
...
},
"scopedRegistries": [
{
"name": "Playdarium",
"url": "https://registry.npmjs.org",
"scopes": [
"com.playdarium.unity"
]
}
]
}
Open Window -> Package Manager
choose Packages: My Regestries
and install package
"com.playdarium.unity.localization": "https://gitlab.com/pd-packages/localization.git#upm"
Create scriptable installer Installers/Localization/LocalizationProjectInstaller
and assign it to your
ProjectContext
key - Has fixed name and can't be customized
language names - Language names can be customized because stored as string value
Example:
key | en | fr | ... |
---|---|---|---|
language | English | French | ... |
... | ... | ... | ... |
Add component LocalizationInjection on your GameObject and assign Components what you need localize. Script LocalizationInjection automatically collect all fields with LocalizationAttribute from assigned components.
For localize static text add attribute LocalizationAttribute to target field. Target field should be private and SerializeField or public.
public class SomeView : MonoBehaviour
{
[Localization("localization.key")]
[SerializeField]
private Text text;
}
For dynamic localization you need provide text contained args pattern. Args pattern you can create using Localizable.ToArgs(params string[] args).
public class SomeView : MonoBehaviour
{
[Localization("localization.key", true)]
[SerializeField]
private Text text;
}
Localization key can contain formatting arguments.
Keys | Values |
---|---|
text.format | "{0}: {1}" |
text.score | "Score" |
public class SomeView : MonoBehaviour
{
[Localization("text.format", true)]
[SerializeField]
private Text textField;
}
...
// Assign text to field in your controller script
textField.text = Localizable.ToArgs("text.score", "100");
// Output: "Score: 100"