Savelyev_Vyacheslav

get bad Translation sys_translation

Oct 11th, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const condition_ = `(value!=Updated by^value!=Created by^value!=Created at^value!=Updated at^value!=Work Notes^value!=VCS Record^value!=Additional Comments^value!=ID^column_id.column_name!=dynamic_link_name^value!=Default^sys_created_at>=2024-09-05 12:08:08)^GROUPBYrecord_id&page=1`
  2.  
  3. const ids = getIDsByCondition(condition_)
  4.  
  5. let idsUnic = [...new Set(ids)];
  6. // ss.info( idsUnic );
  7.  
  8. const newCondition = getConditionByTableAndIDs(idsUnic, 'sys_translation')
  9.  
  10. ss.info(newCondition)
  11.  
  12. function getConditionByTableAndIDs(listOfPrb, tableName) {
  13.     // Преобразуем строку с ID в массив
  14.     if(typeof listOfPrb == 'object') {
  15.         listOfPrb = listOfPrb.join(',')
  16.     }
  17.     var prbIds = listOfPrb.split(',').map(id => id.trim())
  18.  
  19.     // Создаем объект GlideRecord для таблицы itsm_incident
  20.     var grIncident = new SimpleRecord(tableName);
  21.  
  22.     // Добавляем запрос для поиска инцидентов по списку ID
  23.     grIncident.addQuery('sys_id', 'IN', prbIds);
  24.  
  25.     return grIncident.getConditionQuery();
  26. }
  27.  
  28. function getIDsByCondition(condition_) {
  29.   let record = new SimpleRecord('sys_translation');
  30.   record.addEncodedQuery(condition_);
  31.   record.orderByDesc('record_id');
  32.   // record.setLimit(30);
  33.   record.query();
  34.   ss.info('всего строк: ' + record.getRowCount())
  35.   const arr = [];
  36.   while (record.next()) {
  37.     arr.push(record.sys_id)
  38.   }
  39.   ss.info('ОШИБКИ = ' + record.getErrors());
  40.   return arr
  41. }
  42.  
  43.  
  44. function getTranslationsByCondition(condition_,  language, tableName) {
  45.     // Define language IDs based on input language
  46.   const languageIDMap = {
  47.     "ru": "156628684306200767", // Russian language ID
  48.     "en": "156628684306541141"  // English language ID
  49.   };
  50.  
  51.   const targetLanguageID = languageIDMap[language]; // Get the appropriate language ID
  52.   const translations = {}; // Object to store translations by record_id
  53.   let record = new SimpleRecord('sys_translation');
  54.   record.addEncodedQuery(condition_);
  55.   record.addQuery('language_id', targetLanguageID);
  56.   record.orderByDesc('record_id');
  57.   record.query();
  58.  
  59.   // Loop through records to collect translations
  60.   while (record.next()) {
  61.  
  62.     const recordID = record.record_id;
  63.     const languageID = record.getValue('language_id')
  64.     const hasProblem = getCountTranslate(recordID)
  65.     const gr = new SimpleRecord(tableName)
  66.     gr.get(recordID)
  67.     let answer = !!gr.sys_id && !!gr.active && !!hasProblem
  68.     // ss.info(answer)
  69.  
  70.     // ss.info('languageID')
  71.     const translationValue = record.value;
  72.  
  73.     if (answer) {
  74.       translations[recordID] = {
  75.         "ru": "",
  76.         "en": ""
  77.       };
  78.     }
  79.  
  80.     // Assign translation value to the correct language key
  81.     if (answer) {
  82.       translations[recordID][language] = translationValue;
  83.     }
  84.   }
  85.  
  86.   // ss.info('Translations object = ' + JSON.stringify(translations));
  87.   return translations;
  88. }
  89.  
  90. // Example use case
  91.  
  92. const language = 'en'; // Set to either 'ru' or 'en'
  93. const translationObj = getTranslationsByCondition(newCondition, language, 'sys_ui_action');
  94.  
  95. ss.info(JSON.stringify(translationObj));
  96.  
  97. function getCountTranslate(recordID) {
  98.   const gr = new SimpleRecord('sys_translation');
  99.   gr.addQuery('record_id', recordID);
  100.   gr.query();
  101.  
  102.   let hasCyrillic = false;  // Flag for Cyrillic characters (Russian)
  103.   let hasLatin = false;     // Flag for Latin characters (English)
  104.  
  105.   while (gr.next()) {
  106.     const value = gr.value;
  107.     const languageID = gr.getValue('language_id');
  108.  
  109.     if (languageID === '156628684306200767') { // Russian language ID
  110.       // Check if the value contains Cyrillic characters (regular expression for Cyrillic)
  111.       hasCyrillic = /[а-яА-ЯЁё]/.test(value);
  112.     }
  113.  
  114.     if (languageID === '156628684306541141') { // English language ID
  115.       // Check if the value contains Latin characters (regular expression for Latin alphabet)
  116.       hasLatin = /[a-zA-Z]/.test(value);
  117.     }
  118.   }
  119.   const answer = !hasCyrillic || !hasLatin;
  120.   // Return true if either condition is not met: no Cyrillic or no Latin characters
  121.   return answer
  122. }
  123.  
  124.  
Advertisement
Add Comment
Please, Sign In to add comment