@tolgee/core 5.0.0-rc.af99eec.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/README.md +174 -0
  2. package/README.njk.md +61 -0
  3. package/dist/tolgee.cjs.js +733 -357
  4. package/dist/tolgee.cjs.js.map +1 -1
  5. package/dist/tolgee.cjs.min.js +1 -1
  6. package/dist/tolgee.cjs.min.js.map +1 -1
  7. package/dist/{tolgee.esm.mjs → tolgee.esm.js} +732 -352
  8. package/dist/tolgee.esm.js.map +1 -0
  9. package/dist/tolgee.esm.min.mjs +1 -1
  10. package/dist/tolgee.esm.min.mjs.map +1 -1
  11. package/dist/tolgee.umd.js +733 -357
  12. package/dist/tolgee.umd.js.map +1 -1
  13. package/dist/tolgee.umd.min.js +1 -1
  14. package/dist/tolgee.umd.min.js.map +1 -1
  15. package/lib/Controller/Cache/Cache.d.ts +10 -9
  16. package/lib/Controller/Controller.d.ts +104 -45
  17. package/lib/Controller/Events/EventEmitter.d.ts +6 -0
  18. package/lib/Controller/Events/EventEmitterSelective.d.ts +7 -0
  19. package/lib/Controller/Events/Events.d.ts +14 -0
  20. package/lib/Controller/Plugins/Plugins.d.ts +12 -25
  21. package/lib/Controller/State/State.d.ts +27 -9
  22. package/lib/Controller/State/initState.d.ts +46 -15
  23. package/lib/Controller/State/observerOptions.d.ts +41 -0
  24. package/lib/Controller/ValueObserver.d.ts +5 -5
  25. package/lib/FormatSimple/FormatError.d.ts +7 -0
  26. package/lib/FormatSimple/FormatSimple.d.ts +2 -0
  27. package/lib/FormatSimple/formatParser.d.ts +1 -0
  28. package/lib/FormatSimple/formatter.d.ts +2 -0
  29. package/lib/TolgeeCore.d.ts +204 -0
  30. package/lib/TranslateParams.d.ts +1 -1
  31. package/lib/helpers.d.ts +8 -0
  32. package/lib/index.d.ts +4 -4
  33. package/lib/types/cache.d.ts +25 -0
  34. package/lib/types/events.d.ts +66 -0
  35. package/lib/types/general.d.ts +34 -0
  36. package/lib/types/index.d.ts +7 -0
  37. package/lib/types/plugin.d.ts +127 -0
  38. package/package.json +5 -4
  39. package/src/Controller/Cache/Cache.ts +39 -34
  40. package/src/Controller/Cache/helpers.ts +6 -6
  41. package/src/Controller/Controller.ts +81 -53
  42. package/src/Controller/Events/EventEmitter.ts +34 -0
  43. package/src/Controller/Events/EventEmitterSelective.test.ts +110 -0
  44. package/src/Controller/Events/EventEmitterSelective.ts +132 -0
  45. package/src/Controller/Events/Events.ts +69 -0
  46. package/src/Controller/Plugins/Plugins.ts +182 -133
  47. package/src/Controller/State/State.ts +43 -26
  48. package/src/Controller/State/initState.ts +97 -25
  49. package/src/Controller/State/observerOptions.ts +66 -0
  50. package/src/Controller/ValueObserver.ts +5 -2
  51. package/src/FormatSimple/FormatError.ts +26 -0
  52. package/src/FormatSimple/FormatSimple.ts +13 -0
  53. package/src/FormatSimple/formatParser.ts +133 -0
  54. package/src/FormatSimple/formatter.test.ts +190 -0
  55. package/src/FormatSimple/formatter.ts +19 -0
  56. package/src/TolgeeCore.ts +267 -0
  57. package/src/TranslateParams.test.ts +9 -12
  58. package/src/TranslateParams.ts +6 -5
  59. package/src/__test/backend.test.ts +6 -6
  60. package/src/__test/cache.test.ts +190 -0
  61. package/src/__test/client.test.ts +2 -2
  62. package/src/__test/events.test.ts +32 -7
  63. package/src/__test/format.simple.test.ts +14 -0
  64. package/src/__test/formatError.test.ts +61 -0
  65. package/src/__test/initialization.test.ts +15 -3
  66. package/src/__test/languageDetection.test.ts +14 -8
  67. package/src/__test/languageStorage.test.ts +10 -11
  68. package/src/__test/languages.test.ts +30 -6
  69. package/src/__test/loading.test.ts +2 -2
  70. package/src/__test/{namespacesFallback.test.ts → namespaces.fallback.test.ts} +10 -8
  71. package/src/__test/namespaces.test.ts +30 -7
  72. package/src/__test/options.test.ts +64 -0
  73. package/src/__test/plugins.test.ts +29 -18
  74. package/src/helpers.ts +53 -0
  75. package/src/index.ts +4 -10
  76. package/src/types/cache.ts +37 -0
  77. package/src/types/events.ts +85 -0
  78. package/src/types/general.ts +50 -0
  79. package/src/types/index.ts +19 -0
  80. package/src/types/plugin.ts +181 -0
  81. package/dist/tolgee.esm.mjs.map +0 -1
  82. package/lib/Controller/State/helpers.d.ts +0 -6
  83. package/lib/Events/EventEmitter.d.ts +0 -6
  84. package/lib/Events/EventEmitterSelective.d.ts +0 -15
  85. package/lib/Events/Events.d.ts +0 -50
  86. package/lib/Tolgee.d.ts +0 -2
  87. package/lib/constants.d.ts +0 -5
  88. package/lib/types.d.ts +0 -274
  89. package/src/Controller/State/helpers.ts +0 -41
  90. package/src/Events/EventEmitter.ts +0 -27
  91. package/src/Events/EventEmitterSelective.test.ts +0 -108
  92. package/src/Events/EventEmitterSelective.ts +0 -160
  93. package/src/Events/Events.ts +0 -66
  94. package/src/Tolgee.ts +0 -77
  95. package/src/constants.ts +0 -7
  96. package/src/types.ts +0 -380
