Jelajahi Sumber

Add options to add prefix to strings

goanpeca 4 tahun lalu
induk
melakukan
ad73353614

+ 12 - 0
packages/translation-extension/schema/plugin.json

@@ -10,6 +10,18 @@
       "title": "Language locale",
       "description": "Set the interface display language. Examples: 'es_CO', 'fr'.",
       "default": "en"
+    },
+    "stringsPrefix": {
+      "type": "string",
+      "title": "Localized strings prefix",
+      "description": "Add a prefix to localized strings.",
+      "default": "!!"
+    },
+    "displayStringsPrefix": {
+      "type": "boolean",
+      "title": "Display localized strings prefix",
+      "description": "Display the `stringsPrefix` on localized strings.",
+      "default": false
     }
   }
 }

+ 6 - 1
packages/translation-extension/src/index.ts

@@ -54,7 +54,12 @@ const translator: JupyterFrontEndPlugin<ITranslator> = {
   activate: async (app: JupyterFrontEnd, settings: ISettingRegistry) => {
     const setting = await settings.load(PLUGIN_ID);
     const currentLocale: string = setting.get('locale').composite as string;
-    const translationManager = new TranslationManager();
+    let stringsPrefix: string = setting.get('stringsPrefix')
+      .composite as string;
+    const displayStringsPrefix: boolean = setting.get('displayStringsPrefix')
+      .composite as boolean;
+    stringsPrefix = displayStringsPrefix ? stringsPrefix : '';
+    const translationManager = new TranslationManager(stringsPrefix);
     await translationManager.fetch(currentLocale);
     return translationManager;
   }

+ 37 - 5
packages/translation/src/gettext.ts

@@ -91,6 +91,11 @@ interface IOptions {
    */
   pluralForms?: string;
 
+  /**
+   * The string prefix to add to localized strings.
+   */
+  stringsPrefix?: string;
+
   /**
    * Plural form function.
    */
@@ -132,7 +137,8 @@ class Gettext {
       pluralFunc: function (n: number) {
         return { nplurals: 2, plural: n != 1 ? 1 : 0 };
       },
-      contextDelimiter: String.fromCharCode(4) // \u0004
+      contextDelimiter: String.fromCharCode(4), // \u0004
+      stringsPrefix: ''
     };
 
     // Ensure the correct separator is used
@@ -140,6 +146,7 @@ class Gettext {
     this._domain = options.domain || this._defaults.domain;
     this._contextDelimiter =
       options.contextDelimiter || this._defaults.contextDelimiter;
+    this._stringsPrefix = options.stringsPrefix || this._defaults.stringsPrefix;
     this._pluralFuncs = {};
     this._dictionary = {};
     this._pluralForms = {};
@@ -208,6 +215,24 @@ class Gettext {
     return this._domain;
   }
 
+  /**
+   * Set current strings prefix.
+   *
+   * @param prefix - The string prefix to set.
+   */
+  setStringsPrefix(prefix: string): void {
+    this._stringsPrefix = prefix;
+  }
+
+  /**
+   * Get current strings prefix.
+   *
+   * @return The strings prefix.
+   */
+  getStringsPrefix(): string {
+    return this._stringsPrefix;
+  }
+
   /**
    * `sprintf` equivalent, takes a string and some arguments to make a
    * computed string.
@@ -563,7 +588,10 @@ class Gettext {
   ): string {
     // Singular is very easy, just pass dictionary message through strfmt
     if (!options.pluralForm)
-      return Gettext.strfmt(this.removeContext(messages[0]), ...args);
+      return (
+        this._stringsPrefix +
+        Gettext.strfmt(this.removeContext(messages[0]), ...args)
+      );
 
     let plural;
 
@@ -591,9 +619,12 @@ class Gettext {
     )
       plural.plural = 0;
 
-    return Gettext.strfmt(
-      this.removeContext(messages[plural.plural]),
-      ...[n].concat(args)
+    return (
+      this._stringsPrefix +
+      Gettext.strfmt(
+        this.removeContext(messages[plural.plural]),
+        ...[n].concat(args)
+      )
     );
   }
 
@@ -621,6 +652,7 @@ class Gettext {
     this._dictionary[domain][locale] = messages;
   }
 
+  private _stringsPrefix: string;
   private _pluralForms: any;
   private _dictionary: any;
   private _locale: string;

+ 7 - 3
packages/translation/src/manager.ts

@@ -8,8 +8,10 @@ import { ITranslator, TranslationBundle, TranslatorConnector } from './tokens';
  * Translation Manager
  */
 export class TranslationManager implements ITranslator {
-  constructor() {
+  constructor(stringsPrefix?: string) {
     this._connector = new TranslatorConnector();
+    this._stringsPrefix = stringsPrefix || '';
+    this._englishBundle = new Gettext({ stringsPrefix: this._stringsPrefix });
   }
 
   /**
@@ -40,7 +42,8 @@ export class TranslationManager implements ITranslator {
         if (!(domain in this._translationBundles)) {
           let translationBundle = new Gettext({
             domain: domain,
-            locale: this._currentLocale
+            locale: this._currentLocale,
+            stringsPrefix: this._stringsPrefix
           });
           if (domain in this._domainData) {
             let metadata = this._domainData[domain][''];
@@ -63,7 +66,8 @@ export class TranslationManager implements ITranslator {
   private _connector: TranslatorConnector;
   private _currentLocale: string;
   private _domainData: any = {};
-  private _englishBundle = new Gettext();
+  private _englishBundle: Gettext;
   private _languageData: any;
+  private _stringsPrefix: string;
   private _translationBundles: any = {};
 }