react-native-kookit 0.4.0 → 0.4.2

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.
@@ -847,11 +847,35 @@ class ReactNativeKookitModule : Module() {
847
847
  latch.await()
848
848
  }
849
849
 
850
- // Set language
851
- val locale = parseLocale(language)
852
- val result = textToSpeech?.setLanguage(locale)
853
- if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
854
- throw Exception("Language not supported: $language")
850
+ // Set language with fallback for devices (e.g. OPPO ColorOS) that
851
+ // reject certain locale variants like "zh-CN" but accept "zh".
852
+ fun trySetLanguage(tts: TextToSpeech, loc: Locale): Int =
853
+ tts.setLanguage(loc)
854
+
855
+ val primaryLocale = parseLocale(language)
856
+ val fallbackLocales: List<Locale> = buildList {
857
+ add(primaryLocale)
858
+ // BCP-47 tag (handles script variants such as zh-Hans-CN)
859
+ add(Locale.forLanguageTag(language))
860
+ // language-only, e.g. Locale("zh") for "zh-CN"
861
+ if (primaryLocale.country.isNotEmpty()) add(Locale(primaryLocale.language))
862
+ // device default as last resort
863
+ add(Locale.getDefault())
864
+ }
865
+
866
+ var langSetResult = TextToSpeech.LANG_NOT_SUPPORTED
867
+ for (loc in fallbackLocales) {
868
+ val r = textToSpeech?.let { trySetLanguage(it, loc) }
869
+ ?: TextToSpeech.LANG_NOT_SUPPORTED
870
+ if (r != TextToSpeech.LANG_MISSING_DATA && r != TextToSpeech.LANG_NOT_SUPPORTED) {
871
+ langSetResult = r
872
+ break
873
+ }
874
+ }
875
+ if (langSetResult == TextToSpeech.LANG_MISSING_DATA || langSetResult == TextToSpeech.LANG_NOT_SUPPORTED) {
876
+ // Last resort: proceed anyway; some OEM TTS engines ignore setLanguage
877
+ // but still synthesize correctly when a voice is set via voiceId.
878
+ android.util.Log.w("ReactNativeKookit", "Language not supported by TTS engine: $language, proceeding anyway")
855
879
  }
856
880
 
857
881
  // Set voice if specified
@@ -1059,9 +1083,15 @@ class ReactNativeKookitModule : Module() {
1059
1083
  }
1060
1084
 
1061
1085
  /**
1062
- * Parse locale string (e.g., "en-US", "zh-CN") to Locale object
1086
+ * Parse locale string (e.g., "en-US", "zh-CN") to Locale object.
1087
+ * Uses Locale.forLanguageTag first (proper BCP-47 support), then falls back
1088
+ * to the manual split approach for older-style identifiers.
1063
1089
  */
1064
1090
  private fun parseLocale(localeString: String): Locale {
1091
+ // Locale.forLanguageTag handles "zh-CN", "zh-Hans-CN", etc. correctly.
1092
+ val tag = Locale.forLanguageTag(localeString)
1093
+ if (tag.language.isNotEmpty()) return tag
1094
+ // Fallback for underscore-separated identifiers (e.g. "zh_CN")
1065
1095
  val parts = localeString.split("-", "_")
1066
1096
  return when (parts.size) {
1067
1097
  1 -> Locale(parts[0])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-kookit",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "React Native module for intercepting volume button presses on iOS and Android, with FTP client functionality",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",