Translations Diff Tool

Last modified by Vincent Massol on 2021/03/18 11:28

cogWarns you if the keys used for translation aren't present in the translation page
TypeSnippet
Category
Developed by

Jean-Vincent Drean

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

This application looks for all calls to msg.get(...) in the wiki and displays a warning if the used keys aren't present in the translation page

Code

1.1 Translations diff tool

#info("This application looks for all calls to msg.get in the wiki and warns you if the used keys aren't present in the translation page")

<form action="">
Your translation page : <input type='text' name='tr_name' /><br/>
Languages you want to check ~~(optional, comma separated)~~ : <input type='text' name='tr_list' /><br/>
<input type='submit' value='Search for untranslated keys' /><br/>
</form>

<%
import java.util.TreeSet
import java.util.ArrayList
import java.util.Collections
import java.util.regex.Matcher
import java.util.regex.Pattern

if (request.get("tr_name")) {
  String tr_name = request.get("tr_name")
  String excluded_spaces = "Utiyls"

  // Get translations document
  tr_doc = xwiki.getDocument(tr_name)

  // Get translations list
  Map translations = new HashMap<String,String>()
  List available_fr;
  if (request.get("tr_list")) {
    available_tr = Arrays.asList(available_tr = request.get("tr_list").split(","))
  } else {
    available_tr = tr_doc.getTranslationList()
  }
  for (lang in available_tr) {
    translations.put(lang, tr_doc.getTranslatedContent(lang))
  }

  plist = xwiki.searchDocuments("where 1=1 and doc.fullName != '" + tr_name + "' and doc.web != '" + excluded_spaces + "' order by doc.fullName")
  clist = new ArrayList()
  Pattern p = Pattern.compile(/msg\.get\(\"[^\"^\$]+\"\)/);
  ArrayList results = new ArrayList()

  for (item in plist) {
    content = xwiki.getDocument(item).getContent()
    Matcher m = p.matcher(content)
    boolean b = m.find()
 
    while (b) {
      String match = m.group()
      // retrieve msg.get key
      String key = match.substring(9, match.size() - 2)
      // go to next result
      b = m.find()    
      // push result in results list
      if (!results.contains(key)) {
        results.add(key)
      }
    }
  }

  // sort results
  Collections.sort(results)

  // display missing translations in all available languages
  for (current_lang in available_tr) {    
    println ("1.1 Missing translations in : " + current_lang)
    String tr_content = translations.get(current_lang)
    for (current_key in results) {
      if (!tr_content.contains(current_key + "=")) {
        println("" + current_key + "<br/>")
      }
    }
  }
}
%>

Result

Missing translations in : fr

"paragraph"/> ==confirmdelattachment confirmdelwikipage

Missing translations in : en

"paragraph"/> ==

changelogo confirmdelattachment

Get Connected