Friday, January 11, 2019

Translating Text in Sitecore using Microsoft Cognitive Services and PowerShell


Microsoft Cognitive Services is an impressive set of powerful, easy to integrate APIs for developers. With AI at its core, Cognitive Services strives to solve business problems using Vision, Speech, Knowledge, Search, and Language service offerings.  Recently announced, it's now easier than ever to start using Cognitive Services with a simplified key mechanism.

Creating a Cognitive Service in Azure itself is dead simple. It take less than 5 minutes to spin up a Cognitive Service resource and you're ready to develop. The above video will help get you started.☝

So, what can we do with these services in Sitecore?

Cognitive Services Is Not That New

We've seen folks across the Sitecore community utilising this technology since its inception in 2016 - specifically the Computer Vision API. From bulk image alt tagging using Computer Vision and PowerShell and automatic image alt tag generation module, to a fully integrated Cognitive Services module from the infamous Mark Stiles.

These are really great adaptations of these APIs, but I wanted to explore these services myself.

Translator Text API

The Translator Text API is easy to integrate in your applications, websites, tools, and solutions. It allows you to add multi-language user experiences in more than 60 languages, and can be used on any hardware platform with any operating system for text-to-text language translation.
The Translator Text API is part of the Azure Cognitive Services API collection of machine learning and AI algorithms in the cloud, and is readily consumable in your development projects.
What is Translator Text API?

Being completely obsessed with PowerShell, I figured I'd give it a go.  For my demo, I want to allow content authors to right-click on a Sitecore item, select a Translate Text Fields PowerShell script, configure some option in a dialog and click Continue.



The script should create a new language version in the target language selected and translate all text fields selected.

Pricing

Before starting, it should be noted that the Free tier for translations is 2 million characters per month. 
Since we're keeping this integration simple, it's not likely we'll hit the limit.



Let's Script It

Building the dialog is pretty straightforward. Get the context item, grab the applicable languages and fields (Single-Line and Multi-Line only for simplicity and API restriction avoidance), and set the dialog options:

We'll need a function that we can call to process the item after obtaining the selections from the dialog.  We expect to call it like this:

Translate-Item -TargetItem $item -FromLang $selectedFromLanguage -ToLang $selectedToLanguage -FieldsList $fieldSelectionArray

The function should issue an authentication token from the Cognitive Service, generate a new language version for the item in the target language, and call another function to translate the original field value to the target language:

The Issue-CognitiveApiToken in the above function issues the authentication token we need to send with our request to the translation API.

We can send a request to the translation API with the issued token in a new function that will be responsible for processing the actual translation:

Putting It All Together

Now that we've got commands to interact with the Translation API, we can build out the contextual PowerShell script to provide some options for our content author.

Adding this script to one of the Context Menu (PowerShell Script Library) folders (eg. /sitecore/system/Modules/PowerShell/Script Library/SPE/Core/Platform/Content Editor/Context Menu) will allow the script to be executed contextually.


Final Result


I can see this being a good use-case for Dictionary items specifically as there are some restrictions for content length size that the API will reject.  However, this is simply a demo of what is possible here (definitely not production-ready ðŸĪŠ).



Feel free to take and use any part of the script here and build something cool!

Wednesday, January 2, 2019

Sitecore Icon Search v2.0 and Firefox Support


With the holidays behind us and 2019 in full swing, I’m excited to share new updates to the Sitecore Icon Search browser extension offering.  Considering I had some significant time off from work, I was able to pack in several exciting new features in between holiday festivities that I hope will make the experience even better for Sitecore developers.

Chrome Extension v2.0

Improved Search with Synonyms

When the Sitecore Icon Search Web App was released, one suggestion that stood out was



It's true, the search relied solely on the icon's name.  Unless you get the right name, you may miss an icon that may work better in your situation.

Well, phase 2 is here!

Utilizing the Google Vision API and Datamuse API, I wrote a couple of PowerShell scripts to process each icon in the dataset through each respective API and added the results into a new ‘attributes’ column to the existing data file (this column is hidden from general view).

The Google Vision API processed the image itself and primarily provided color attributes for every icon.  When searching, specifying a color may narrow down or help target the right icon.


Using the Datamuse API, I passed in the name of each icon and returned a maximum of 10 synonyms per icon and stored it into the ‘attributes’ column as well.   You can now search for a generalized term and likely retrieve new icons that may work for you



Offline / Localized Environment Support

Another feature that’s been requested has been to allow the extension to function when an internet connection is unavailable.   This makes plenty of sense as development is often done locally and doesn’t always require an online connection.

The problem stems from how the extension loads the icon images.  Up until now, the source of the icons has always been direct references to Sitecore’s website.  Images are lazy loaded based on the user’s search parameters.  When you lose internet connection, for icons that haven’t cached, you’d see broken images:

Not cool.

The extension is now 'context aware' and detects when you’re within the Sitecore shell.  Therefore, the extension will rely on the current local Sitecore URL to load icons (instead of relying on Sitecore’s domain).  This allows the extension to work offline.

Additionally, icons that are not available for the specific version of Sitecore will display a message stating it.

When you attempt to load the extension outside of a local Sitecore solution while offline, a pop-up will display:


If you select Offline Settings, you’ll be taken to the options menu where you can enable the Image URL Override option and set a URL domain to a local Sitecore instance.  By enabling this setting, offline the extension will always use this local domain vs the Sitecore domain, allowing more control in offline mode.



Firefox Add-on v1.0


via GIPHY

I realize that not everyone uses Google Chrome as their primary browser.  Even I use Firefox as my backup browser.  Luckily, Firefox offers a very similar development structure for ‘Add-ons’.  With some fiddling, I managed to port over the Chrome extension to a Firefox Add-on. 😊👍

Version 1.0 contains all the same features built for Google Chrome up to now.

Add it here!


As always, I'm open for feedback.  DM me on Twitter for any bugs, issues, or suggestions.