@@ -1,4 +1,4 @@
1
- import { Tolgee } from '../index';
1
+ import { TolgeeCore } from '../index';
2
2
 
3
3
  const tolgeeWithNamespaces = () => {
4
4
  const promiseEnTest = jest.fn(() => Promise.resolve({ test: 'Test' }));
@@ -8,9 +8,10 @@ const tolgeeWithNamespaces = () => {
8
8
  Promise.resolve({ cancel: 'Cancellar' })
9
9
  );
10
10
 
11
- return Tolgee({
11
+ return TolgeeCore().init({
12
12
  language: 'en',
13
13
  ns: ['common'],
14
+ defaultNs: 'common',
14
15
  staticData: {
15
16
  'en:common': promiseEnCommon,
16
17
  'en:test': promiseEnTest,
@@ -22,7 +23,7 @@ const tolgeeWithNamespaces = () => {
22
23
 
23
24
  describe('language changes', () => {
24
25
  it('returns correct translation from namespace', () => {
25
- const tolgee = Tolgee({
26
+ const tolgee = TolgeeCore().init({
26
27
  language: 'en',
27
28
  staticData: {
28
29
  en: { hello: 'World' },
@@ -35,7 +36,7 @@ describe('language changes', () => {
35
36
  });
36
37
 
37
38
  it('uses defaultNs', async () => {
38
- const tolgee = Tolgee({
39
+ const tolgee = TolgeeCore().init({
39
40
  language: 'en',
40
41
  defaultNs: 'common',
41
42
  staticData: {
@@ -76,24 +77,46 @@ describe('language changes', () => {
76
77
 
77
78
  it('removing active namespaces', async () => {
78
79
  const tolgee = tolgeeWithNamespaces();
79
- await tolgee.run();
80
80
  const handler = jest.fn();
81
81
  tolgee.on('cache', handler);
82
+ await tolgee.run();
82
83
 
83
84
  expect(tolgee.t({ key: 'cancel', ns: 'common' })).toEqual('Cancel');
84
85
  expect(tolgee.t({ key: 'test', ns: 'test' })).toEqual('test');
85
86
 
86
87
  await tolgee.addActiveNs('test');
87
- expect(handler).toBeCalledTimes(1);
88
+ expect(handler).toBeCalledTimes(2);
88
89
 
89
90
  expect(tolgee.t({ key: 'cancel', ns: 'common' })).toEqual('Cancel');
90
91
  expect(tolgee.t({ key: 'test', ns: 'test' })).toEqual('Test');
91
92
 
92
93
  tolgee.removeActiveNs('test');
93
94
  await tolgee.changeLanguage('es');
94
- expect(handler).toBeCalledTimes(2);
95
+ expect(handler).toBeCalledTimes(3);
95
96
 
96
97
  expect(tolgee.t({ key: 'cancel', ns: 'common' })).toEqual('Cancellar');
97
98
  expect(tolgee.t({ key: 'test', ns: 'test' })).toEqual('test');
98
99
  });
100
+
101
+ it('add active namespace with forget', async () => {
102
+ const tolgee = tolgeeWithNamespaces();
103
+ const handler = jest.fn();
104
+ tolgee.on('cache', handler);
105
+ await tolgee.run();
106
+
107
+ // test ns is not loaded
108
+ expect(tolgee.t({ key: 'test', ns: 'test' })).toEqual('test');
109
+
110
+ await tolgee.addActiveNs('test', true);
111
+ expect(handler).toBeCalledTimes(2);
112
+
113
+ // test ns is loaded
114
+ expect(tolgee.t({ key: 'test', ns: 'test' })).toEqual('Test');
115
+
116
+ await tolgee.changeLanguage('es');
117
+ expect(handler).toBeCalledTimes(3);
118
+
119
+ // test ns is not loaded for spanish
120
+ expect(tolgee.t({ key: 'test', ns: 'test' })).toEqual('test');
121
+ });
99
122
  });
@@ -0,0 +1,64 @@
1
+ import { TolgeeCore } from '../TolgeeCore';
2
+
3
+ describe('initial options', () => {
4
+ it('combines options correctly', () => {
5
+ const tolgee = TolgeeCore()
6
+ .updateDefaults({
7
+ observerType: 'text',
8
+ observerOptions: { highlightColor: 'red', restrictedElements: ['a'] },
9
+ apiUrl: 'https://app.tolgee.io',
10
+ language: 'en',
11
+ })
12
+ .init({
13
+ language: 'cs',
14
+ apiUrl: 'http://localhost:8080',
15
+ observerOptions: { highlightWidth: 90 },
16
+ });
17
+
18
+ expect(tolgee.getInitialOptions().apiUrl).toEqual('http://localhost:8080');
19
+
20
+ tolgee.updateOptions({
21
+ apiUrl: 'http://localhost:8202',
22
+ });
23
+
24
+ const {
25
+ observerType,
26
+ apiUrl,
27
+ language,
28
+ defaultNs,
29
+ observerOptions: {
30
+ highlightColor,
31
+ highlightWidth,
32
+ restrictedElements,
33
+ inputPrefix,
34
+ },
35
+ } = tolgee.getInitialOptions();
36
+
37
+ expect(apiUrl).toEqual('http://localhost:8202');
38
+ expect(observerType).toEqual('text');
39
+ expect(language).toEqual('cs');
40
+ expect(highlightWidth).toEqual(90);
41
+ expect(restrictedElements).toEqual(['a']);
42
+ expect(highlightColor).toEqual('red');
43
+ expect(inputPrefix).toEqual('%-%tolgee:');
44
+ expect(defaultNs).toEqual('');
45
+ });
46
+
47
+ it('sanitizes url', () => {
48
+ const tolgee = TolgeeCore().init({
49
+ language: 'cs',
50
+ apiUrl: 'http://localhost:8080/',
51
+ observerOptions: { highlightWidth: 90 },
52
+ });
53
+
54
+ expect(tolgee.getInitialOptions().apiUrl).toEqual('http://localhost:8080');
55
+
56
+ tolgee.updateOptions({ apiUrl: 'http://localhost:8202/' });
57
+
58
+ expect(tolgee.getInitialOptions().apiUrl).toEqual('http://localhost:8202');
59
+
60
+ tolgee.overrideCredentials({ apiUrl: 'http://localhost:8000/' });
61
+
62
+ expect(tolgee.getInitialOptions().apiUrl).toEqual('http://localhost:8000');
63
+ });
64
+ });
@@ -1,15 +1,15 @@
1
- import { Tolgee } from '../Tolgee';
1
+ import { TolgeeCore } from '../TolgeeCore';
2
2
  import {
3
- FinalFormatterInterface,
4
- FormatterInterface,
5
- FormatterInterfaceFormatParams,
6
- ObserverInterface,
3
+ FinalFormatterMiddleware,
4
+ FormatterMiddleware,
5
+ FormatterMiddlewareFormatParams,
6
+ ObserverMiddleware,
7
7
  TolgeePlugin,
8
8
  WrapperWrapFunction,
9
9
  } from '../types';
10
10
 
11
11
  const testObserver =
12
- (outputNotFormattable: boolean): ObserverInterface =>
12
+ (outputNotFormattable: boolean): ObserverMiddleware =>
13
13
  () => {
14
14
  const wrap: WrapperWrapFunction = ({ key, translation }) => {
15
15
  return `${key}|${translation}`;
@@ -35,20 +35,20 @@ const testObserver =
35
35
  });
36
36
  };
37
37
 
38
- const testFormatter1: FormatterInterface = {
39
- format: ({ translation }: FormatterInterfaceFormatParams) => {
38
+ const testFormatter1: FormatterMiddleware = {
39
+ format: ({ translation }: FormatterMiddlewareFormatParams) => {
40
40
  return `(1${translation})`;
41
41
  },
42
42
  };
43
43
 
44
- const testFormatter2: FormatterInterface = {
45
- format: ({ translation }: FormatterInterfaceFormatParams) => {
44
+ const testFormatter2: FormatterMiddleware = {
45
+ format: ({ translation }: FormatterMiddlewareFormatParams) => {
46
46
  return `(2${translation})`;
47
47
  },
48
48
  };
49
49
 
50
- const testFinalFormatter: FinalFormatterInterface = {
51
- format: ({ translation }: FormatterInterfaceFormatParams) => {
50
+ const testFinalFormatter: FinalFormatterMiddleware = {
51
+ format: ({ translation }: FormatterMiddlewareFormatParams) => {
52
52
  return { final: translation };
53
53
  },
54
54
  };
@@ -69,30 +69,41 @@ const formattersPlugin: TolgeePlugin = (tolgee, tools) => {
69
69
 
70
70
  describe('plugins', () => {
71
71
  it('wraps and formats translation', () => {
72
- const tolgee = Tolgee({
72
+ const tolgee = TolgeeCore().init({
73
73
  language: 'en',
74
74
  staticData: { en: { hello: 'world' } },
75
75
  });
76
- tolgee.use(observerPlugin(false));
76
+ tolgee.addPlugin(observerPlugin(false));
77
77
  tolgee.run();
78
78
  expect(tolgee.t({ key: 'hello' })).toEqual('hello|world');
79
79
 
80
- tolgee.use(formattersPlugin);
80
+ tolgee.addPlugin(formattersPlugin);
81
81
  expect(tolgee.t({ key: 'hello' })).toEqual({ final: '(2(1hello|world))' });
82
82
  tolgee.stop();
83
83
  });
84
84
 
85
85
  it("won't format when observer doesn't return formattable text", () => {
86
- const tolgee = Tolgee({
86
+ const tolgee = TolgeeCore().init({
87
87
  language: 'en',
88
88
  staticData: { en: { hello: 'world' } },
89
89
  });
90
- tolgee.use(observerPlugin(true));
90
+ tolgee.addPlugin(observerPlugin(true));
91
91
  tolgee.run();
92
92
  expect(tolgee.t({ key: 'hello' })).toEqual('hello|world');
93
93
 
94
- tolgee.use(formattersPlugin);
94
+ tolgee.addPlugin(formattersPlugin);
95
95
  expect(tolgee.t({ key: 'hello' })).toEqual('hello|world');
96
96
  tolgee.stop();
97
97
  });
98
+
99
+ it("won't wrap before run", () => {
100
+ const tolgee = TolgeeCore().init({
101
+ language: 'en',
102
+ staticData: { en: { hello: 'world' } },
103
+ });
104
+ tolgee.addPlugin(observerPlugin(false));
105
+ expect(tolgee.t({ key: 'hello' })).toEqual('world');
106
+ tolgee.run();
107
+ expect(tolgee.t({ key: 'hello' })).toEqual('hello|world');
108
+ });
98
109
  });
package/src/helpers.ts CHANGED
@@ -1,3 +1,9 @@
1
+ import {
2
+ FallbackGeneral,
3
+ FallbackLanguageObject,
4
+ FallbackLanguageOption,
5
+ } from './types';
6
+
1
7
  export function isPromise(value: any) {
2
8
  return Boolean(value && typeof value.then === 'function');
3
9
  }
@@ -15,3 +21,50 @@ export const valueOrPromise = <T, R>(
15
21
 
16
22
  export const missingOptionError = (option: string) =>
17
23
  `Tolgee: You need to specify '${option}' option`;
24
+
25
+ export function isObject(item: any) {
26
+ return typeof item === 'object' && !Array.isArray(item) && item !== null;
27
+ }
28
+
29
+ export function getFallback(value: FallbackGeneral): string[] | undefined {
30
+ if (typeof value === 'string') {
31
+ return [value];
32
+ }
33
+ if (Array.isArray(value)) {
34
+ return value;
35
+ }
36
+ return undefined;
37
+ }
38
+
39
+ export function getFallbackArray(value: FallbackGeneral): string[] {
40
+ return getFallback(value) || [];
41
+ }
42
+
43
+ export function getFallbackFromStruct(
44
+ language: string,
45
+ fallbackLanguage: FallbackLanguageOption
46
+ ) {
47
+ if (isObject(fallbackLanguage)) {
48
+ return getFallbackArray(
49
+ (fallbackLanguage as FallbackLanguageObject)?.[language]
50
+ );
51
+ } else {
52
+ return getFallbackArray(fallbackLanguage as FallbackGeneral);
53
+ }
54
+ }
55
+
56
+ export function unique<T>(arr: T[]) {
57
+ return Array.from(new Set(arr));
58
+ }
59
+
60
+ export function sanitizeUrl(url: string | undefined) {
61
+ return url ? url.replace(/\/+$/, '') : url;
62
+ }
63
+
64
+ export function getErrorMessage(error: any): string | undefined {
65
+ if (typeof error === 'string') {
66
+ return error;
67
+ } else if (typeof error?.message === 'string') {
68
+ return error.message;
69
+ }
70
+ }
package/src/index.ts CHANGED
@@ -1,11 +1,5 @@
1
- export { Tolgee } from './Tolgee';
2
- export {
3
- RESTRICTED_ASCENDANT_ATTRIBUTE,
4
- DEVTOOLS_ID,
5
- TOLGEE_ATTRIBUTE_NAME,
6
- TOLGEE_HIGHLIGHTER_CLASS,
7
- TOLGEE_WRAPPED_ONLY_DATA_ATTRIBUTE,
8
- } from './constants';
1
+ export { getFallback, getFallbackArray } from './helpers';
2
+ export { TolgeeCore } from './TolgeeCore';
9
3
  export * from './types';
10
- export { getTranslateParams } from './TranslateParams';
11
- export { getFallback, getFallbackArray } from './Controller/State/helpers';
4
+ export { getTranslateProps } from './TranslateParams';
5
+ export { FormatSimple } from './FormatSimple/FormatSimple';
@@ -0,0 +1,37 @@
1
+ export type TranslationValue = string | undefined | null;
2
+
3
+ export type TranslationsFlat = Map<string, TranslationValue>;
4
+
5
+ export type TreeTranslationsData = {
6
+ [key: string]: TranslationValue | TreeTranslationsData;
7
+ };
8
+
9
+ export type CacheDescriptor = {
10
+ language: string;
11
+ namespace?: string;
12
+ };
13
+
14
+ export type CacheDescriptorInternal = {
15
+ language: string;
16
+ namespace: string;
17
+ };
18
+
19
+ export type CacheDescriptorWithKey = CacheDescriptorInternal & {
20
+ key?: string;
21
+ };
22
+
23
+ export type TranslationChanger = {
24
+ revert: () => void;
25
+ };
26
+
27
+ export type ChangeTranslationInterface = (
28
+ descriptor: CacheDescriptor,
29
+ key: string,
30
+ value: string
31
+ ) => TranslationChanger;
32
+
33
+ export type CachePublicRecord = {
34
+ data: TranslationsFlat;
35
+ language: string;
36
+ namespace: string;
37
+ };
@@ -0,0 +1,85 @@
1
+ import type { NsFallback } from './general';
2
+ import type { CacheDescriptorWithKey } from './cache';
3
+
4
+ export type Subscription = {
5
+ unsubscribe: () => void;
6
+ };
7
+
8
+ export type SubscriptionSelective = {
9
+ unsubscribe: () => void;
10
+ /**
11
+ * Subscribes to namespace(s)
12
+ * @param ns - namespace(s), if empty default namespace is used
13
+ *
14
+ * Can be used multiple times to subscribe for more.
15
+ */
16
+ subscribeNs(ns?: NsFallback): SubscriptionSelective;
17
+ };
18
+
19
+ export type ListenerEvent<T> = { value: T };
20
+ export type Listener<T> = (e: ListenerEvent<T>) => void;
21
+
22
+ export type TolgeeEvent =
23
+ | 'language'
24
+ | 'pendingLanguage'
25
+ | 'loading'
26
+ | 'fetching'
27
+ | 'initialLoad'
28
+ | 'running'
29
+ | 'cache'
30
+ | 'update';
31
+
32
+ export interface EventType {
33
+ language: string;
34
+ pendingLanguage: string;
35
+ loading: boolean;
36
+ fetching: boolean;
37
+ initialLoad: void;
38
+ running: boolean;
39
+ cache: CacheDescriptorWithKey;
40
+ update: void;
41
+ }
42
+
43
+ export type TolgeeOn<E extends keyof EventType = keyof EventType> = {
44
+ /**
45
+ * Emitted when any key needs (or might need) to be re-rendered.
46
+ * Similar to tolgee.onNsUpdate, except for all namespaces.
47
+ */
48
+ (event: 'update', handler: Listener<void>): Subscription;
49
+
50
+ /**
51
+ * Emitted on language change.
52
+ */
53
+ (event: 'language', handler: Listener<string>): Subscription;
54
+
55
+ /**
56
+ * Emitted on pendingLanguage change.
57
+ */
58
+ (event: 'pendingLanguage', handler: Listener<string>): Subscription;
59
+
60
+ /**
61
+ * Emitted on loading change. Changes when tolgee is loading some data for the first time.
62
+ */
63
+ (event: 'loading', handler: Listener<boolean>): Subscription;
64
+
65
+ /**
66
+ * Emitted on fetching change. Changes when tolgee is fetching any data.
67
+ */
68
+ (event: 'fetching', handler: Listener<boolean>): Subscription;
69
+
70
+ /**
71
+ * Emitted when `tolgee.run` method finishes.
72
+ */
73
+ (event: 'initialLoad', handler: Listener<void>): Subscription;
74
+
75
+ /**
76
+ * Emitted when internal `running` state changes.
77
+ */
78
+ (event: 'running', handler: Listener<boolean>): Subscription;
79
+
80
+ /**
81
+ * Emitted when cache changes.
82
+ */
83
+ (event: 'cache', handler: Listener<CacheDescriptorWithKey>): Subscription;
84
+ (event: E, handler: unknown): Subscription;
85
+ };
@@ -0,0 +1,50 @@
1
+ export type FallbackGeneral = undefined | false | string | string[];
2
+
3
+ export type NsType = string;
4
+
5
+ export type KeyType = string;
6
+
7
+ export type NsFallback = undefined | NsType | NsType[];
8
+
9
+ export type FallbackLanguageObject = Record<string, FallbackGeneral>;
10
+
11
+ export type FallbackLanguageOption = FallbackGeneral | FallbackLanguageObject;
12
+
13
+ export type DefaultParamType = string | number | bigint;
14
+
15
+ export type TranslateParams<T = DefaultParamType> = {
16
+ [key: string]: T;
17
+ };
18
+
19
+ export type TranslateOptions = {
20
+ ns?: NsType;
21
+ noWrap?: boolean;
22
+ orEmpty?: boolean;
23
+ };
24
+
25
+ export type TranslateProps<T = DefaultParamType> = {
26
+ key: KeyType;
27
+ defaultValue?: string;
28
+ params?: TranslateParams<T>;
29
+ } & TranslateOptions;
30
+
31
+ export type TranslatePropsInternal = TranslateProps & {
32
+ translation?: string;
33
+ };
34
+
35
+ type PropType<TObj> = TObj[keyof TObj];
36
+
37
+ export type CombinedOptions<T> = TranslateOptions & {
38
+ [key: string]: T | PropType<TranslateOptions>;
39
+ };
40
+
41
+ export type TFnType<T = DefaultParamType, R = string> = {
42
+ (key: string, defaultValue?: string, options?: CombinedOptions<T>): R;
43
+ (key: string, options?: CombinedOptions<T>): R;
44
+ (props: TranslateProps<T>): R;
45
+ };
46
+
47
+ export type KeyAndNamespacesInternal = Pick<
48
+ TranslatePropsInternal,
49
+ 'key' | 'ns'
50
+ >;
@@ -0,0 +1,19 @@
1
+ export * from './general';
2
+ export * from './events';
3
+ export * from './cache';
4
+ export * from './plugin';
5
+
6
+ export type {
7
+ State,
8
+ TolgeeOptions,
9
+ TolgeeOptionsInternal,
10
+ TolgeeStaticData,
11
+ } from '../Controller/State/initState';
12
+
13
+ export type {
14
+ ObserverOptions,
15
+ ObserverOptionsInternal,
16
+ ModifierKey,
17
+ } from '../Controller/State/observerOptions';
18
+
19
+ export type { TolgeeChainer, TolgeeInstance } from '../TolgeeCore';
@@ -0,0 +1,181 @@
1
+ import { ChangeTranslationInterface, TreeTranslationsData } from './cache';
2
+ import {
3
+ NsFallback,
4
+ NsType,
5
+ TranslateParams,
6
+ TranslatePropsInternal,
7
+ } from './general';
8
+ import type { ObserverOptionsInternal } from '../Controller/State/observerOptions';
9
+ import { TolgeeInstance } from '../TolgeeCore';
10
+
11
+ export type BackendDevProps = {
12
+ apiUrl?: string;
13
+ apiKey?: string;
14
+ projectId?: number | string;
15
+ };
16
+
17
+ export type BackendGetRecordProps = {
18
+ language: string;
19
+ namespace?: string;
20
+ };
21
+
22
+ export type BackendGetRecord = (
23
+ data: BackendGetRecordProps
24
+ ) => Promise<TreeTranslationsData | undefined> | undefined;
25
+
26
+ export interface BackendMiddleware {
27
+ getRecord: BackendGetRecord;
28
+ }
29
+
30
+ export type BackendGetDevRecord = (
31
+ data: BackendGetRecordProps & BackendDevProps
32
+ ) => Promise<TreeTranslationsData | undefined> | undefined;
33
+
34
+ export type BackendDevMiddleware = {
35
+ getRecord: BackendGetDevRecord;
36
+ };
37
+
38
+ export type KeyAndParams = {
39
+ key: string;
40
+ params?: TranslateParams;
41
+ defaultValue?: string;
42
+ ns?: NsType;
43
+ };
44
+
45
+ export type Unwrapped = { text: string; keys: KeyAndParams[] };
46
+
47
+ export type WrapperWrapProps = Pick<
48
+ TranslatePropsInternal,
49
+ 'key' | 'params' | 'defaultValue' | 'ns' | 'translation'
50
+ >;
51
+ export type WrapperWrapFunction = (props: WrapperWrapProps) => string;
52
+ export type WrapperUnwrapFunction = (text: string) => Unwrapped;
53
+
54
+ export type WrapperAttributeXPathGetter = (props: {
55
+ tag: string;
56
+ attribute: string;
57
+ }) => string;
58
+
59
+ export type HighlightInterface = (key?: string, ns?: NsFallback) => Highlighter;
60
+
61
+ export type ObserverRunProps = {
62
+ mouseHighlight: boolean;
63
+ };
64
+
65
+ export type ObserverMiddleware = (props: ObserverProps) => {
66
+ unwrap: (text: string) => Unwrapped;
67
+ wrap: WrapperWrapFunction;
68
+ retranslate: () => void;
69
+ stop: () => void;
70
+ run: (props: ObserverRunProps) => void;
71
+ highlight: HighlightInterface;
72
+ outputNotFormattable: boolean;
73
+ };
74
+
75
+ export type LanguageDetectorProps = {
76
+ availableLanguages: string[];
77
+ };
78
+
79
+ export type LanguageDetectorMiddleware = {
80
+ getLanguage: (
81
+ props: LanguageDetectorProps
82
+ ) => string | undefined | Promise<string | undefined>;
83
+ };
84
+
85
+ export type LanguageStorageMiddleware = {
86
+ getLanguage: () => string | undefined | Promise<string | undefined>;
87
+ setLanguage: (language: string) => void | Promise<void>;
88
+ };
89
+
90
+ export type DevCredentials =
91
+ | undefined
92
+ | {
93
+ apiUrl?: string;
94
+ apiKey?: string;
95
+ projectId?: string | number;
96
+ };
97
+ export type WrapperMiddleware = {
98
+ unwrap: WrapperUnwrapFunction;
99
+ wrap: WrapperWrapFunction;
100
+ getTextXPath: () => string;
101
+ getAttributeXPath: WrapperAttributeXPathGetter;
102
+ };
103
+
104
+ export type FormatterMiddlewareFormatParams = {
105
+ translation: string;
106
+ language: string;
107
+ params: Record<string, any> | undefined;
108
+ };
109
+
110
+ export type FormatterMiddleware = {
111
+ format: (props: FormatterMiddlewareFormatParams) => string;
112
+ };
113
+
114
+ export type TranslationOnClick = (data: {
115
+ keysAndDefaults: KeyAndParams[];
116
+ event: any;
117
+ }) => void;
118
+
119
+ export type ObserverProps = {
120
+ translate: (params: TranslatePropsInternal) => string;
121
+ onClick: TranslationOnClick;
122
+ options: ObserverOptionsInternal;
123
+ };
124
+
125
+ export type Highlighter = {
126
+ unhighlight(): void;
127
+ };
128
+
129
+ export type FinalFormatterMiddleware = {
130
+ format: (props: FormatterMiddlewareFormatParams) => any;
131
+ };
132
+
133
+ export type UiProps = {
134
+ apiUrl: string;
135
+ apiKey: string;
136
+ projectId: number | string | undefined;
137
+ highlight: HighlightInterface;
138
+ changeTranslation: ChangeTranslationInterface;
139
+ };
140
+
141
+ export type UiKeyOption = {
142
+ key: string;
143
+ defaultValue?: string;
144
+ ns: string[];
145
+ translation: string | undefined;
146
+ };
147
+
148
+ export type UiMiddleware = (props: UiProps) => UiInterface;
149
+
150
+ export interface UiInterface {
151
+ handleElementClick(keysAndDefaults: UiKeyOption[], event: any): Promise<void>;
152
+ }
153
+
154
+ export type PluginTools = Readonly<{
155
+ setFinalFormatter: (formatter: FinalFormatterMiddleware | undefined) => void;
156
+ addFormatter: (formatter: FormatterMiddleware | undefined) => void;
157
+ setObserver: (observer: ObserverMiddleware | undefined) => void;
158
+ hasObserver: () => boolean;
159
+ setUi: (ui: UiMiddleware | undefined) => void;
160
+ hasUi: () => boolean;
161
+ addBackend: (backend: BackendMiddleware | undefined) => void;
162
+ setDevBackend: (backend: BackendDevMiddleware | undefined) => void;
163
+ setLanguageDetector: (
164
+ languageDetector: LanguageDetectorMiddleware | undefined
165
+ ) => void;
166
+ setLanguageStorage: (
167
+ languageStorage: LanguageStorageMiddleware | undefined
168
+ ) => void;
169
+ }>;
170
+
171
+ export type TolgeePlugin = (
172
+ tolgee: TolgeeInstance,
173
+ tools: PluginTools
174
+ ) => TolgeeInstance;
175
+
176
+ export type FormatErrorHandler = (
177
+ error: string,
178
+ info: TranslatePropsInternal
179
+ ) => string;
180
+
181
+ export type OnFormatError = string | FormatErrorHandler;