# Custom Localizer Support

#### <a class="anchor" id="bkmrk-"></a>Requirements for Custom Localizer

In order to use a custom Localizer, a CMake feature switch CANDERA\_CUSTOM\_LOCALIZER\_ENABLED has to be enabled. This flag will disable the usage of DefaultLocalizer which loads LanguagePacks from an asset.

In addition, please ensure that the following are fulfilled:

<div class="contents" id="bkmrk-candera-globalizatio"><div class="contents"><div class="textblock">- <span style="color: rgb(230,126,35);">[Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]")</span> Globalization sources available
- CMake setting CANDERA\_GLOBALIZATION\_ENABLED is enabled

</div></div></div>#### <a class="anchor" id="bkmrk--0"></a>Defining a Custom Localizer

A custom Localizer class (eg: CustomLocalizer) should derive from abstract class <span style="color: rgb(230,126,35);">[Candera::Globalization::Localizer](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_globalization_1_1_localizer.html)</span> and provide implementations for the pure virtual functions:

<div class="contents" id="bkmrk-candera%3A%3Aglobalizati"><div class="contents"><div class="textblock">- <span style="color: rgb(230,126,35);">[Candera::Globalization::Localizer::SetCurrentCulture](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_globalization_1_1_localizer.html#a9bc595d40e6576279113dc52ee5f440e "Will be called by the CultureManager whenever the current culture is changed. This triggers the Defau...")</span>
- <span style="color: rgb(230,126,35);">[Candera::Globalization::Localizer::GetTextBaseString](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_globalization_1_1_localizer.html#af13307c5801287b4900a974014f46e11 "Retrieves the localized base string for the given string ID.")</span>
- <span style="color: rgb(230,126,35);">[Candera::Globalization::Localizer::GetTextType](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_globalization_1_1_localizer.html#ac96d52f14265ae9f1b6d1e624bdf130d "Retrieves the TextType for the given string ID.")</span>

</div></div></div>For consistency, use <span style="color: rgb(230,126,35);">[FEATSTD\_TYPEDEF\_SHARED\_POINTER(CLASS)](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf2bcc08fa848e582cf0434c73bc771c3)</span> and <span style="color: rgb(230,126,35);">[FEATSTD\_SHARED\_POINTER\_CREATE\_DECLARATION()](http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gab388090f9bc82928ee1c55ae18e2c1e7)</span> macros to make the class manageable by SharedPointer.

Please note that FEATSTD\_SHARED\_POINTER\_DECLARATION() macro should not be used in this case, because some base class methods will be overwritten and SharedPointer memory management will not work correctly.

Implement corresponding <span style="color: rgb(230,126,35);">[Create()](http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857)</span> method like in the following snippet:

```
/******************************************************************************
  Create
 ******************************************************************************/
CustomLocalizer::SharedPointer <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">CustomLocalizer::Create</a>()
{
    return CustomLocalizer::SharedPointer(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___m_e_m_o_r_y_m_a_n_a_g_e_m_e_n_t.html#gaf232237bb1f8faa2509fe9aa71a81d53">FEATSTD_NEW</a>(CustomLocalizer));
}
```

#### <a class="anchor" id="bkmrk--1"></a>Example: Using a Custom Localizer in Applications

##### <a class="anchor" id="bkmrk--2"></a>Define cultures

Create new cultures and add them to the list of available cultures of <span style="color: rgb(230,126,35);">[Candera::Globalization::GlobalizationCultureManager](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_globalization_1_1_globalization_culture_manager.html)</span>:

```
    m_cultureManager.RemoveAllCultures();
    for (UInt16 i = 0; i < cultureListSize; ++i) {
        Candera::Globalization::Culture::SharedPointer culture = <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">Culture::Create</a>();
        culture->SetLocale(cultureList[i].locale);
        culture->SetTextDirection(cultureList[i].textDirection);
        culture->SetDisplayName(cultureList[i].locale);
        m_cultureManager.AddCulture(culture);
        m_cultureVct.Add(culture);
    }
```

Set default culture to be used in case no current culture is set. This step is mandatory since the default culture will be used for initialization.

```
        m_cultureManager.SetDefaultCulture(m_cultureVct[0]);
```

##### <a class="anchor" id="bkmrk--3"></a>Use a custom localizer

Declare a custom localizer:

```
    CustomLocalizer::SharedPointer m_localizer1;
    CustomLocalizer::SharedPointer m_localizer2;
```

Create the custom localizer:

```
    m_localizer1(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">CustomLocalizer::Create</a>()),
    m_localizer2(<a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___c_o_u_r_i_e_r___v_i_s_u_a_l_i_z_a_t_i_o_n.html#ggaf0e93dc4242f607c3c8d13dafd036af9aaabfa5309af24986d3111a092449f857">CustomLocalizer::Create</a>())
```

After a custom localizer has been created, the new localizer needs to be registered to the CultureManager:

```
    GlobalizationCultureManager::GetInstance().SetLocalizer(m_localizer1);
```

Initialize culture by setting current culture. If current culture is not set, default culture will be used.

```
        m_cultureManager.SetCurrentCulture(m_cultureVct[0]);
```

##### <a class="anchor" id="bkmrk--4"></a>Switching localizer during run-time

Since only one localizer is allowed to be registered, in order to set a new localizer, the previous one has to be unregistered by calling SetLocalizer(0), like in the following snippet.

```
            GlobalizationCultureManager::GetInstance().SetLocalizer(CustomLocalizer::SharedPointer(0));
            GlobalizationCultureManager::GetInstance().SetLocalizer((toggleLocalizer1) ? m_localizer1 : m_localizer2);
```

Corresponding localizer cultures have to be added to the CultureManager and default culture has to be defined if no current culture is available.