Skip to content

Commit

Permalink
feat: add isLanguageSupported method (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Mar 20, 2021
1 parent e77e8ba commit eb39f93
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 10 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const getSupportedVoices = async () => {
* [`stop()`](#stop)
* [`getSupportedLanguages()`](#getsupportedlanguages)
* [`getSupportedVoices()`](#getsupportedvoices)
* [`isLanguageSupported(...)`](#islanguagesupported)
* [`openInstall()`](#openinstall)
* [Interfaces](#interfaces)

Expand Down Expand Up @@ -149,7 +150,7 @@ Stops the TTS engine.
getSupportedLanguages() => Promise<{ languages: string[]; }>
```

Returns a list of supported languages.
Returns a list of supported BCP 47 language tags.

**Returns:** <code>Promise&lt;{ languages: string[]; }&gt;</code>

Expand All @@ -169,6 +170,23 @@ Returns a list of supported voices.
--------------------


### isLanguageSupported(...)

```typescript
isLanguageSupported(options: { lang: string; }) => Promise<{ supported: boolean; }>
```

Checks if a specific BCP 47 language tag is supported.

| Param | Type |
| ------------- | ------------------------------ |
| **`options`** | <code>{ lang: string; }</code> |

**Returns:** <code>Promise&lt;{ supported: boolean; }&gt;</code>

--------------------


### openInstall()

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,9 @@ public boolean isAvailable() {
}

public boolean isLanguageSupported(String lang) {
Set<Locale> supportedLocales = tts.getAvailableLanguages();
if (supportedLocales.contains(Locale.forLanguageTag(lang))) {
return true;
}
return false;
Locale loc = Locale.forLanguageTag(lang);
int result = tts.isLanguageAvailable(loc);
return result == tts.LANG_AVAILABLE || result == tts.LANG_COUNTRY_AVAILABLE || result == tts.LANG_COUNTRY_VAR_AVAILABLE;
}

public void onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ public void getSupportedVoices(PluginCall call) {
}
}

@PluginMethod
public void isLanguageSupported(PluginCall call) {
String lang = call.getString("lang", "");
try {
boolean isLanguageSupported = implementation.isLanguageSupported(lang);
JSObject ret = new JSObject();
ret.put("supported", isLanguageSupported);
call.resolve(ret);
} catch (Exception ex) {
call.reject(ex.getLocalizedMessage());
}
}

@PluginMethod
public void openInstall(PluginCall call) {
try {
Expand Down
6 changes: 3 additions & 3 deletions ios/Plugin/TextToSpeech.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ import Capacitor
})
}

@objc public func isLanguageSupported(_ language: String) -> Bool {
let languages = self.getSupportedLanguages()
return languages.contains(language)
@objc public func isLanguageSupported(_ lang: String) -> Bool {
let voice = AVSpeechSynthesisVoice(language: lang)
return voice != nil
}

// Adjust rate for a closer match to other platform.
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/TextToSpeechPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
CAP_PLUGIN_METHOD(openInstall, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getSupportedLanguages, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getSupportedVoices, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isLanguageSupported, CAPPluginReturnPromise);
)
8 changes: 8 additions & 0 deletions ios/Plugin/TextToSpeechPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ public class TextToSpeechPlugin: CAPPlugin {
"voices": []
])
}

@objc func isLanguageSupported(_ call: CAPPluginCall) {
let lang = call.getString("lang") ?? ""
let isLanguageSupported = self.implementation.isLanguageSupported(lang)
call.resolve([
"supported": isLanguageSupported
])
}
}
8 changes: 7 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ export interface TextToSpeechPlugin {
*/
stop(): Promise<void>;
/**
* Returns a list of supported languages.
* Returns a list of supported BCP 47 language tags.
*/
getSupportedLanguages(): Promise<{ languages: string[] }>;
/**
* Returns a list of supported voices.
*/
getSupportedVoices(): Promise<{ voices: SpeechSynthesisVoice[] }>;
/**
* Checks if a specific BCP 47 language tag is supported.
*/
isLanguageSupported(options: {
lang: string;
}): Promise<{ supported: boolean }>;
/**
* Verifies proper installation and availability of resource files on the system.
*
Expand Down
8 changes: 8 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export class TextToSpeechWeb extends WebPlugin implements TextToSpeechPlugin {
return { voices };
}

public async isLanguageSupported(options: {
lang: string;
}): Promise<{ supported: boolean }> {
const result = await this.getSupportedLanguages();
const isLanguageSupported = result.languages.includes(options.lang);
return { supported: isLanguageSupported };
}

public async openInstall(): Promise<void> {
this.throwUnimplementedError();
}
Expand Down

0 comments on commit eb39f93

Please sign in to comment.