@yxw007/translate 0.0.3 → 0.0.5

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,4 +1,4 @@
1
- // translate v0.0.3 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
1
+ // translate v0.0.5 Copyright (c) 2024 Potter<aa4790139@gmail.com> and contributors
2
2
  'use strict';
3
3
 
4
4
  Object.defineProperty(exports, '__esModule', { value: true });
@@ -8,7 +8,7 @@ function google(options) {
8
8
  return {
9
9
  name: "google",
10
10
  async translate(text, opts) {
11
- const { from, to } = opts;
11
+ const { from = "auto", to } = opts;
12
12
  if (!Array.isArray(text)) {
13
13
  text = [text];
14
14
  }
@@ -42,7 +42,7 @@ function azure(options) {
42
42
  name: "azure",
43
43
  async translate(text, opts) {
44
44
  const { from, to } = opts;
45
- const url = `${base}&from=${from}&to=${to}`;
45
+ const url = `${base}&to=${to}${from && from !== "auto" ? `&from=${from}` : ""}`;
46
46
  if (!Array.isArray(text)) {
47
47
  text = [text];
48
48
  }
@@ -4005,7 +4005,7 @@ const commonParams = {
4005
4005
 
4006
4006
  var name = "@aws-sdk/client-translate";
4007
4007
  var description = "AWS SDK for JavaScript Translate Client for Node.js, Browser and React Native";
4008
- var version = "3.645.0";
4008
+ var version = "3.649.0";
4009
4009
  var scripts = {
4010
4010
  build: "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
4011
4011
  "build:cjs": "node ../../scripts/compilation/inline client-translate",
@@ -4024,43 +4024,43 @@ var sideEffects = false;
4024
4024
  var dependencies = {
4025
4025
  "@aws-crypto/sha256-browser": "5.2.0",
4026
4026
  "@aws-crypto/sha256-js": "5.2.0",
4027
- "@aws-sdk/client-sso-oidc": "3.645.0",
4028
- "@aws-sdk/client-sts": "3.645.0",
4029
- "@aws-sdk/core": "3.635.0",
4030
- "@aws-sdk/credential-provider-node": "3.645.0",
4031
- "@aws-sdk/middleware-host-header": "3.620.0",
4032
- "@aws-sdk/middleware-logger": "3.609.0",
4033
- "@aws-sdk/middleware-recursion-detection": "3.620.0",
4034
- "@aws-sdk/middleware-user-agent": "3.645.0",
4035
- "@aws-sdk/region-config-resolver": "3.614.0",
4036
- "@aws-sdk/types": "3.609.0",
4037
- "@aws-sdk/util-endpoints": "3.645.0",
4038
- "@aws-sdk/util-user-agent-browser": "3.609.0",
4039
- "@aws-sdk/util-user-agent-node": "3.614.0",
4040
- "@smithy/config-resolver": "^3.0.5",
4041
- "@smithy/core": "^2.4.0",
4042
- "@smithy/fetch-http-handler": "^3.2.4",
4043
- "@smithy/hash-node": "^3.0.3",
4044
- "@smithy/invalid-dependency": "^3.0.3",
4045
- "@smithy/middleware-content-length": "^3.0.5",
4046
- "@smithy/middleware-endpoint": "^3.1.0",
4047
- "@smithy/middleware-retry": "^3.0.15",
4048
- "@smithy/middleware-serde": "^3.0.3",
4049
- "@smithy/middleware-stack": "^3.0.3",
4050
- "@smithy/node-config-provider": "^3.1.4",
4051
- "@smithy/node-http-handler": "^3.1.4",
4052
- "@smithy/protocol-http": "^4.1.0",
4053
- "@smithy/smithy-client": "^3.2.0",
4054
- "@smithy/types": "^3.3.0",
4055
- "@smithy/url-parser": "^3.0.3",
4027
+ "@aws-sdk/client-sso-oidc": "3.649.0",
4028
+ "@aws-sdk/client-sts": "3.649.0",
4029
+ "@aws-sdk/core": "3.649.0",
4030
+ "@aws-sdk/credential-provider-node": "3.649.0",
4031
+ "@aws-sdk/middleware-host-header": "3.649.0",
4032
+ "@aws-sdk/middleware-logger": "3.649.0",
4033
+ "@aws-sdk/middleware-recursion-detection": "3.649.0",
4034
+ "@aws-sdk/middleware-user-agent": "3.649.0",
4035
+ "@aws-sdk/region-config-resolver": "3.649.0",
4036
+ "@aws-sdk/types": "3.649.0",
4037
+ "@aws-sdk/util-endpoints": "3.649.0",
4038
+ "@aws-sdk/util-user-agent-browser": "3.649.0",
4039
+ "@aws-sdk/util-user-agent-node": "3.649.0",
4040
+ "@smithy/config-resolver": "^3.0.6",
4041
+ "@smithy/core": "^2.4.1",
4042
+ "@smithy/fetch-http-handler": "^3.2.5",
4043
+ "@smithy/hash-node": "^3.0.4",
4044
+ "@smithy/invalid-dependency": "^3.0.4",
4045
+ "@smithy/middleware-content-length": "^3.0.6",
4046
+ "@smithy/middleware-endpoint": "^3.1.1",
4047
+ "@smithy/middleware-retry": "^3.0.16",
4048
+ "@smithy/middleware-serde": "^3.0.4",
4049
+ "@smithy/middleware-stack": "^3.0.4",
4050
+ "@smithy/node-config-provider": "^3.1.5",
4051
+ "@smithy/node-http-handler": "^3.2.0",
4052
+ "@smithy/protocol-http": "^4.1.1",
4053
+ "@smithy/smithy-client": "^3.3.0",
4054
+ "@smithy/types": "^3.4.0",
4055
+ "@smithy/url-parser": "^3.0.4",
4056
4056
  "@smithy/util-base64": "^3.0.0",
4057
4057
  "@smithy/util-body-length-browser": "^3.0.0",
4058
4058
  "@smithy/util-body-length-node": "^3.0.0",
4059
- "@smithy/util-defaults-mode-browser": "^3.0.15",
4060
- "@smithy/util-defaults-mode-node": "^3.0.15",
4061
- "@smithy/util-endpoints": "^2.0.5",
4062
- "@smithy/util-middleware": "^3.0.3",
4063
- "@smithy/util-retry": "^3.0.3",
4059
+ "@smithy/util-defaults-mode-browser": "^3.0.16",
4060
+ "@smithy/util-defaults-mode-node": "^3.0.16",
4061
+ "@smithy/util-endpoints": "^2.1.0",
4062
+ "@smithy/util-middleware": "^3.0.4",
4063
+ "@smithy/util-retry": "^3.0.4",
4064
4064
  "@smithy/util-utf8": "^3.0.0",
4065
4065
  tslib: "^2.6.2",
4066
4066
  uuid: "^9.0.1"
@@ -5390,7 +5390,7 @@ function amazon(options) {
5390
5390
  return {
5391
5391
  name: "amazon",
5392
5392
  async translate(text, opts) {
5393
- const { from, to } = opts;
5393
+ const { from = "auto", to } = opts;
5394
5394
  const translateClient = new TranslateClient({ region: region, credentials: { accessKeyId, secretAccessKey } });
5395
5395
  if (!Array.isArray(text)) {
5396
5396
  text = [text];
@@ -6509,7 +6509,7 @@ function baidu(options) {
6509
6509
  return {
6510
6510
  name: "baidu",
6511
6511
  async translate(text, opts) {
6512
- const { to, from, domain = "it" } = opts;
6512
+ const { to, from = "auto", domain = "it" } = opts;
6513
6513
  if (!Array.isArray(text)) {
6514
6514
  text = [text];
6515
6515
  }
@@ -6740,13 +6740,255 @@ var iso = {
6740
6740
  zul: "zu",
6741
6741
  };
6742
6742
 
6743
- const isoKeys = Object.values(iso).sort();
6744
- function language(name) {
6743
+ var languageNames = {
6744
+ afar: "aa",
6745
+ abkhazian: "ab",
6746
+ afrikaans: "af",
6747
+ akan: "ak",
6748
+ albanian: "sq",
6749
+ amharic: "am",
6750
+ arabic: "ar",
6751
+ aragonese: "an",
6752
+ armenian: "hy",
6753
+ assamese: "as",
6754
+ avaric: "av",
6755
+ avestan: "ae",
6756
+ aymara: "ay",
6757
+ azerbaijani: "az",
6758
+ bashkir: "ba",
6759
+ bambara: "bm",
6760
+ basque: "eu",
6761
+ belarusian: "be",
6762
+ bengali: "bn",
6763
+ "bihari languages": "bh",
6764
+ bislama: "bi",
6765
+ tibetan: "bo",
6766
+ bosnian: "bs",
6767
+ breton: "br",
6768
+ bulgarian: "bg",
6769
+ burmese: "my",
6770
+ catalan: "ca",
6771
+ valencian: "ca",
6772
+ czech: "cs",
6773
+ chamorro: "ch",
6774
+ chechen: "ce",
6775
+ chinese: "zh",
6776
+ "church slavic": "cu",
6777
+ "old slavonic": "cu",
6778
+ "church slavonic": "cu",
6779
+ "old bulgarian": "cu",
6780
+ "old church slavonic": "cu",
6781
+ chuvash: "cv",
6782
+ cornish: "kw",
6783
+ corsican: "co",
6784
+ cree: "cr",
6785
+ welsh: "cy",
6786
+ danish: "da",
6787
+ german: "de",
6788
+ divehi: "dv",
6789
+ dhivehi: "dv",
6790
+ maldivian: "dv",
6791
+ dutch: "nl",
6792
+ flemish: "nl",
6793
+ dzongkha: "dz",
6794
+ greek: "el",
6795
+ english: "en",
6796
+ esperanto: "eo",
6797
+ estonian: "et",
6798
+ ewe: "ee",
6799
+ faroese: "fo",
6800
+ persian: "fa",
6801
+ fijian: "fj",
6802
+ finnish: "fi",
6803
+ french: "fr",
6804
+ "western frisian": "fy",
6805
+ fulah: "ff",
6806
+ georgian: "ka",
6807
+ gaelic: "gd",
6808
+ "scottish gaelic": "gd",
6809
+ irish: "ga",
6810
+ galician: "gl",
6811
+ manx: "gv",
6812
+ guarani: "gn",
6813
+ gujarati: "gu",
6814
+ haitian: "ht",
6815
+ "haitian creole": "ht",
6816
+ hausa: "ha",
6817
+ hebrew: "he",
6818
+ herero: "hz",
6819
+ hindi: "hi",
6820
+ "hiri motu": "ho",
6821
+ croatian: "hr",
6822
+ hungarian: "hu",
6823
+ igbo: "ig",
6824
+ icelandic: "is",
6825
+ ido: "io",
6826
+ "sichuan yi": "ii",
6827
+ nuosu: "ii",
6828
+ inuktitut: "iu",
6829
+ interlingue: "ie",
6830
+ occidental: "ie",
6831
+ interlingua: "ia",
6832
+ indonesian: "id",
6833
+ inupiaq: "ik",
6834
+ italian: "it",
6835
+ javanese: "jv",
6836
+ japanese: "ja",
6837
+ kalaallisut: "kl",
6838
+ greenlandic: "kl",
6839
+ kannada: "kn",
6840
+ kashmiri: "ks",
6841
+ kanuri: "kr",
6842
+ kazakh: "kk",
6843
+ "central khmer": "km",
6844
+ kikuyu: "ki",
6845
+ gikuyu: "ki",
6846
+ kinyarwanda: "rw",
6847
+ kirghiz: "ky",
6848
+ kyrgyz: "ky",
6849
+ komi: "kv",
6850
+ kongo: "kg",
6851
+ korean: "ko",
6852
+ kuanyama: "kj",
6853
+ kwanyama: "kj",
6854
+ kurdish: "ku",
6855
+ lao: "lo",
6856
+ latin: "la",
6857
+ latvian: "lv",
6858
+ limburgan: "li",
6859
+ limburger: "li",
6860
+ limburgish: "li",
6861
+ lingala: "ln",
6862
+ lithuanian: "lt",
6863
+ luxembourgish: "lb",
6864
+ letzeburgesch: "lb",
6865
+ "luba-katanga": "lu",
6866
+ ganda: "lg",
6867
+ macedonian: "mk",
6868
+ marshallese: "mh",
6869
+ malayalam: "ml",
6870
+ maori: "mi",
6871
+ marathi: "mr",
6872
+ malay: "ms",
6873
+ malagasy: "mg",
6874
+ maltese: "mt",
6875
+ mongolian: "mn",
6876
+ nauru: "na",
6877
+ navajo: "nv",
6878
+ navaho: "nv",
6879
+ "ndebele, south": "nr",
6880
+ "south ndebele": "nr",
6881
+ "ndebele, north": "nd",
6882
+ "north ndebele": "nd",
6883
+ ndonga: "ng",
6884
+ nepali: "ne",
6885
+ "norwegian nynorsk": "nn",
6886
+ "nynorsk, norwegian": "nn",
6887
+ "norwegian bokmål": "nb",
6888
+ "bokmål, norwegian": "nb",
6889
+ norwegian: "no",
6890
+ chichewa: "ny",
6891
+ chewa: "ny",
6892
+ nyanja: "ny",
6893
+ occitan: "oc",
6894
+ ojibwa: "oj",
6895
+ oriya: "or",
6896
+ oromo: "om",
6897
+ ossetian: "os",
6898
+ ossetic: "os",
6899
+ panjabi: "pa",
6900
+ punjabi: "pa",
6901
+ pali: "pi",
6902
+ polish: "pl",
6903
+ portuguese: "pt",
6904
+ pushto: "ps",
6905
+ pashto: "ps",
6906
+ quechua: "qu",
6907
+ romansh: "rm",
6908
+ romanian: "ro",
6909
+ moldavian: "ro",
6910
+ moldovan: "ro",
6911
+ rundi: "rn",
6912
+ russian: "ru",
6913
+ sango: "sg",
6914
+ sanskrit: "sa",
6915
+ sinhala: "si",
6916
+ sinhalese: "si",
6917
+ slovak: "sk",
6918
+ slovenian: "sl",
6919
+ "northern sami": "se",
6920
+ samoan: "sm",
6921
+ shona: "sn",
6922
+ sindhi: "sd",
6923
+ somali: "so",
6924
+ "sotho, southern": "st",
6925
+ spanish: "es",
6926
+ castilian: "es",
6927
+ sardinian: "sc",
6928
+ serbian: "sr",
6929
+ swati: "ss",
6930
+ sundanese: "su",
6931
+ swahili: "sw",
6932
+ swedish: "sv",
6933
+ tahitian: "ty",
6934
+ tamil: "ta",
6935
+ tatar: "tt",
6936
+ telugu: "te",
6937
+ tajik: "tg",
6938
+ tagalog: "tl",
6939
+ thai: "th",
6940
+ tigrinya: "ti",
6941
+ tonga: "to",
6942
+ tswana: "tn",
6943
+ tsonga: "ts",
6944
+ turkmen: "tk",
6945
+ turkish: "tr",
6946
+ twi: "tw",
6947
+ uighur: "ug",
6948
+ uyghur: "ug",
6949
+ ukrainian: "uk",
6950
+ urdu: "ur",
6951
+ uzbek: "uz",
6952
+ venda: "ve",
6953
+ vietnamese: "vi",
6954
+ volapük: "vo",
6955
+ walloon: "wa",
6956
+ wolof: "wo",
6957
+ xhosa: "xh",
6958
+ yiddish: "yi",
6959
+ yoruba: "yo",
6960
+ zhuang: "za",
6961
+ chuang: "za",
6962
+ zulu: "zu",
6963
+ };
6964
+
6965
+ const nameKeys = Object.keys(languageNames).sort();
6966
+ const isoKeys = Object.keys(iso).sort();
6967
+ const iosValues = Object.values(iso);
6968
+ function isValidLanguage(name) {
6969
+ if (!name) {
6970
+ return false;
6971
+ }
6972
+ if (name === "auto") {
6973
+ return true;
6974
+ }
6745
6975
  if (name.length > 100) {
6746
6976
  throw new Error(`The "language" is too long at ${name.length} characters`);
6747
6977
  }
6748
- if (!isoKeys.includes(name)) {
6749
- throw new Error(`The language "${name}" is not part of the ISO 639-1`);
6978
+ return isoKeys.includes(name) || nameKeys.includes(name) || iosValues.includes(name);
6979
+ }
6980
+ function getISO(name) {
6981
+ if (name === "auto") {
6982
+ return name;
6983
+ }
6984
+ if (nameKeys.includes(name)) {
6985
+ return languageNames[name];
6986
+ }
6987
+ if (isoKeys.includes(name)) {
6988
+ return iso[name];
6989
+ }
6990
+ if (iosValues.includes(name)) {
6991
+ return name;
6750
6992
  }
6751
6993
  return name;
6752
6994
  }
@@ -6843,17 +7085,20 @@ class Translator {
6843
7085
  this.engines.set(engine.name, engine);
6844
7086
  }
6845
7087
  translate(text, options) {
6846
- const { from, to, engine = "google", cache_time = 60 * 1000 } = options;
7088
+ const { engine = "google", cache_time = 60 * 1000 } = options;
7089
+ let { from = "auto", to } = options;
7090
+ from = options.from = getISO(from);
7091
+ to = options.to = getISO(to);
6847
7092
  //1. Check if engine exists
6848
7093
  if (!this.engines.has(engine)) {
6849
7094
  throw new Error(`Engine ${engine} not found`);
6850
7095
  }
6851
7096
  //2. Check if language exists
6852
- if (!language(from)) {
6853
- throw new Error(`Language ${from} not found`);
7097
+ if (!isValidLanguage(to)) {
7098
+ throw new Error(`The language "${to}" is not part of the ISO 639-1 or is not part of the language names`);
6854
7099
  }
6855
- if (!language(to)) {
6856
- throw new Error(`Language ${to} not found`);
7100
+ if (!isValidLanguage(from)) {
7101
+ throw new Error(`The language "${from}" is not part of the ISO 639-1 or is not part of the language names`);
6857
7102
  }
6858
7103
  const key = `${from}:${to}:${engine}:${text}`;
6859
7104
  //3. If the cache is matched, the cache is used directly
@@ -6874,10 +7119,13 @@ const translator = new Translator();
6874
7119
  var index = {
6875
7120
  engines,
6876
7121
  translator,
7122
+ Cache,
7123
+ languageNames,
6877
7124
  };
6878
7125
 
6879
7126
  exports.Cache = Cache;
6880
7127
  exports.default = index;
6881
7128
  exports.engines = engines;
7129
+ exports.languageNames = languageNames;
6882
7130
  exports.translator = translator;
6883
7131
  //# sourceMappingURL=index.cjs.map