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/LICENSE +20 -0
- package/Lingua.podspec +21 -0
- package/README.md +254 -0
- package/android/CMakeLists.txt +55 -0
- package/android/build.gradle +94 -0
- package/android/cpp-adapter.cpp +36 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/lingua/JNIOnLoad.kt +20 -0
- package/android/src/main/java/com/lingua/LinguaModule.kt +51 -0
- package/android/src/main/java/com/lingua/LinguaPackage.kt +33 -0
- package/android/src/main/jni/include/liblingua.h +65 -0
- package/cpp/lingua.cpp +184 -0
- package/cpp/lingua.h +12 -0
- package/cpp/macros.h +15 -0
- package/ios/Lingua.h +5 -0
- package/ios/Lingua.mm +40 -0
- package/lib/module/NativeLingua.js +5 -0
- package/lib/module/NativeLingua.js.map +1 -0
- package/lib/module/index.js +82 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeLingua.d.ts +7 -0
- package/lib/typescript/src/NativeLingua.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +50 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +164 -0
- package/src/NativeLingua.ts +7 -0
- package/src/index.tsx +121 -0
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
|
+
}
|