Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translation lang enum #126

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/input-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,13 @@ function defineType(info) {

let baseParamsMeta = null;
let getParams = () => undefined;
const getMeta = (params) =>
Array.isArray(params) ? params : Object.keys(params);
if (typeof (info.baseParams) === "object") {
getParams = () => info.baseParams;
baseParamsMeta = Array.isArray(info.baseParams)
? info.baseParams
: Object.keys(info.baseParams);
if (!(info.baseParams instanceof Promise)) {
baseParamsMeta = getMeta(info.baseParams);
}
} else if (typeof (info.baseParams) === "function") {
getParams = info.baseParams;
} else if (info.baseParams) {
Expand All @@ -177,6 +179,13 @@ function defineType(info) {
params: baseParamsMeta,
},
};
// promises get late linking into the type metadata
if (info.baseParams instanceof Promise) {
(async () => {
typeMeta.baseType.params = getMeta(await info.baseParams);
})();
}

const argType = new RPCArgumentType(
info.name,
typeMeta,
Expand Down
40 changes: 25 additions & 15 deletions src/procedures/translation/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

const { AzureTranslationKey } = require("../utils/api-key");
const types = require("../../input-types");
const ApiConsumer = require("../utils/api-consumer");
const TranslationConsumer = new ApiConsumer(
"Translation",
Expand All @@ -16,6 +17,25 @@ const TranslationConsumer = new ApiConsumer(
);
ApiConsumer.setRequiredApiKey(TranslationConsumer, AzureTranslationKey);

let SUPPORTED_LANGUAGES = undefined;
types.defineType({
name: "Language",
description:
"A language supported by the :doc:`/services/Translation/index` service.",
baseType: "Enum",
baseParams: (async () => {
SUPPORTED_LANGUAGES = (await TranslationConsumer._requestData({
path: "languages",
queryString: "?api-version=3.0&scope=translation",
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": TranslationConsumer.apiKey.value,
},
})).translation;
return Object.keys(SUPPORTED_LANGUAGES);
})(),
});

/**
* Generates a GUID-like string
*/
Expand All @@ -29,8 +49,8 @@ TranslationConsumer._get_guid = function () {
/**
* Translate text between languages
* @param {String} text Text in another language
* @param {String=} from Language to translate from (auto-detects if not specified)
* @param {String} to Language to translate to
* @param {Language=} from Language to translate from (auto-detects if not specified)
* @param {Language} to Language to translate to
* @returns {String} Text translated to requested language
*/
TranslationConsumer.translate = function (text, from, to) {
Expand Down Expand Up @@ -70,7 +90,7 @@ TranslationConsumer.toEnglish = function (text) {
/**
* Attempt to detect language of input text
* @param {String} text Text in an unknown language
* @returns {String} Abbreviation for name of language detected in text
* @returns {Language} Abbreviation for name of language detected in text
*/
TranslationConsumer.detectLanguage = function (text) {
let body = [{ "Text": text }];
Expand All @@ -93,20 +113,10 @@ TranslationConsumer.detectLanguage = function (text) {

/**
* Attempt to detect language of input text
* @returns {Array} List of languages supported by the translator
* @returns {Object} List of languages supported by the translator
*/
TranslationConsumer.getSupportedLanguages = function () {
return this._sendAnswer({
path: "languages",
queryString: "?api-version=3.0&scope=translation",
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": this.apiKey.value,
},
}, ".translation")
.catch((err) => {
throw err;
});
return SUPPORTED_LANGUAGES;
};

module.exports = TranslationConsumer;
Loading