generaltranslation
Version:
A language toolkit for AI developers
267 lines (243 loc) • 10.3 kB
TypeScript
declare const defaultCacheUrl: "https://cdn.gtx.dev";
declare const defaultBaseUrl: "https://api2.gtx.dev";
declare const defaultRuntimeApiUrl: "https://runtime2.gtx.dev";
declare const libraryDefaultLocale: "en";
declare const defaultTimeout = 60000;
declare const pluralForms: readonly ["singular", "plural", "dual", "zero", "one", "two", "few", "many", "other"];
type PluralType = (typeof pluralForms)[number];
declare function isAcceptedPluralForm(form: string): form is PluralType;
/**
* Given a number and a list of allowed plural forms, return the plural form that best fits the number.
*
* @param {number} n - The number to determine the plural form for.
* @param {locales[]} forms - The allowed plural forms.
* @returns {PluralType} The determined plural form, or an empty string if none fit.
*/
declare function _getPluralForm(n: number, forms?: PluralType[], locales?: string[]): PluralType | '';
type LocaleProperties = {
code: string;
name: string;
nativeName: string;
languageCode: string;
languageName: string;
nativeLanguageName: string;
nameWithRegionCode: string;
nativeNameWithRegionCode: string;
regionCode: string;
regionName: string;
nativeRegionName: string;
scriptCode: string;
scriptName: string;
nativeScriptName: string;
maximizedCode: string;
maximizedName: string;
nativeMaximizedName: string;
minimizedCode: string;
minimizedName: string;
nativeMinimizedName: string;
emoji: string;
};
type VariableType = 'v' | 'n' | 'd' | 'c';
/**
* Variables are used to store the variable name and type.
*/
type Variable = {
k: string;
i?: number;
v?: VariableType;
};
/**
* Map of data-_gt properties to their corresponding React props
*/
declare const HTML_CONTENT_PROPS: {
readonly pl: "placeholder";
readonly ti: "title";
readonly alt: "alt";
readonly arl: "aria-label";
readonly arb: "aria-labelledby";
readonly ard: "aria-describedby";
};
type HtmlContentPropKeysRecord = Partial<Record<keyof typeof HTML_CONTENT_PROPS, string>>;
/**
* GTProp is an internal property used to contain data for translating and rendering elements.
* note, transformations are only read on the server side if they are 'plural' or 'branch'
*/
type GTProp = {
b?: Record<string, JsxChildren>;
t?: 'p' | 'b';
} & HtmlContentPropKeysRecord;
type JsxElement = {
t?: string;
i?: number;
d?: GTProp;
c?: JsxChildren;
};
type JsxChild = string | JsxElement | Variable;
/**
* A content type representing JSX elements
*/
type JsxChildren = JsxChild | JsxChild[];
type VariableTransformationSuffix = 'variable' | 'number' | 'datetime' | 'currency';
/**
* @deprecated Use {@link Content} instead.
*/
type _Content = string | Array<string | Variable>;
declare function isVariable(obj: unknown): obj is Variable;
declare function minifyVariableType(variableType: VariableTransformationSuffix): VariableType;
declare function encode(data: string): string;
declare function decode(base64: string): string;
/**
* Given an encoded ICU string, interpolate only _gt_ variables that have been marked with declareVar()
* @example
* const encodedIcu = "Hi" + declareVar("Brian") + ", my name is {name}"
* // 'Hi {_gt_, select, other {Brian}}, my name is {name}'
* decodeVars(encodedIcu)
* // 'Hi Brian, my name is {name}'
*/
declare function decodeVars(icuString: string): string;
/**
* Mark as a non-translatable string. Use within a declareStatic() call to mark content as not statically analyzable (e.g., not possible to know before runtime).
*
* @example
* function staticFunction() {
* if (condition) {
* return declareVar(Math.random())
* }
* return 'John Doe';
* }
*
* const gt = useGT();
* gt(`My name is ${declareStatic(staticFunction())}`);
*
* @param {string | number | boolean | null | undefined} variable - The variable to sanitize.
* @param {Object} [options] - The options for the sanitization.
* @param {string} [options.$name] - The name of the variable.
* @returns {string} The sanitized value.
*/
declare function declareVar(variable: string | number | boolean | null | undefined, options?: {
$name?: string;
}): string;
/**
* declareStatic() is a powerful but dangerous function which marks its argument as statically analyzable for the compiler and CLI tool.
*
* This function is dangerous because it can cause the compiler and CLI tool to throw an error if the argument is not statically analyzable.
*
* @example
* ```jsx
* function getSubject() {
* return (Math.random() > 0.5) ? "Alice" : "Brian";
* }
* ...
* gt(`My name is ${declareStatic(getSubject())}`);
* ```
*
* @param {T extends string | boolean | number | null | undefined} content - Content to mark as statically analyzable.
* @returns content
*/
declare function declareStatic<T extends string | boolean | number | null | undefined>(content: T): T;
/**
* Given an ICU string adds identifiers to each _gt_ placeholder
* indexVars('Hello {_gt_} {_gt_} World') => 'Hello {_gt_1_} {_gt_2_} World'
*/
declare function indexVars(icuString: string): string;
/**
* Given an unindexed ICU string, extracts all the _gt_ variables and an indexed mapping of the variable to the values
*
* extractVars('Hello {_gt_, select, other {World}}') => { _gt_1: 'World' }
*
* @param {string} icuString - The ICU string to extract variables from.
* @returns {Record<string, string>} A mapping of the variable to the value.
*/
declare function extractVars(icuString: string): Record<string, string>;
/**
* Given an indexed ICU string, condenses any select to an argument
* indexVars('Hello {_gt_1, select, other {World}}') => 'Hello {_gt_1}'
* @param {string} icuString - The ICU string to condense.
* @returns {string} The condensed ICU string.
*/
declare function condenseVars(icuString: string): string;
declare const VAR_IDENTIFIER = "_gt_";
declare const VAR_NAME_IDENTIFIER = "_gt_var_name";
type OldBranchType = 'branch' | 'plural';
type OldGTProp = {
id: number;
transformation?: OldBranchType;
branches?: Record<string, OldJsxChildren>;
};
type OldJsxElement = {
type: string;
props: {
children?: OldJsxChildren;
'data-_gt': OldGTProp;
};
};
type OldVariableType = 'number' | 'variable' | 'datetime' | 'currency';
type OldVariableObject = {
variable?: OldVariableType;
key: string;
id?: number;
};
type OldJsxChild = OldJsxElement | OldVariableObject | string;
type OldJsxChildren = OldJsxChild | OldJsxChild[];
/**
* Convert request data from old format to new format
*/
declare function getNewJsxChild(child: OldJsxChild): JsxChild;
declare function getNewJsxChildren(children: OldJsxChildren): JsxChildren;
declare function getNewJsxElement(element: OldJsxElement): JsxElement;
declare function getNewBranchType(branch: OldBranchType): 'b' | 'p';
declare function getNewVariableType(variable: OldVariableType): VariableType;
declare function getNewVariableObject(variable: OldVariableObject): Variable;
declare function getNewGTProp(dataGT: OldGTProp): GTProp;
/**
* Convert response data from old format to new format
*/
declare function getOldJsxChild(child: JsxChild): OldJsxChild;
declare function getOldJsxChildren(children: JsxChildren | OldJsxChildren): OldJsxChildren;
declare function getOldJsxElement(element: JsxElement): OldJsxElement;
declare function getOldBranchType(branch: 'b' | 'p'): OldBranchType;
declare function getOldVariableType(variable: VariableType): OldVariableType;
declare function getOldVariableObject(variable: Variable): OldVariableObject;
declare function getOldGTProp(dataGT: GTProp, i: number): OldGTProp;
/**
* Checks if a JSX child is an old variable object format
* @param child - The JSX child to check
* @returns True if the child is an old variable object (has 'key' property)
*/
declare function isOldVariableObject(child: OldJsxChild | JsxChild): child is OldVariableObject;
/**
* Checks if a JSX child is a new variable object format
* @param child - The JSX child to check
* @returns True if the child is a new variable object (has 'k' property)
*/
declare function isNewVariableObject(child: OldJsxChild | JsxChild): child is Variable;
/**
* Checks if JSX children follow the old format
* @param children - The JSX children to check (can be string, array, or single child)
* @returns True if all children are in the old format
*/
declare function isOldJsxChildren(children: OldJsxChildren | JsxChildren): children is OldJsxChildren;
/**
* Calculates a unique hash for a given string using sha256.
*
* @param {string} string - The string to be hashed.
* @returns {string} - The resulting hash as a hexadecimal string.
*/
declare function oldHashString(string: string): string;
/**
* Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.
*
* @param {any} childrenAsObjects - The children objects to be hashed.
* @param {string} context - The context for the children
* @param {string} id - The id for the JSX Children object
* @param {function} hashFunction custom hash function
* @returns {string} - The unique has of the children.
*/
declare function oldHashJsxChildren({ source, context, id, dataFormat, }: {
source: OldJsxChildren;
context?: string;
id?: string;
dataFormat: string;
}, hashFunction?: (string: string) => string): string;
export { VAR_IDENTIFIER, VAR_NAME_IDENTIFIER, condenseVars, declareStatic, declareVar, decode, decodeVars, defaultBaseUrl, defaultCacheUrl, defaultRuntimeApiUrl, defaultTimeout, encode, extractVars, getNewBranchType, getNewGTProp, getNewJsxChild, getNewJsxChildren, getNewJsxElement, getNewVariableObject, getNewVariableType, getOldBranchType, getOldGTProp, getOldJsxChild, getOldJsxChildren, getOldJsxElement, getOldVariableObject, getOldVariableType, _getPluralForm as getPluralForm, indexVars, isAcceptedPluralForm, isNewVariableObject, isOldJsxChildren, isOldVariableObject, isVariable, libraryDefaultLocale, minifyVariableType, oldHashJsxChildren, oldHashString, pluralForms };
export type { JsxChild, JsxChildren, JsxElement, LocaleProperties, OldBranchType, OldGTProp, OldJsxChild, OldJsxChildren, OldJsxElement, OldVariableObject, OldVariableType, _Content };