@sassoftware/vi-api
Version:
Types used in the SAS Visual Investigator API
131 lines (130 loc) • 4.79 kB
TypeScript
/**
* This API is used for the localization methods.
*
* Accessed from the window at `window.sas.vi.localization`.
*
* @example window.sas.vi.localization.getLocale();
* @category API
*/
export interface LocalizationApi {
/**
* @method
* @description Returns the MomentJS format string based on locale.
* @param [showTime=true] {boolean} Checks whether or not to include time (in 12 hour format).
* @returns The localized date time format as a string.
*/
getLocalizedDateTimeFormat(showTime?: boolean): string;
/**
* @method
* @description Gets the application locale in BCP 47 format.
* @returns The current application locale.
*/
getLocale(): string;
/**
* @method
* @description Gets user's preferred locale format in BCP 47 format. This should be used
* to format dates\numbers in VI extensions but not to drive the language.
* @returns The user's preferred locale format.
*/
getFormatLocale(): string;
/**
* @method
* @description Gets the application display direction.
* This is either "ltr" for left-to-right display locales, or "rtl" for right-to-left locales.
* @returns The current application display direction.
*/
getDirection(): "ltr" | "rtl";
/**
* Methods related to solution content localization.
*/
solution: SolutionLocalizationApi;
}
/**
* This API is used for solution content localization.
*
* Accessed from the window at `window.sas.vi.localization.solution`.
*
* @example window.sas.vi.metadata.getLocalizationBundle();
* @category API
*/
export interface SolutionLocalizationApi {
/**
* @method
* @returns The {@link SolutionLocalizationBundle} used to lookup translations if solution localization is enabled, else undefined.
*/
getLocalizationBundle(): SolutionLocalizationBundle | undefined;
/**
* @method
* @description
* Check that solution localization is enabled.
* Localization methods will return unmodified inputs if isEnabled returns false.
* Solution localization is not available in the administration application.
* @returns True if solution localization is enabled, else false.
*/
isEnabled(): boolean;
/**
* @method
* @returns The localized value for the given resource key, else undefined.
* @param resourceKey {string} A resource key used to lookup the localized value.
* @example
* getLocalizedResource("colors.red.txt") === "rouge"
*/
getLocalizedResource(resourceKey: string): string | undefined;
/**
* @method
* @description
* Localize a string using a resourceKey.
* Fallback to lookup by value if the resourceKey is not found or not supplied.
* @param str {string} The string to localize.
* @param resourceKey {string} A resource key used to lookup the localized value.
* @returns The localized string.
* @example
* localize("red", "colors.red.txt") === "rouge"
*/
localize(str: string, resourceKey?: string): string;
/**
* @method
* @description
* Localize string properties of an object.
* Properties are provided as a map of property keys to resource keys, deep property keys are accepted.
* @param obj {T} The source object to localize.
* @param props {Record<string, string | undefined>} The properties to localize and their resource keys.
* @returns The localized object.
* @example
* localizeObject({ label: "red" }, { "label": "colors.red.txt" }) === { label: "rouge" }
*/
localizeObject<T extends object>(obj: T, props: Record<string, string | undefined>): T;
/**
* @method
* @description
* Localize a list of objects.
* @param list {T[]} The objects to translate.
* @param getProps {function(T): Record<string, string | undefined>} A callback to return a map of properties and resource keys for the given list element.
* @returns The localized list.
* @example
* localizeList(
* [{ id: "red", label: "Red" }, { id: "blue", label: "Blue"} ],
* (item) => ({ "label": `colors.${item.id}.title` })
* ) === [{ id: "red", label: "Rouge" }, { id: "blue", label: "Bleu"} ]
*/
localizeList<T extends object>(list: T[], getProps: (obj: T) => Record<string, string | undefined>): T[];
}
/**
* Stores localized strings by key and value.
* @example
* {
* byKey: {
* "colors.red.label": "rouge",
* "colors.blue.label": "bleu"
* },
* byValue: {
* "red": "rouge",
* "blue": "bleu"
* }
* }
*/
interface SolutionLocalizationBundle {
byKey: Record<string, string>;
byValue: Record<string, string>;
}
export {};