# Scripting Reference

### Localization Settings

```csharp
// To get localization settings:
LocalizationSettings localizationSettings = LocalizationSettings.Instance;

// You can get available languages as:
List<SystemLanguage> availableLanguages = localizationSettings.AvailableLanguages;

// To access Google auth file:
TextAsset authFile = localizationSettings.GoogleAuthenticationFile;
```

### Localization Manager

```csharp
// You can get current language:
SystemLanguage currentLanguage = Localization.Instance.CurrentLanguage;

// or you can set current language:
Localization.Instance.CurrentLanguage = SystemLanguage.English;

// or set by system language:
Localization.Instance.SetSystemLanguage();

// or set by default language defined in LocalizationSettings (first item is the default language):
Localization.Instance.SetDefaultLanguage();

// Register application locale changed event:
Localization.Instance.LocaleChanged += (object sender, LocaleChangedEventArgs e) => 
{
    Debug.Log("Application locale has changed from " + e.PreviousLanguage + " to " + e.CurrentLanguage);
};
```

{% hint style="warning" %}
Use `Localization.Instance` if only if application is playing. See [Application.isPlaying](https://docs.unity3d.com/ScriptReference/Application-isPlaying.html).
{% endhint %}

### Extending Custom Localized Asset

Creating custom localized asset is involved simple steps:

* Extend class from `LocalizedAsset<T>`, enter your asset type for generic parameter:

```csharp
[CreateAssetMenu(fileName = "MyLocalizedCustomAsset", menuName = "GameToolkit/Localization/My Custom Asset")]
public class MyLocalizedCustomAsset : LocalizedAsset<MyCustomAsset>
```

* Your custom localized asset automatically registered under `Localization Explorer -> Create`menu if you set `menuName` property of `CreateAssetMenu` attribute as `"GameToolkit/Localization/<your_asset_name>"`
* Create serializable asset item by extending `LocaleItem<T>`, enter your asset type for generic parameter again (it is necessary for the Unity to serialize object):

```csharp
[Serializable]
private class MyCustomLocaleItem : LocaleItem<MyCustomAsset> { };
```

* Define locale items array with concrete type you declared:

```csharp
[SerializeField]
private MyCustomLocaleItem[] m_LocaleItems = new MyCustomLocaleItem[1];
```

* Finally, implement getter method for getting locale items with the base class type:

```csharp
public override LocaleItemBase[] LocaleItems { get { return m_LocaleItems; } }
```

&#x20;Complete code:

```csharp
using System;
using UnityEngine;
using GameToolkit.Localization;

[CreateAssetMenu(fileName = "MyLocalizedCustomAsset", menuName = "GameToolkit/Localization/My Custom Asset")]
public class MyLocalizedCustomAsset : LocalizedAsset<MyCustomAsset>
{
    [Serializable]
    private class MyCustomLocaleItem : LocaleItem<MyCustomAsset> { };

    [SerializeField]
    private MyCustomLocaleItem[] m_LocaleItems = new MyCustomLocaleItem[1];

    public override LocaleItemBase[] LocaleItems { get { return m_LocaleItems; } }
}
```

Congratulations! You have a custom localized asset that can use your game.

### Extending Custom Localized Asset Behaviour

If you want to extend localized asset behavior, you have two options:

* If you want to implement completely custom behavior, you should extend from `LocalizedAssetBehaviour`.
* If you want to create generic component & property based behavior for your custom localized asset, then you should extend from `LocalizedGenericAssetBehaviour`.

**1. Extending from LocalizedAssetBehaviour**

You must extend your class from `LocalizedAssetBehaviour` and override the `TryUpdateComponentLocalization()` appropriately. This method is invoked every-time when game starts or application language has changed. You should update the component property with your custom localized asset's value.

```csharp
public class MyLocalizedAssetBehaviour : LocalizedAssetBehaviour
{
    public MyLocalizedCustomAsset LocalizedAsset;

    protected override bool TryUpdateComponentLocalization(bool isOnValidate)
    {
        // Update the specified property with current value.
        ... = LocalizedAsset.Value;
        
        // or using safe value getter.
        ... = GetValueOrDefault(LocalizedAsset);
        
        // If component is updated successfully.
        return true;
    }
}
```

**2. Extending from LocalizedGenericAssetBehaviour\<TAsset, TType>**

The only step you need to take is the extend from `LocalizedGenericAssetBehaviour` and specify your custom asset type for the first generic parameter, and specify your asset value type as the second generic parameter. That's it!

```csharp
public class MyLocalizedAssetBehaviour : LocalizedGenericAssetBehaviour<MyLocalizedCustomAsset, MyCustomAsset>
{
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hibrahimpenekli.gitbook.io/gametoolkit-localization/scripting-reference.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
