@olenbetong/appframe-core 2.0.2 → 2.0.4

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.
@@ -1,7 +1,8 @@
1
- async function getLocalizedStringAsync(string) {
1
+ import { has } from "./has";
2
+ async function fetchLocalizedString(text) {
2
3
  try {
3
- let response = await fetch(`/api/user/localize`, {
4
- body: JSON.stringify({ string, ArticleID: af.article.id }),
4
+ let response = await fetch(`/api/user/localize/new/${af.article.id}`, {
5
+ body: JSON.stringify({ text }),
5
6
  headers: {
6
7
  Accept: "application/json; */*",
7
8
  "Content-Type": "application/json",
@@ -14,13 +15,24 @@ async function getLocalizedStringAsync(string) {
14
15
  }
15
16
  }
16
17
  catch (error) { }
17
- return string;
18
+ return text;
18
19
  }
19
- function afGetLocalizedString(string, wait) {
20
+ export function getLocalizedStringAsync(string) {
21
+ if (!string)
22
+ return string;
23
+ return getLocalizedString(string, true);
24
+ }
25
+ export function getLocalizedString(string, wait) {
26
+ if (typeof string === "string" && string.trim().length === 0) {
27
+ return string;
28
+ }
29
+ if (string === undefined || string === "" || string === null) {
30
+ return;
31
+ }
20
32
  if (af?.article?.i18n !== undefined) {
21
- if (!Object.prototype.hasOwnProperty.call(af.article.i18n, string)) {
33
+ if (!has(af.article.i18n, string)) {
22
34
  af.article.i18n[string] = string;
23
- let promise = getLocalizedStringAsync(string).then((text) => {
35
+ let promise = fetchLocalizedString(string).then((text) => {
24
36
  af.article.i18n[string] = text;
25
37
  return text;
26
38
  });
@@ -34,81 +46,3 @@ function afGetLocalizedString(string, wait) {
34
46
  return string;
35
47
  }
36
48
  }
37
- const key = "__UNLOCALIZED_STRINGS__";
38
- const localizeSet = window[key] || new Set();
39
- if (!window[key]) {
40
- window[key] = localizeSet;
41
- }
42
- function handleReadyStateChange() {
43
- if (document.readyState !== "loading") {
44
- addDeveloperUtils();
45
- document.removeEventListener("readystatechange", handleReadyStateChange);
46
- }
47
- }
48
- if (document.readyState !== "loading") {
49
- addDeveloperUtils();
50
- }
51
- else {
52
- document.addEventListener("readystatechange", handleReadyStateChange);
53
- }
54
- function isDeveloper() {
55
- const { af } = window;
56
- return af && af.userSession && af.userSession.isDeveloper;
57
- }
58
- function addDeveloperUtils() {
59
- if (isDeveloper()) {
60
- if (typeof window.__doNothingWithStrings === "function") {
61
- const addedStrings = window.__doNothingWithStrings();
62
- for (let string of Object.keys(addedStrings)) {
63
- localizeSet.add(string);
64
- }
65
- }
66
- if (!window[key]) {
67
- window[key] = localizeSet;
68
- }
69
- if (!window.__getUnlocalizedStrings) {
70
- window.__getUnlocalizedStrings = function (forCustomRoute) {
71
- const strings = [];
72
- for (let string of localizeSet) {
73
- if (string) {
74
- const escaped = string.replace(/'/g, "\\'");
75
- if (forCustomRoute) {
76
- const localized = afGetLocalizedString(string).replaceAll(/'/g, "\\'");
77
- strings.push(`'${escaped}': '${localized}',`);
78
- }
79
- else {
80
- strings.push(`s['${escaped}'] = @Html(@context.AddLocalizableString("${string.replaceAll('"', '\\"')}"));`);
81
- }
82
- }
83
- }
84
- if (forCustomRoute) {
85
- return `// Localization hack, since it AddLocalizableString doesn't work properly when custom routes is used
86
- if (af.userSession.uiculture === 'nb-NO') {
87
- const strings = {
88
- ${strings.join("\n\t\t")}
89
- };
90
- af.article.i18n = af.article.i18n || {};
91
- Object.assign(af.article.i18n, strings);
92
- }`;
93
- }
94
- return `<script type="text/javascript">
95
- function __doNothingWithStrings() {
96
- @{
97
- var context = @Appframe.Web.Context.RequestContext.Current;
98
- }
99
- let s = {};
100
- ${strings.join("\n")}
101
- return s;
102
- }
103
- </script>`;
104
- };
105
- }
106
- }
107
- }
108
- export function getLocalizedString(string) {
109
- const localized = afGetLocalizedString(string);
110
- if (localized === string && !localizeSet.has(string)) {
111
- localizeSet.add(string);
112
- }
113
- return localized;
114
- }
package/es/index.js CHANGED
@@ -10,4 +10,4 @@ export { default as localStorage } from "./localStorage.js";
10
10
  export { default as SessionHandler } from "./SessionHandler.js";
11
11
  export { UserSession };
12
12
  export * from "./settings.js";
13
- export const version = "2.0.2";
13
+ export const version = "2.0.4";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-core",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "Javascript libraries to utilize the Appframe framework",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -48,5 +48,5 @@
48
48
  "devDependencies": {
49
49
  "chalk": "^5.0.1"
50
50
  },
51
- "gitHead": "c192b2ce5f422635d0e38eaf984f286fd8120bc8"
51
+ "gitHead": "ad1c3f7787d9a631f76fd811d9383d9ac3dfa07b"
52
52
  }
@@ -1,7 +1,9 @@
1
- async function getLocalizedStringAsync(string: string) {
1
+ import { has } from "./has";
2
+
3
+ async function fetchLocalizedString(text: string) {
2
4
  try {
3
- let response = await fetch(`/api/user/localize`, {
4
- body: JSON.stringify({ string, ArticleID: af.article.id }),
5
+ let response = await fetch(`/api/user/localize/new/${af.article.id}`, {
6
+ body: JSON.stringify({ text }),
5
7
  headers: {
6
8
  Accept: "application/json; */*",
7
9
  "Content-Type": "application/json",
@@ -15,18 +17,38 @@ async function getLocalizedStringAsync(string: string) {
15
17
  }
16
18
  } catch (error) {}
17
19
 
18
- return string;
20
+ return text;
21
+ }
22
+ export function getLocalizedStringAsync(string: undefined | null): undefined | null;
23
+ export function getLocalizedStringAsync(string: string): Promise<string>;
24
+ export function getLocalizedStringAsync(string: string | undefined | null) {
25
+ if (!string) return string;
26
+
27
+ return getLocalizedString(string, true);
19
28
  }
20
29
 
21
- function afGetLocalizedString(string: string): string;
22
- function afGetLocalizedString(string: string, wait: false): string;
23
- function afGetLocalizedString(string: string, wait: true): Promise<string>;
24
- function afGetLocalizedString(string: string, wait?: boolean): string | Promise<string> {
30
+ export function getLocalizedString(string: undefined | null): undefined | null;
31
+ export function getLocalizedString(string: string): string;
32
+ export function getLocalizedString(string: string, wait: false): string;
33
+ export function getLocalizedString(string: string, wait: true): Promise<string>;
34
+ export function getLocalizedString(string: undefined | null, wait: true): undefined | null;
35
+ export function getLocalizedString(
36
+ string: string | undefined | null,
37
+ wait?: boolean
38
+ ): string | undefined | null | Promise<string> {
39
+ if (typeof string === "string" && string.trim().length === 0) {
40
+ return string;
41
+ }
42
+
43
+ if (string === undefined || string === "" || string === null) {
44
+ return;
45
+ }
46
+
25
47
  if (af?.article?.i18n !== undefined) {
26
- if (!Object.prototype.hasOwnProperty.call(af.article.i18n, string)) {
48
+ if (!has(af.article.i18n, string)) {
27
49
  af.article.i18n[string] = string;
28
50
 
29
- let promise = getLocalizedStringAsync(string).then((text) => {
51
+ let promise = fetchLocalizedString(string).then((text) => {
30
52
  af.article.i18n[string] = text;
31
53
  return text;
32
54
  });
@@ -41,104 +63,3 @@ function afGetLocalizedString(string: string, wait?: boolean): string | Promise<
41
63
  return string;
42
64
  }
43
65
  }
44
-
45
- declare global {
46
- interface Window {
47
- __UNLOCALIZED_STRINGS__: Set<string>;
48
- __doNothingWithStrings?: () => Record<string, string>;
49
- __getUnlocalizedStrings?: (forCustomRoute?: boolean) => string;
50
- }
51
- }
52
-
53
- const key = "__UNLOCALIZED_STRINGS__";
54
- const localizeSet = window[key] || new Set();
55
-
56
- if (!window[key]) {
57
- window[key] = localizeSet;
58
- }
59
-
60
- function handleReadyStateChange() {
61
- if (document.readyState !== "loading") {
62
- addDeveloperUtils();
63
- document.removeEventListener("readystatechange", handleReadyStateChange);
64
- }
65
- }
66
-
67
- if (document.readyState !== "loading") {
68
- addDeveloperUtils();
69
- } else {
70
- document.addEventListener("readystatechange", handleReadyStateChange);
71
- }
72
-
73
- function isDeveloper() {
74
- const { af } = window;
75
-
76
- return af && af.userSession && af.userSession.isDeveloper;
77
- }
78
-
79
- function addDeveloperUtils() {
80
- if (isDeveloper()) {
81
- if (typeof window.__doNothingWithStrings === "function") {
82
- const addedStrings = window.__doNothingWithStrings();
83
-
84
- for (let string of Object.keys(addedStrings)) {
85
- localizeSet.add(string);
86
- }
87
- }
88
-
89
- if (!window[key]) {
90
- window[key] = localizeSet;
91
- }
92
-
93
- if (!window.__getUnlocalizedStrings) {
94
- window.__getUnlocalizedStrings = function (forCustomRoute) {
95
- const strings = [];
96
-
97
- for (let string of localizeSet) {
98
- if (string) {
99
- const escaped = string.replace(/'/g, "\\'");
100
- if (forCustomRoute) {
101
- const localized = afGetLocalizedString(string).replaceAll(/'/g, "\\'");
102
- strings.push(`'${escaped}': '${localized}',`);
103
- } else {
104
- strings.push(
105
- `s['${escaped}'] = @Html(@context.AddLocalizableString("${string.replaceAll('"', '\\"')}"));`
106
- );
107
- }
108
- }
109
- }
110
-
111
- if (forCustomRoute) {
112
- return `// Localization hack, since it AddLocalizableString doesn't work properly when custom routes is used
113
- if (af.userSession.uiculture === 'nb-NO') {
114
- const strings = {
115
- ${strings.join("\n\t\t")}
116
- };
117
- af.article.i18n = af.article.i18n || {};
118
- Object.assign(af.article.i18n, strings);
119
- }`;
120
- }
121
-
122
- return `<script type="text/javascript">
123
- function __doNothingWithStrings() {
124
- @{
125
- var context = @Appframe.Web.Context.RequestContext.Current;
126
- }
127
- let s = {};
128
- ${strings.join("\n")}
129
- return s;
130
- }
131
- </script>`;
132
- };
133
- }
134
- }
135
- }
136
-
137
- export function getLocalizedString(string: string) {
138
- const localized = afGetLocalizedString(string);
139
- if (localized === string && !localizeSet.has(string)) {
140
- localizeSet.add(string);
141
- }
142
-
143
- return localized;
144
- }