js2
js2

Reputation: 111

Get count of characters for translation in Kentico Cloud

Is there a way to tell the count of characters of all text fields in some of our content items? We need to estimate a translation price for our content items.

Upvotes: 0

Views: 37

Answers (1)

js2
js2

Reputation: 111

You can use Delivery API to retrieve your items and run a quick javascript to count the characters for you. First, get all your items (or a subset, depending on what you need) with the call excluding all the modular content (linked items) like this:

https://deliver.kenticocloud.com/<projectid>/items?depth=0​​​​​​​

Then you can use browser console to run this piece of code:

var response = JSON.parse(document.getElementsByTagName("BODY")[0].textContent);
var noOfChars = 0;
for (var x = 0; x < response.items.length; x++) {
    var p = response.items[x].elements;
    for (var key in p) {
        if (p[key].type=='rich_text' || p[key].type=='text') {
            noOfChars += strip(p[key].value).length;
        }
    }
}
noOfChars;

function strip(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}

And hit enter. This is what the result will look like:

Browser console showcase

Upvotes: 1

Related Questions