Gravatar for anweshadastider@gmail.com

Question by anwesha, Aug 6, 2015 7:18 AM

Add translation phrase in the default culture

I have a facet with the field set as template. The facet lists all the referred templates that are coming up in the search results page. I need to translate the template names based on the target sites language exp: "de". How can this be done? Can I extend the de.js culture file somehow?

I have added the below script at the end of my "CoveoSearch.ascx" file but it dint work for me.

2 Replies
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Aug 6, 2015 2:21 PM

The answer depends on how your search page is implemented. Is it possible to see results of multiple languages in the same search session (either with a facet to toggle the language or by avoiding to filter on the current site language)?

If you can see results from the same template but with different languages in the same result set, you need to decide whether you want to see the all the localized template names as separate facet values or to have them merged in a single facet value of a single language.

If you cannot see results of a language different than the site language once the page is loaded or if you want only one facet item per template but always localized in the current site language, you can implement the translation directly in the UI with the facet's valueCaption option. You'll need to dynamically render the JSON object to map the original strings to the localized ones on the server based on the current site language while rendering the page. This render-time operation is not optimal as it takes more time to render the page but has the advantage of having only one facet value per template. If you go with this solution, note that you will have to duplicate the facet sublayout/rendering and its ascx/cshtml file to add an id attribute to the facet <div> element and set its value to Model.UniqueId because the valueCaption option cannot be set as a data- attribute and must be set by JavaScript in the initialization options.

If you can see results from different languages in a single session and decide to have a facet value per language, you can store the localized template name directly in the CES indexed documents with the help of a computed field. Your computed field would read the template name of the item to be indexed and return the localized template name. You'll need to reindex all your Sitecore indexes to. You'll also need to define a new "Localized Template Name" facet field in "/sitecore/System/Settings/Buckets/Facets" and use this facet item as the field of your UI facet.

In all the cases, you can store the translated strings in a Sitecore dictionary and use its API to get the translations.

Gravatar for anweshadastider@gmail.com

Comment by anwesha, Aug 7, 2015 1:24 AM

Thanks for the reply!

"If you cannot see results of a language different than the site language once the page is loaded or if you want only one facet item per template but always localized in the current site language, you can implement the translation directly in the UI with the facet's valueCaption option." I have used this approach to translate the template names in the facet to use my captions. I have placed the script in the "CoveoSearch.ascx" file and it works.

But I want the facet template names to be localized i.e. the same facet template to have the language specific names when we load the language specific search pages. Right now the same translation is appearing for all the languages. Is there a way to do that?

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Aug 7, 2015 10:10 AM

Currently, I think you made your script by hand and it is hardcoded for one target language. You should create an ASP.Net server-side control that would generate the JavaScript initialization options you want in the site's current language based on Sitecore.Context.Culture or Sitecore.Context.Language. Then, the code will generate the translations for the page language on page load and output the valueCaption option value in the page. Then, the JS UI will initialize with the right translations.

Please note that modifying the CoveoSearch.ascx file directly exposes you to future upgrade problems because this file is overwritten when installing a new version of Coveo for Sitecore. It is recommended to do a copy of the files and associated sublayouts you want to modify and use your copies instead of the original ones (see Duplicating the Coveo Search Component for an example).

Gravatar for anweshadastider@gmail.com

Comment by anwesha, Aug 9, 2015 11:49 PM

Thanks jflheureux!! It really helped..

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Aug 10, 2015 8:45 AM

You're welcome. Please mark this answer as the accepted one if it answers your question.

Thanks

Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Aug 6, 2015 9:00 AM

For the dictionary to work, I believe you need to run this script before the call to .coveoForSitecore('init', ...);.

Also, for the template name facet, maybe creating the template items in all the desired languages would work. When indexing the item, I believe Coveo loads the item's template item in the item language and fallback to the default language if the specific language is not available in Sitecore.

Gravatar for anweshadastider@gmail.com

Comment by anwesha, Aug 6, 2015 10:49 AM

Is there a way to achieve this without creating versions of each template in all the required languages?

Ask a question