Extending the Multilingual functionality
In this article you will find:
Goal
This article is written for developers to show how the multilingual functionality can be extended. When indexing, with enabled multilingual, the fields of the document's are concatenated with a language suffix.
There might be some fields, that do no need to have this suffix and this article is going to explain
Prerequisite
Install the Hawksearch Connector: Installing the Connector
Introduction to multilingual functionality: Multilingual: Overview
Set-up the multilingual: Multilingual: Setup
Registered custom search service - Register custom search service
How can the multilingual be extended
Currently, the basis of multilingual functionality is the addition of field extensions. Of course, users may want to have fields that do not have a suffix added.
In the logic of multilingual there are 4 fields for which no suffix is added. These fields are - language, pageurl, permissions, denials.
The addition of suffixes can be controlled by extending the method GetMultilingualDocuments
In the example below is shown how the field pageurl
can be left without a suffix. The same will be with any other field. It should be just added in the check in the AddLanguageSuffix
method (line 65).
In order for the indexing to work properly for multilingual and Filter by Permissions , it is necessary for these 4 fields (permissions, denials, language, pageurl) to remain without suffixes as in the sample below.
using System;
using System.Collections.Generic;
using System.Linq;
using Hawksearch.SDK.Indexing;
using Hawksearch.Search;
using Telerik.Sitefinity.Services;
namespace SitefinityWebApp.Custom
{
public class CustomSearchService : HawksearchService
{
protected override List<SubmitDocument> GetMultilingualDocuments(IEnumerable<SubmitDocument> documents)
{
var documentList = new List<SubmitDocument>();
var languages = SystemManager.CurrentContext.AppSettings.DefinedFrontendLanguages.Select(language => language.Name).ToList();
foreach (var document in documents)
{
var language = this.GetDocumentLanguage(document, languages);
if (!languages.Contains(language))
{
continue;
}
this.AddLanguageSuffix(document, language);
this.SetLanguageField(document, language);
documentList.Add(document);
}
return documentList;
}
private void SetLanguageField(SubmitDocument document, string language)
{
var languageField = document.Fields.ToList().FirstOrDefault(f => f.Name == "Language");
if (languageField != null)
{
if (string.IsNullOrWhiteSpace(languageField.Values.FirstOrDefault()))
{
languageField.Values[0] = language;
}
}
}
private void AddLanguageSuffix(SubmitDocument document, string language)
{
foreach (var documentField in document.Fields)
{
var languageSuffix = "_" + language;
if (documentField.Name == "Id")
{
var id = documentField.Values.FirstOrDefault();
if (!string.IsNullOrWhiteSpace(id))
{
id += languageSuffix;
documentField.Values.Clear();
documentField.Values.Add(id);
}
}
else if (string.Equals(documentField.Name, "language", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(documentField.Name, "pageurl", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(documentField.Name, "permissions", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(documentField.Name, "provider", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(documentField.Name, "denials", StringComparison.InvariantCultureIgnoreCase))
{
}
else
{
documentField.Name += languageSuffix;
}
}
}
private string GetDocumentLanguage(SubmitDocument document, List<string> languages)
{
var language = string.Empty;
var languageField = document.Fields.ToList().FirstOrDefault(f => f.Name == "Language");
if (languageField != null)
{
language = languageField.Values.FirstOrDefault();
if (languages.Count == 1 && string.IsNullOrWhiteSpace(language))
{
language = languages.FirstOrDefault();
}
}
return language;
}
}
}
Once you implement the code in Visual Studio , build your solution and you will also have to reindex the index you are using from Administrator → Search Indexes → Action → Reindex