Skip to content

Commit 51c8ec3

Browse files
committed
feat: ignoring URLs in translation feature added
1 parent 85445ab commit 51c8ec3

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,22 @@ To ignore words on translation use `{{word}}` OR `{word}` style on your object.
386386
}
387387
```
388388

389+
- jsontt also ignores the `URL` in the text which means sometimes translations ruin the URL in the given string while translating that string. It prevents such cases by ignoring URLs in the string while translating.
390+
391+
- You don't especially need to do anything for it, it ignores them automatically.
392+
393+
```
394+
{
395+
"text": "this is a puppy https://shorturl.at/lvPY5"
396+
}
397+
398+
...translating to german
399+
400+
{
401+
"text": "das ist ein welpe https://shorturl.at/lvPY5"
402+
}
403+
```
404+
389405
## **7. CLI commands**
390406

391407
- translate
@@ -470,7 +486,7 @@ jsontt --help
470486

471487
:heavy_check_mark: Argos Translate option (CLI)
472488

473-
- [ ] Ignore URL translation on given string
489+
:heavy_check_mark: Ignore URL translation on given string
474490

475491
- [ ] Libre Translate option (in code package)
476492

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.4.2",
2+
"version": "1.5.0",
33
"license": "MIT",
44
"main": "dist/index.js",
55
"description": "Translate your JSON file or object into another languages with Google Translate API",
@@ -66,7 +66,7 @@
6666
"@types/bluebird": "^3.5.36",
6767
"@types/filesystem": "^0.0.32",
6868
"@vitalets/google-translate-api": "^9.0.0",
69-
"axios": "^0.27.2",
69+
"axios": "^1.2.2",
7070
"bluebird": "^3.7.2",
7171
"cwait": "^1.1.2",
7272
"http-proxy-agent": "^5.0.0",

src/core/ignorer.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export function map(
55
db_map: { [key: string]: string };
66
sb_map: { [key: string]: string };
77
} {
8+
// encode urls if exists in the str
9+
str = urlEncoder(str);
10+
811
let { map: db_map, word: initial_ignored_word } = mapByDoubleBracket(str);
912
let { map: sb_map, word: ignored_word } = mapBySingleBracket(
1013
initial_ignored_word
@@ -21,6 +24,9 @@ export function unMap(str: string, db_map: object, sb_map: object): string {
2124
let word = unmapBySingleBracket(str, sb_map);
2225
word = unmapByDoubleBracket(word, db_map);
2326

27+
// decode urls if exists in the str
28+
word = urlDecoder(word);
29+
2430
return word;
2531
}
2632

@@ -89,3 +95,28 @@ function unmapIgnoredValues(
8995

9096
return str;
9197
}
98+
99+
// URL detector & encode AND decoder
100+
function urlEncoder(text: string): string {
101+
// url finder regex => url
102+
const regex = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!;:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!;:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!;:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/gim;
103+
104+
let new_text = text.replace(regex, function(url) {
105+
url = `{` + url + `}`;
106+
return url;
107+
});
108+
109+
return new_text;
110+
}
111+
112+
function urlDecoder(text: string): string {
113+
// url finder regex => {url}
114+
const regex = /{(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!;:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!;:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!;:,.]*\)|[A-Z0-9+&@#\/%=~_|$])}/gim;
115+
116+
let new_text = text.replace(regex, function(url) {
117+
url = url.substring(1, url.length - 1);
118+
return url;
119+
});
120+
121+
return new_text;
122+
}

yarn.lock

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,13 +1940,14 @@ axe-core@^4.4.3:
19401940
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.5.1.tgz#04d561c11b6d76d096d34e9d14ba2c294fb20cdc"
19411941
integrity sha512-1exVbW0X1O/HSr/WMwnaweyqcWOgZgLiVxdLG34pvSQk4NlYQr9OUy0JLwuhFfuVNQzzqgH57eYzkFBCb3bIsQ==
19421942

1943-
axios@^0.27.2:
1944-
version "0.27.2"
1945-
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
1946-
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
1943+
axios@^1.2.2:
1944+
version "1.2.2"
1945+
resolved "https://registry.yarnpkg.com/axios/-/axios-1.2.2.tgz#72681724c6e6a43a9fea860fc558127dbe32f9f1"
1946+
integrity sha512-bz/J4gS2S3I7mpN/YZfGFTqhXTYzRho8Ay38w2otuuDR322KzFIWm/4W2K6gIwvWaws5n+mnb7D1lN9uD+QH6Q==
19471947
dependencies:
1948-
follow-redirects "^1.14.9"
1948+
follow-redirects "^1.15.0"
19491949
form-data "^4.0.0"
1950+
proxy-from-env "^1.1.0"
19501951

19511952
axobject-query@^2.2.0:
19521953
version "2.2.0"
@@ -3354,7 +3355,7 @@ flatted@^2.0.0:
33543355
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.2.tgz#4575b21e2bcee7434aa9be662f4b7b5f9c2b5138"
33553356
integrity sha512-r5wGx7YeOwNWNlCA0wQ86zKyDLMQr+/RB8xy74M4hTphfmjlijTSSXGuH8rnvKZnfT9i+75zmd8jcKdMR4O6jA==
33563357

3357-
follow-redirects@^1.14.9:
3358+
follow-redirects@^1.15.0:
33583359
version "1.15.2"
33593360
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
33603361
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
@@ -5864,6 +5865,11 @@ prop-types@^15.8.1:
58645865
object-assign "^4.1.1"
58655866
react-is "^16.13.1"
58665867

5868+
proxy-from-env@^1.1.0:
5869+
version "1.1.0"
5870+
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
5871+
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
5872+
58675873
psl@^1.1.28, psl@^1.1.33:
58685874
version "1.9.0"
58695875
resolved "https://registry.yarnpkg.com/psl/-/psl-1.9.0.tgz#d0df2a137f00794565fcaf3b2c00cd09f8d5a5a7"

0 commit comments

Comments
 (0)