Skip to content

Support for separating survey JSON definition from localized strings and loading translations at runtime #10735

@JaneSjs

Description

@JaneSjs

Feature Proposal

We propose adding functionality to separate the survey layout JSON from localized strings, allowing them to be stored in separate files and fetched/loaded at runtime. This would improve modularity, especially for applications handling multiple languages dynamically.

Proposed Changes

  1. Extend ISaveToJSONOptions Interface
    Add the following options to control how localizable strings are handled during serialization:

    storeLocaleStrings?: boolean | "stringsOnly";
    locales?: Array<string>;

    Behavior based on storeLocalizableStrings:

    • false: Serialize without any localizable strings, including the default locale.
    • true: The default behavior, you may not set it. Serialize only the specified locales from locales (e.g., ["default", "fr", "it"]) or if it is empty then all strings for all locales (default serializer behavior).
    • "stringsOnly": Serialize only object types, unique properties, and localized strings (excludes other properties). Useful for isolating strings in a separate JSON file.
    • "stringsOnly" with locales: Limits the strings to the specified locales (e.g., only Italian with ["it"]).

    Examples usage:

  //exclude all locales except default, "fr" and "it"
   const json = survey.toJSON({ locales: ["default", "fr", "it"] }); 
   //serialize strings only and ingore all locales except "fr" and "it"
   const json = survey.toJSON({ storeLocalizableStrings: "stringsOnly", locales: ["fr", "it"] }); 
   //do not serialize any strings, including the text for default locale
   const json = survey.toJSON({ storeLocalizableStrings: false ); 
  1. New Method: survey.mergeLocalizationJSON(json: any, locales?: Array<string>): void
    This method merges localized strings from a provided JSON into the existing survey. It enables loading translations from separate files on demand, supporting dynamic locale switching.

    Example:

    // Assume 'translations.json' contains strings for 'fr' and 'it'
    fetch('translations.json')
      .then(response => response.json())
      .then(translations => {
        survey.mergeLocalizationJSON(translations, ["fr"]); // Load only French strings
      });
  2. New Method: survey.getLocalizationJSON(locales?: Array<string>): any
    This function calls toJSON function with storeLocalizableStrings: "stringsOnly" parameter. It makes API simpler.

Benefits

  • Reduces main survey JSON size by offloading translations.
  • Facilitates easier management of multilingual surveys.
  • Enables runtime loading for better performance in web apps.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions