add: getString with args
This commit is contained in:
		
							parent
							
								
									a254ff8bc3
								
							
						
					
					
						commit
						4d3bc65b94
					
				@ -14,23 +14,35 @@ export function initLocale() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Get locale string
 | 
			
		||||
 * @param localString
 | 
			
		||||
 * @param branch branch name
 | 
			
		||||
 * Get locale string, see https://firefox-source-docs.mozilla.org/l10n/fluent/tutorial.html#fluent-translation-list-ftl
 | 
			
		||||
 * @param localString ftl key
 | 
			
		||||
 * @param options.branch branch name
 | 
			
		||||
 * @param options.args args
 | 
			
		||||
 * @example
 | 
			
		||||
 * ```ftl
 | 
			
		||||
 * # addon.ftl
 | 
			
		||||
 * addon-name = Addon Template
 | 
			
		||||
 *     .label = Addon Template Label
 | 
			
		||||
 * addon-static-example = This is default branch!
 | 
			
		||||
 *     .branch-example = This is a branch under addon-static-example!
 | 
			
		||||
 * addon-dynamic-example =
 | 
			
		||||
    { $count ->
 | 
			
		||||
        [one] I have { $count } apple
 | 
			
		||||
       *[other] I have { $count } apples
 | 
			
		||||
    }
 | 
			
		||||
 * ```
 | 
			
		||||
 * ```js
 | 
			
		||||
 * getString("addon-name"); // Addon Template
 | 
			
		||||
 * getString("addon-name", "label"); // Addon Template Label
 | 
			
		||||
 * getString("addon-static-example"); // This is default branch!
 | 
			
		||||
 * getString("addon-static-example", { branch: "branch-example" }); // This is a branch under addon-static-example!
 | 
			
		||||
 * getString("addon-dynamic-example", { args: { count: 1 } }); // I have 1 apple
 | 
			
		||||
 * getString("addon-dynamic-example", { args: { count: 2 } }); // I have 2 apples
 | 
			
		||||
 * ```
 | 
			
		||||
 */
 | 
			
		||||
export function getString(localString: string, branch = ""): string {
 | 
			
		||||
export function getString(
 | 
			
		||||
  localString: string,
 | 
			
		||||
  options: { branch?: string | undefined; args?: Record<string, unknown> } = {}
 | 
			
		||||
): string {
 | 
			
		||||
  const { branch, args } = options;
 | 
			
		||||
  const pattern = addon.data.locale?.current.formatMessagesSync([
 | 
			
		||||
    { id: localString },
 | 
			
		||||
    { id: localString, args },
 | 
			
		||||
  ])[0];
 | 
			
		||||
  if (!pattern) {
 | 
			
		||||
    return localString;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user