Home > widget-integration > FilterTranslationService > translateByKey

FilterTranslationService.translateByKey() method

Retrieves a translated string by its key from the app's translation configuration.

Supports nested keys using dot notation (e.g., 'filter.title.color'). Returns the default value if the key is not found in translations or is invalid.

Signature:

translateByKey(key?: string, defaultValue?: string): string;

Parameters

Parameter

Type

Description

key

string

(Optional) The translation key, supports dot notation for nested objects. Empty string returns the default value.

defaultValue

string

(Optional) Fallback value returned if translation key doesn't exist. Defaults to empty string.

Returns:

string

The translated string if found, otherwise the default value

Example 1

Basic usage:

const title = this.translateByKey('filter.title', 'Filters');
const nested = this.translateByKey('filter.options.color.label', 'Color');

Example 2

Extend to add translation caching:

window.boostWidgetIntegration.extend('FilterTranslationService', (FilterTranslationService) => {
  return class CustomFilterTranslationService extends FilterTranslationService {
    constructor(appService, filterStore, filterHelper) {
      super(appService, filterStore, filterHelper);
      this.translationCache = new Map();
    }

    translateByKey(key, defaultValue) {
      const cacheKey = `trans_${key}`;
      if (this.translationCache.has(cacheKey)) {
        return this.translationCache.get(cacheKey);
      }
      const result = super.translateByKey(key, defaultValue);
      this.translationCache.set(cacheKey, result);
      return result;
    }
  };
});