TDS Validators – Translated Text Has Corresponding Dictionary Entry

May 1, 2018

Blog | Development | TDS Validators – Translated Text Has Corresponding Dictionary Entry
TDS Validators: Translated Text Has Corresponding Dictionary Entry

In my never-ending quest to improve efficiency with the aid of TDS Classic, I use TDS Validators quite often. Somewhat related to my recent custom validator for ensuring language fallback is set properly, I’ve found the need to ensure all uses of Translate.Text ("Text Key") have a corresponding Dictionary entry in Sitecore. At GeekHive, it’s best practice to wrap any utility text with Translate.Text. Most often, phrases that require translating are words such as: Sort By, More, Order By, Filter, etc. While you can add these directly to a template, they tend to quickly clutter it up. Even worse, it may result in having duplicate words defined on multiple templates. This is where the Sitecore Dictionary excels: It provides a single location for defining translations of these reusable or one-off phrases.

Note: Sitecore also supports Dictionary Domains. Domains allow you to have separate dictionaries per site, or however you would like to separate them. More info can be found here. If using Domains, you’ll need to modify the code below.

Code for TDS Validators

Granted, there’s a lot to digest. The following is a breakdown of what it does:

  1. Consumes a list of absolute folder paths (more on this later)
  2. Searches each folder path for all files matching *.cs* (.cs and .cshtml, will include .css as well, but small overhead)
  3. Searches each matched file for any occurrence of Translate.Text("SOME KEY")
  4. Stores these matches for comparison with Sitecore items
  5. Iterates through all items in current TDS project under path /sitecore/system/dictionary
  6. Outputs any occurrence of a Dictionary Key that does not have a corresponding Sitecore Dictionary item and the corresponding file path
  7. This list outputs in the Warnings section, generated during a standard build of the project

TDS Validators Usage

I recommend that you run this validator locally. Even then, it adds considerable time to the build process (10-15 seconds). I suggest only running it every so often to ensure new entries to the Dictionary haven’t been missed.

Only enable it on a TDS project that contains Dictionary items. At GeekHive, this would never be in an always-deploy TDS project, but an accompanying project to keep track of entries (what we internally would call a Content TDS project).

As previously stated, I recommend only running this validator locally as needed. Note: The name of the validator shows up as GEEKHIVE001. But in fact, this is the DictionaryValidator. The properties contain all folder paths that need to be scanned for instances of Translate.Text("KEY"). As different developer machines could have varying absolute paths (though they shouldn’t), take care in setting these up to receive proper results.

Installing TDS Validators

Installing TDS Validators is relatively painless. I documented the process in brief in my earlier post, and more thoroughly on the official Hedgehog post detailing custom validators.

Our Experiences Using TDS Validators

At GeekHive, we’re always looking for ways to be more efficient. While it’s simple as a developer to quickly write Translate.Text("Filter By") for a label, it’s much harder to remember to add a Dictionary item to make this effort fruitful. This validator has saved us considerable time in searching for missed entries.

John Rappel

.NET Practice Lead
Tags
  • Sitecore
  • TDS
  • TDS Validators

Recent Work

Check out what else we've been working on