Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
messageProvider.ts
Go to the documentation of this file.
1
85import * as vscode from 'vscode';
86import { messages } from './messageReference';
87
106 private locale: string = vscode.env.language.toLowerCase();
107
109 private messageNotFound: string = "messageNotFound";
110
112 private messages: Record<string, Record<string, (...args: any[]) => string>> = messages;
113
123 private getFirstAvailableLanguageNode(): Record<string, (...args: any[]) => string> {
124 const langs = Object.keys(this.messages);
125 if (langs.length === 0) {
126 throw new Error("No messages available.");
127 }
128 return this.messages[langs[0]];
129 }
130
144 private returnCorrectMessage(
145 node: Record<string, (...args: any[]) => string>,
146 messageKey: string,
147 ...args: any[]
148 ): string {
149 if (node[messageKey] === undefined) {
150 return node[this.messageNotFound](messageKey, ...args);
151 }
152 return node[messageKey](...args);
153 }
154
161 public get(messageKey: string, ...args: any[]): string;
162
170 public get(messageKey: string, options: { language: string }, ...args: any[]): string;
171
187 public get(messageKey: string, optionsOrArg?: { language: string } | any, ...args: any[]): string {
188 let lang: string;
189 let finalArgs: any[];
190
191 if (optionsOrArg && typeof optionsOrArg === 'object' && 'language' in optionsOrArg) {
192 lang = optionsOrArg.language;
193 finalArgs = args;
194 } else {
195 lang = this.locale;
196 finalArgs = args;
197 if (optionsOrArg !== undefined) {
198 finalArgs = [optionsOrArg, ...args];
199 }
200 }
201
202 const node = this.messages[lang] || this.messages['en'] || this.getFirstAvailableLanguageNode();
203 return this.returnCorrectMessage(node, messageKey, ...finalArgs);
204 }
205}
206
215
232export const getMessage = instance.get.bind(instance);
Internationalization message provider with multi-language support and fallback mechanisms.
import *as vscode from vscode
export const getMessage
Exported function for direct message retrieval.
const instance
Singleton instance of the MessageProvider for application-wide use.
export const messages
Complete message dictionary for all supported languages with type-safe function interfaces @export Ex...
export const Record< string,(...args:any[])=> string