Name

sn_cd.cd_ContentTranslations

Description

Utility for getting translated messages from the sys_translated_text table for content, or interfacing with Localization Framework to achieve this

Script

var cd_ContentTranslations = Class.create();
cd_ContentTranslations.prototype = {
  initialize: function() {
  },

  /** Get an object with notificiation subject and body in appropriate language
   * @param notificationGr GlideRecord The content record containing the email subject/body
   * @param langCode String The language code to attempt to find a body/subject for
   * @return Object object containing subject and body in the correct language
   */
  getTranslatedEmailContent: function(notificationGr, langCode) {
      var notificationSysId = notificationGr.getUniqueValue();

      // Get Subject
      var translationsGr = new GlideRecord("sys_translated_text");
      translationsGr.addQuery("documentkey", notificationSysId);
      translationsGr.addQuery("fieldname", "subject");
      translationsGr.addQuery("language", langCode);
      translationsGr.setLimit(1);
      translationsGr.query();
      var subject = translationsGr.next() ? translationsGr.getValue("value") : notificationGr.getValue("subject");

      // Get Body
      var body = "";
      if (notificationGr.use_block && notificationGr.block) {
          translationsGr = new GlideRecord("sys_translated_text");
          translationsGr.addQuery("documentkey", notificationGr.getValue("block"));
          translationsGr.addQuery("fieldname", "rich_text");
          translationsGr.addQuery("language", langCode);
          translationsGr.setLimit(1);
          translationsGr.query();
          if (translationsGr.next())
              body = translationsGr.getValue("value");
          else {
              var blockGr = new GlideRecord("sn_cd_block");
              if (blockGr.get(notificationGr.getValue("block")))
                  body = blockGr.getValue("rich_text");
          }
      } else {
          translationsGr = new GlideRecord("sys_translated_text");
          translationsGr.addQuery("documentkey", notificationSysId);
          translationsGr.addQuery("fieldname", "rich_text_plain_html");
          translationsGr.addQuery("language", langCode);
          translationsGr.setLimit(1);
          translationsGr.query();
          body = translationsGr.next() ? translationsGr.getValue("value") : notificationGr.getValue("rich_text_plain_html");
      }

      return {
          "body" : body,
          "subject" : subject
      };
  },

  getContentTranslationStates: function(contentSysId, table, languages) {
  	var contentGr = new GlideRecord(table);
  	if (!contentGr.get(contentSysId)) {
  		gs.addErrorMessage(gs.getMessage('Content Publishing: Content Translations - Content {0} not found', contentSysId));
  		return;
  	}
  	
  	// Get fields we care about for translations
  	var translatedFields;
  	switch (table) {
  		case cd_CommonConstants.CONTENT_TABLE_PORTAL:
  			translatedFields = cd_CommonConstants.PORTAL_TRANSLATABLE_FIELDS[contentGr.content_type.value];
  			break;
  		case cd_CommonConstants.CONTENT_TABLE_BLOCK:
  			translatedFields = cd_CommonConstants.BLOCK_TRANSLATABLE_FIELDS;
  			break;
  		case cd_CommonConstants.CONTENT_TABLE_TODO:
  			translatedFields = cd_CommonConstants.TODO_TRANSLATABLE_FIELDS[contentGr.content_type.value];
  			break;
  		case cd_CommonConstants.CONTENT_TABLE_NOTIFICATION:
  			translatedFields = cd_CommonConstants.NOTIFICATION_TRANSLATABLE_FIELDS[contentGr.content_type.value];
  			break;
  		default:
  			return {
  				translated : [],
  				notTranslated : [],
  				notSupported : languages
  			};
  	}

  	var translatedFieldsByLang = {};
  	for (var i = 0; i < languages.length; i++)
  		translatedFieldsByLang[languages[i]] = this._createTranslatedArrayClone(translatedFields, contentGr);

  	var translationsGr = new GlideRecord('sys_translated_text');
  	translationsGr.addQuery('documentkey', contentSysId);
  	translationsGr.query();

  	while (translationsGr.next()) {
  		var language = translationsGr.getValue('language');
  		var fieldName = translationsGr.getValue('fieldname');
  		if (translatedFields.indexOf(fieldName) >= 0)
  			if (translationsGr.getValue('value') || !contentGr.getValue(fieldName))
  				translatedFieldsByLang[language].splice(translatedFieldsByLang[language].indexOf(fieldName), 1);
  	}

  	var result = {
  		translated : [],
  		notTranslated: [],
  		notSupported: []
  	};
  	
  	for (var langCode in translatedFieldsByLang) {
  		if (translatedFieldsByLang[langCode].length === 0)
  			result.translated.push(langCode);
  		else
  			result.notTranslated.push(langCode);
  	}

  	return result;
  },
  
  _createTranslatedArrayClone: function(array, removeEmptyGr) {
  	var result = [];
  	for (var i = 0; i < array.length; i++)
  		if (!removeEmptyGr || removeEmptyGr.getValue(array[i]))
  			result.push(array[i]);
  	return result;
  },

  /**
   * Given artifact details and a target language, return an LFDocument Content object for LF to translate with
   * @param {Object} params Contains translation request details
   * @param {String} params.tableName Provides the table name of the artifact record (table it references)
   * @param {String} params.sysId sysId of the artifact record
   * @param {String} params.language target language code for the translation request
   * @returns {Object} An LFDocumentContent object with translation details
   */
  getLFTranslatableContent: function(params) {
      // Get data from params
      var tableName = params.tableName;
      var sysId = params.sysId;
      var language = params.language;
      var additionalParams = params.additionalParameters;
      var sourceId;
      var targetId;

      if (additionalParams && additionalParams.sourceID) { // Translated record exists
          sourceId = additionalParams.sourceID;
          targetId = sysId;
      } else // Translated record does not exist
          sourceId = sysId;

      var contentGr = new GlideRecord(tableName);
      if (!contentGr.get(sourceId)) {
          gs.error("Content Publishing Localization Framework: Could not get ID " + sourceId + " from table " + tableName);
          return;
      }

      var groupName = contentGr.getValue("title") || contentGr.getValue("name");
      var lfDocumentContentBuilder = new global.LFDocumentContentBuilder("v1", language, sysId, tableName, additionalParams);
      lfDocumentContentBuilder.processTranslatableFieldsForSingleRecord(contentGr, groupName);
      return lfDocumentContentBuilder.build();
  },

  /**
   * Given a record, return true if this content is translatable by LF, false otherwise
   * @param {GlideRecord} contentGr A GlideRecord that is from a child table of sn_cd_content_base
   * @returns {Boolean} True if a request can be made for this content, false otherwise
   */
  validateContentLFTranslatable: function(contentGr) {
      var tableName = contentGr.getTableName();

      if ((tableName === cd_CommonConstants.CONTENT_TABLE_PORTAL && contentGr.content_type.toString() !== cd_CommonConstants.CONTENT_TYPE_CALENDAR) ||
          (tableName === cd_CommonConstants.CONTENT_TABLE_NOTIFICATION && contentGr.content_type.toString() === cd_CommonConstants.CONTENT_TYPE_EMAIL) ||
          (tableName === cd_CommonConstants.CONTENT_TABLE_TODO))
          return gs.getProperty(cd_CommonConstants.PROPERTY_LF_INTEGRATION, false);
      return false;
  },

  type: 'cd_ContentTranslations'
};

Sys ID

fa599238c04e4110fa9bdc97ae72beb5

Offical Documentation

Official Docs: