11import { Translate } from "@google-cloud/translate/build/src/v2" ;
22
3- import { JsonTranslateExecutor } from "./internal/JsonTranslateExecutor " ;
3+ import { JsonTranslateComposer } from "./internal/JsonTranslateComposer " ;
44
55/**
66 * JSON Translator.
@@ -41,8 +41,8 @@ export class JsonTranslator {
4141 * @returns The translated JSON data.
4242 */
4343 public async translate < T > ( props : JsonTranslator . IProps < T > ) : Promise < T > {
44- const collection : JsonTranslateExecutor . ICollection =
45- JsonTranslateExecutor . prepare ( props ) ;
44+ const collection : JsonTranslateComposer . ICollection =
45+ JsonTranslateComposer . composeCollection ( props ) ;
4646 const translated : string [ ] = [ ] ;
4747 const from : string | undefined =
4848 props . source === null
@@ -100,20 +100,41 @@ export class JsonTranslator {
100100 * through the Google Translate API, with the similar properties like the
101101 * {@link JsonTranslator.translate} method.
102102 *
103- * Therefore, if you want to filter some specific values to participate in
104- * the language detection, fill the {@link JsonTranslator.IProps .filter}
103+ * Therefore, if you want to filter out some specific values to participate in
104+ * the language detection, fill the {@link JsonTranslator.IDetectProps .filter}
105105 * function.
106106 *
107107 * @param input Properties for language detection.
108108 * @returns The detected language or `undefined` if the language is unknown.
109109 */
110110 public async detect < T > (
111- props : Omit < JsonTranslator . IProps < T > , "source" | "target" > ,
111+ props : JsonTranslator . IDetectProps < T > ,
112112 ) : Promise < string | undefined > {
113- const texts : string [ ] = JsonTranslateExecutor . getTexts ( props ) ;
113+ const texts : string [ ] = JsonTranslateComposer . composeTexts ( props ) ;
114114 return this . _Detect_language ( texts ) ;
115115 }
116116
117+ /**
118+ * Compose dictionary from translated.
119+ *
120+ * Compose dictionary between original JSON input data and its translated
121+ * output data. The dictionary is a key-value pair object containing the
122+ * original value and its translated value.
123+ *
124+ * If you've composed {@link JsonTranslator.IProps.filter} function in
125+ * the {@link JsonTranslator.translate} method, don't forget to re-apply
126+ * the filter function to the {@link JsonTranslator.IDictionaryProps.filter}
127+ * property.
128+ *
129+ * @param props Properties for the dictionary composition.
130+ * @returns Composed dictionary
131+ */
132+ public dictionary < T > (
133+ props : JsonTranslator . IDictionaryProps < T > ,
134+ ) : Record < string , string > {
135+ return JsonTranslateComposer . composeDictionary ( props ) ;
136+ }
137+
117138 /**
118139 * @internal
119140 */
@@ -130,6 +151,8 @@ export class JsonTranslator {
130151export namespace JsonTranslator {
131152 /**
132153 * Properties for the translation.
154+ *
155+ * Keyworded properties used in the {@link JsonTranslator.translate} method.
133156 */
134157 export interface IProps < T > {
135158 /**
@@ -177,6 +200,61 @@ export namespace JsonTranslator {
177200 dictionary ?: Record < string , string > | null | undefined ;
178201 }
179202
203+ /**
204+ * Properties for the language detection.
205+ *
206+ * Keyworded properties used in the {@link JsonTranslator.detect} method.
207+ */
208+ export interface IDetectProps < T > {
209+ /**
210+ * The JSON input data to detect language.
211+ */
212+ input : T ;
213+
214+ /**
215+ * Filter function specifying which data to translate.
216+ *
217+ * @param explore Information about the data to explore.
218+ * @returns `true` if the data should be translated; otherwise, `false`.
219+ */
220+ filter ?: ( ( explore : IExplore ) => boolean ) | null | undefined ;
221+
222+ /**
223+ * Reserved dictionary of pre-translated values.
224+ *
225+ * The dictionary is a key-value pair object containing the pre-translated
226+ * values. The key means the original value, and the value means the
227+ * pre-translated value.
228+ *
229+ * If this dictionary has been configured and a JSON input value matches to
230+ * the dictionary's key, the dictionary's value would be used instead of
231+ * calling the Google Translate API.
232+ */
233+ dictionary ?: Record < string , string > | null | undefined ;
234+ }
235+
236+ /**
237+ * Properties for the dictionary composition.
238+ *
239+ * Keyworded properties used in nthe {@link JsonTranslator.dictionary} method.
240+ */
241+ export interface IDictionaryProps < T > {
242+ /**
243+ * Input JSON, the original data.
244+ */
245+ input : T ;
246+
247+ /**
248+ * Output JSON, the translated data.
249+ */
250+ output : T ;
251+
252+ /**
253+ * Filter function specifying which data be translated.
254+ */
255+ filter ?: ( ( explore : IExplore ) => boolean ) | null | undefined ;
256+ }
257+
180258 /**
181259 * Exploration information used in the {@link IProps.filter} function.
182260 */
@@ -214,8 +292,15 @@ export namespace JsonTranslator {
214292 }
215293}
216294
295+ /**
296+ * @internal
297+ */
217298interface IPiece {
218299 text : string ;
219300 length : number ;
220301}
302+
303+ /**
304+ * @internal
305+ */
221306const SEPARATOR = " //|-0-|\\ " ;
0 commit comments