Programmatically Updating Resource Strings in Kentico

December 14th, 2017 / 5 MINUTE READ / BY LAURA FRESE

Kentico makes it easy to create and export custom resource strings for localizing text fields on multilingual sites. Unfortunately, importing resource strings is not as straight forward due to the format in which the translated localization strings are returned. Below you will find a block of code that will help you to programmatically import resource strings.

CultureInfo cult = CultureInfoProvider.GetCultureInfo(culturecode);//culture code of the imported dataif (cult != null){ResourceStringInfo rx = ResourceStringInfoProvider.GetResourceStringInfo(key);if (rx != null){int stringid = rx.StringID;ResourceTranslationInfo rti = ResourceTranslationInfoProvider.GetResourceTranslationInfo(stringid, cult.CultureID);if (rti == null){rti = ResourceTranslationInfo.New();rti.TranslationStringID = stringid;rti.TranslationCultureID = cult.CultureID;rti.TranslationText = translatedText;rti.Insert();rx.Update();}else{rti.TranslationStringID = stringid;rti.TranslationCultureID = cult.CultureID;rti.TranslationText = translatedText;rti.Update();rx.Update();}}}

As you can see, Kentico has the resource string and the resource translation as seperate objects. Once we get the resource string using the key, we are able to get or create the resource translation. The method in which you choose to import the data is up to you. My localization strings were imported from an excel file.