World is now on Opti ID! Learn more


May 29, 2016
  2432
(0 votes)

Missing translations in XML translation files

Most Episerver sites involve working with language files. These files are sadly often neglected during the development phase and give both developers and editors a headache when launch date closes in on the project.

There is no silver bullet to magically solve the challenges of translation, but I will here give a small contribution on how to gain insight on missing translation texts.

The Episerver Localization Service provides [Missing text….] output to spot missing translations, but often these texts are hidden I e-mail templates, validation texts or other, not so visible, places.

 

The image below shows a Spanish version of Alloy where translation fallback have been turned off. There are several [Missing text ‘key’ for Spanish] displayed.

image

 

In the Alloy sample site the translation files are mostly located in the Views.xml file in the Resources directory and this file is referred in the web.config file

   1: <episerver.framework>
   2: ....
   3:     <localization fallbackBehavior="Echo, MissingMessage">
   4:         <providers>
   5:             <add virtualPath="~/Resources/LanguageFiles" name="languageFiles" type="EPiServer.Framework.Localization.XmlResources.FileXmlLocalizationProvider, EPiServer.Framework" />        
   6:         </providers>
   7:     </localization>
   8: </episerver.framework>

 

It is possible to have several providers added to the list of providers. The localization service will return the first value that matches the passed key. If one provider returnes null, the key is passed on to the next provider.

If all providers return ‘null’, the missing text is returned.

 

To log missing translations, you can simply add a provider to log keys that have no previous provider.

   1: <localization fallbackBehavior="Echo, MissingMessage">
   2:   <providers>
   3:     <add virtualPath="~/Resources/LanguageFiles" name="languageFiles" type="EPiServer.Framework.Localization.XmlResources.FileXmlLocalizationProvider, EPiServer.Framework" />
   4:     <add name="missing_keys" type="Alloy.Business.Localization.MissingKeyLoggerLocalizationProvider, Alloy"/>
   5:   </providers>
   6: </localization>

 

Result:

image

 

The provider

The code shows a simplified version of the attached code file.

The basic outline of the functionality is to create a dictionary with missing keys (language key + culture). Every 10 minutes the missing keys are written to a local file on the server.  To remove duplicates that might be registered due to restarting the site etc., I recommend using Excel or some other tool Winking smile

 
   1: using System;
   2: using System.Collections.Generic;
   3: using System.Globalization;
   4: using System.IO;
   5: using System.Linq;
   6: using EPiServer.DataAbstraction;
   7: using EPiServer.Framework.Localization;
   8: using EPiServer.ServiceLocation;
   9:  
  10:  
  11: namespace Alloy.Business.Localization
  12: {
  13:     public class MissingKeyLoggerLocalizationProvider : LocalizationProvider
  14:     {
  15:         Dictionary<string, DateTime> languageKeys = new Dictionary<string, DateTime>();
  16:         private DateTime _latestLogDateTime = DateTime.MinValue;
  17:  
  18:         public override string GetString(string originalKey, string[] normalizedKey, CultureInfo culture)
  19:         {
  20:             //Concatenate key and culture ot create a unique key for dictionary
  21:             string keyWithLanguage = originalKey + ";" + culture.Name;
  22:             if (!languageKeys.ContainsKey(keyWithLanguage))
  23:             {
  24:                 languageKeys.Add(keyWithLanguage, DateTime.Now);
  25:             }
  26:  
  27:             //Log all currently found keys
  28:             TimeSpan timeSinceLog = (DateTime.Now - _latestLogDateTime);
  29:             if (timeSinceLog > new TimeSpan(0, 0, 10, 0))
  30:             {
  31:                 LogDictionary();
  32:             }
  33:             return null; //return null to pass on to other providers
  34:         }
  35:  
  36:         private void LogDictionary()
  37:         {
  38:             IEnumerable<string> keyInfo = languageKeys.Keys.Select(x => $"Date:{languageKeys[x]};Key:{x}");
  39:  
  40:             string missingKeyLogPath = $@"c:\Temp\MissingKeys_{Name}.log";
  41:  
  42:             File.WriteAllLines(missingKeyLogPath, keyInfo);
  43:             _latestLogDateTime = DateTime.Now;
  44:         }
  45:  
  46:         public override IEnumerable<ResourceItem> GetAllStrings(string originalKey, string[] normalizedKey, CultureInfo culture)
  47:         {
  48:             return new List<ResourceItem>();
  49:         }
  50:  
  51:         public override IEnumerable<CultureInfo> AvailableLanguages
  52:         {
  53:             get { return ServiceLocator.Current.GetInstance<ILanguageBranchRepository>().ListEnabled().Select(x => x.Culture); }
  54:         }
  55:     }
  56: }

 

MissingKeyLoggerLocalizationProvider.cs

May 29, 2016

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |