react-native-lingua 0.1.0

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.
package/src/index.tsx ADDED
@@ -0,0 +1,121 @@
1
+ import Lingua from './NativeLingua';
2
+
3
+ // Language Detector type
4
+ export type LanguageDetector = {
5
+ __nativeState: unknown;
6
+ };
7
+
8
+ // Confidence value result
9
+ export type LanguageConfidence = {
10
+ language: string;
11
+ confidence: number;
12
+ };
13
+
14
+ // Internal proxy that gets injected by JSI
15
+ type LinguaProxy = {
16
+ createDetectorForAllLanguages: () => LanguageDetector;
17
+ createDetectorForLanguages: (languages: string) => LanguageDetector;
18
+ detectLanguage: (detector: LanguageDetector, text: string) => string | null;
19
+ computeLanguageConfidence: (
20
+ detector: LanguageDetector,
21
+ text: string,
22
+ languageCode: string
23
+ ) => number;
24
+ computeLanguageConfidenceValues: (
25
+ detector: LanguageDetector,
26
+ text: string
27
+ ) => LanguageConfidence[];
28
+ };
29
+
30
+ declare global {
31
+ var __LinguaProxy: LinguaProxy | undefined;
32
+ }
33
+
34
+ // Install the JSI bindings
35
+ const errorMsg = Lingua.install();
36
+
37
+ if (errorMsg != null) {
38
+ console.error(`Lingua could not be installed: ${errorMsg}`);
39
+ }
40
+
41
+ const proxy = globalThis.__LinguaProxy;
42
+
43
+ if (proxy == null) {
44
+ console.error(
45
+ 'Lingua could not install JSI functions. Please check the native module implementation.'
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Creates a language detector for all supported languages
51
+ */
52
+ export function createDetectorForAllLanguages(): LanguageDetector {
53
+ if (!proxy) {
54
+ throw new Error('Lingua JSI proxy not available');
55
+ }
56
+ return proxy.createDetectorForAllLanguages();
57
+ }
58
+
59
+ /**
60
+ * Creates a language detector for specific languages
61
+ * @param languages Array of ISO 639-1 language codes (e.g., ['en', 'fr', 'de'])
62
+ */
63
+ export function createDetectorForLanguages(
64
+ languages: string[]
65
+ ): LanguageDetector {
66
+ if (!proxy) {
67
+ throw new Error('Lingua JSI proxy not available');
68
+ }
69
+ const langString = languages.join(',');
70
+ return proxy.createDetectorForLanguages(langString);
71
+ }
72
+
73
+ /**
74
+ * Detects the language of the given text
75
+ * @param detector Language detector instance
76
+ * @param text Text to analyze
77
+ * @returns ISO 639-1 language code (e.g., 'en') or null if detection failed
78
+ */
79
+ export function detectLanguage(
80
+ detector: LanguageDetector,
81
+ text: string
82
+ ): string | null {
83
+ if (!proxy) {
84
+ throw new Error('Lingua JSI proxy not available');
85
+ }
86
+ return proxy.detectLanguage(detector, text);
87
+ }
88
+
89
+ /**
90
+ * Computes the confidence value for a specific language
91
+ * @param detector Language detector instance
92
+ * @param text Text to analyze
93
+ * @param languageCode ISO 639-1 language code (e.g., 'en')
94
+ * @returns Confidence value between 0.0 and 1.0
95
+ */
96
+ export function computeLanguageConfidence(
97
+ detector: LanguageDetector,
98
+ text: string,
99
+ languageCode: string
100
+ ): number {
101
+ if (!proxy) {
102
+ throw new Error('Lingua JSI proxy not available');
103
+ }
104
+ return proxy.computeLanguageConfidence(detector, text, languageCode);
105
+ }
106
+
107
+ /**
108
+ * Computes confidence values for all languages
109
+ * @param detector Language detector instance
110
+ * @param text Text to analyze
111
+ * @returns Array of language confidence values, sorted by confidence (descending)
112
+ */
113
+ export function computeLanguageConfidenceValues(
114
+ detector: LanguageDetector,
115
+ text: string
116
+ ): LanguageConfidence[] {
117
+ if (!proxy) {
118
+ throw new Error('Lingua JSI proxy not available');
119
+ }
120
+ return proxy.computeLanguageConfidenceValues(detector, text);
121
+ }