The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How I translate english to french and german language when a record is created in CI

RiyazAhamed
Tera Contributor

Hello Everyone,

 

I am working on a task where I need to create translated text for the KB Category.

I will be creating the Configuration Item in English it will create a kb category with the same CI name in English, when the kb category is created, its translated text should be needs to be created in sys_translated_text table for French and German.

I don't want to install Dynamic Translation plugin.

I have French Translations and German Translations plugins installed.

 

Thank You!

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @RiyazAhamed ,

 

Ensure you have the French and German translations available for each Configuration Item name. This can be done by maintaining a mapping table or hardcoding it if the list is small.

 Then  you can write script to insert record in sys_translated_text table using Asyn business rule.

For example

 

 

(function executeRule(current, previous /*null when async*/) {
    // Define the translations (This can also be fetched from a custom mapping table or an external source)
    var translations = {
        "Configuration Item Name 1": {
            "fr": "Nom de l'élément de configuration 1",
            "de": "Konfigurationselementname 1"
        },
        "Configuration Item Name 2": {
            "fr": "Nom de l'élément de configuration 2",
            "de": "Konfigurationselementname 2"
        }
    };

    // Get the name of the newly created KB Category
    var categoryName = current.name;

    // Check if translations exist for this category name
    if (translations[categoryName]) {
        // Insert French translation
        var frenchTranslation = new GlideRecord('sys_translated_text');
        frenchTranslation.initialize();
        frenchTranslation.field_name = "name"; // Field to translate
        frenchTranslation.table_name = "kb_category"; // Target table
        frenchTranslation.locale = "fr"; // French locale
        frenchTranslation.document_key = current.sys_id.toString(); // Reference to KB category
        frenchTranslation.value = translations[categoryName]["fr"];
        frenchTranslation.insert();

        // Insert German translation
        var grt= new GlideRecord('sys_translated_text');
        grt.initialize();
        grt.field_name = "name"; // Field to translate
        grt.table_name = "kb_category"; // Target table
        grt.locale = "de"; // German locale
        grt.document_key = current.sys_id.toString(); // Reference to KB category
        grt.value = translations[categoryName]["de"];
        grt.insert();
    }
})(current, previous);

 

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

View solution in original post

5 REPLIES 5

Omkar Mone
Mega Sage

Have you tried writing a after insert BR on kb_category table? Try the below code in the BR and let me know if it works. -  

 

(function executeRule(current, previous /*null when async*/ ) {
    // Define translations for French and German
    var translations = {
        fr: "French translation of " + current.label,
        de: "German translation of " + current.label
    };

    // Create French translation
    var frTranslation = new GlideRecord("sys_translated_text");
    frTranslation.initialize();
    frTranslation.tablename = "kb_category";
    frTranslation.documentkey = "kb_category:" + current.sys_id;
    frTranslation.fieldname = "label"; // Field to be translated
    frTranslation.language = "fr"; // French
    frTranslation.value = translations.fr;
    frTranslation.insert();

    // Create German translation
    var deTranslation = new GlideRecord("sys_translated_text");
    deTranslation.initialize();
    deTranslation.tablename = "kb_category";
    deTranslation.documentkey = "kb_category:" + current.sys_id;
    deTranslation.fieldname = "label"; // Field to be translated
    deTranslation.language = "de"; // German
    deTranslation.value = translations.de;
    deTranslation.insert();
})(current, previous);

Yes, I have tried the same thing, but now I am facing different issue.

When I am creating a CI keeping my profile language in English it works fine, but when I am creating the same with French language profile, one record for English and one for German is created additionally 2 French record is also created. How to avoid it.

 

New record in en = 1 de and 1 fr

New record in fr = 1 de and 1 en

New record in de = 1 en and 1 fr

should create

The duplication issue occurs because the kb_category table lacks a built-in mechanism to identify the language preference of the user who created the record. To resolve this, we can leverage system fields like sys_created_by to identify the creator and retrieve their profile language. Based on this information, we can adjust the script logic so that if a record is created by a French user, only English (en) and German (de) translations are created, and similarly for other languages. This way, we can ensure that unnecessary duplicates are avoided.

 

also see if you can use gs.getSession().getLanguage() to get the info. Hope this helps.

Runjay Patel
Giga Sage

Hi @RiyazAhamed ,

 

Ensure you have the French and German translations available for each Configuration Item name. This can be done by maintaining a mapping table or hardcoding it if the list is small.

 Then  you can write script to insert record in sys_translated_text table using Asyn business rule.

For example

 

 

(function executeRule(current, previous /*null when async*/) {
    // Define the translations (This can also be fetched from a custom mapping table or an external source)
    var translations = {
        "Configuration Item Name 1": {
            "fr": "Nom de l'élément de configuration 1",
            "de": "Konfigurationselementname 1"
        },
        "Configuration Item Name 2": {
            "fr": "Nom de l'élément de configuration 2",
            "de": "Konfigurationselementname 2"
        }
    };

    // Get the name of the newly created KB Category
    var categoryName = current.name;

    // Check if translations exist for this category name
    if (translations[categoryName]) {
        // Insert French translation
        var frenchTranslation = new GlideRecord('sys_translated_text');
        frenchTranslation.initialize();
        frenchTranslation.field_name = "name"; // Field to translate
        frenchTranslation.table_name = "kb_category"; // Target table
        frenchTranslation.locale = "fr"; // French locale
        frenchTranslation.document_key = current.sys_id.toString(); // Reference to KB category
        frenchTranslation.value = translations[categoryName]["fr"];
        frenchTranslation.insert();

        // Insert German translation
        var grt= new GlideRecord('sys_translated_text');
        grt.initialize();
        grt.field_name = "name"; // Field to translate
        grt.table_name = "kb_category"; // Target table
        grt.locale = "de"; // German locale
        grt.document_key = current.sys_id.toString(); // Reference to KB category
        grt.value = translations[categoryName]["de"];
        grt.insert();
    }
})(current, previous);

 

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------