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/cpp/lingua.cpp
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
#include "lingua.h"
|
|
2
|
+
#include "macros.h"
|
|
3
|
+
#include <liblingua.h>
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <vector>
|
|
6
|
+
|
|
7
|
+
namespace lingua {
|
|
8
|
+
namespace jsi = facebook::jsi;
|
|
9
|
+
namespace react = facebook::react;
|
|
10
|
+
|
|
11
|
+
// Wrapper class for LinguaDetector to manage as NativeState
|
|
12
|
+
class JSI_EXPORT LinguaDetectorWrapper : public jsi::NativeState {
|
|
13
|
+
public:
|
|
14
|
+
LinguaDetectorWrapper(void *detector) : detector_ptr(detector) {}
|
|
15
|
+
|
|
16
|
+
~LinguaDetectorWrapper() {
|
|
17
|
+
if (detector_ptr != nullptr) {
|
|
18
|
+
lingua_detector_destroy(static_cast<LinguaDetector *>(detector_ptr));
|
|
19
|
+
detector_ptr = nullptr;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
void *detector_ptr;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
void install(jsi::Runtime &rt, std::shared_ptr<react::CallInvoker> invoker) {
|
|
27
|
+
jsi::Object linguaModule = jsi::Object(rt);
|
|
28
|
+
|
|
29
|
+
// createDetectorForAllLanguages
|
|
30
|
+
auto createDetectorForAllLanguages =
|
|
31
|
+
HOST_STATIC_FN("createDetectorForAllLanguages") {
|
|
32
|
+
void *detector = lingua_detector_create_all();
|
|
33
|
+
|
|
34
|
+
if (detector == nullptr) {
|
|
35
|
+
throw jsi::JSError(rt, "Failed to create language detector");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
auto wrapper = std::make_shared<LinguaDetectorWrapper>(detector);
|
|
39
|
+
auto detectorObj = jsi::Object(rt);
|
|
40
|
+
detectorObj.setNativeState(rt, wrapper);
|
|
41
|
+
|
|
42
|
+
return detectorObj;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// createDetectorForLanguages
|
|
46
|
+
auto createDetectorForLanguages =
|
|
47
|
+
HOST_STATIC_FN("createDetectorForLanguages") {
|
|
48
|
+
if (count < 1 || !args[0].isString()) {
|
|
49
|
+
throw jsi::JSError(rt, "Language codes string required");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
std::string langCodes = args[0].asString(rt).utf8(rt);
|
|
53
|
+
const char *error = nullptr;
|
|
54
|
+
|
|
55
|
+
void *detector =
|
|
56
|
+
lingua_detector_create_from_languages(langCodes.c_str(), &error);
|
|
57
|
+
|
|
58
|
+
if (detector == nullptr) {
|
|
59
|
+
std::string errorMsg = "Failed to create detector";
|
|
60
|
+
if (error != nullptr) {
|
|
61
|
+
errorMsg += ": " + std::string(error);
|
|
62
|
+
}
|
|
63
|
+
throw jsi::JSError(rt, errorMsg);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
auto wrapper = std::make_shared<LinguaDetectorWrapper>(detector);
|
|
67
|
+
auto detectorObj = jsi::Object(rt);
|
|
68
|
+
detectorObj.setNativeState(rt, wrapper);
|
|
69
|
+
|
|
70
|
+
return detectorObj;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// detectLanguage
|
|
74
|
+
auto detectLanguage = HOST_STATIC_FN("detectLanguage") {
|
|
75
|
+
if (count < 2 || !args[0].isObject() || !args[1].isString()) {
|
|
76
|
+
throw jsi::JSError(
|
|
77
|
+
rt, "Invalid arguments: detector object and text string required");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
auto detectorObj = args[0].asObject(rt);
|
|
81
|
+
auto wrapper = detectorObj.getNativeState<LinguaDetectorWrapper>(rt);
|
|
82
|
+
|
|
83
|
+
if (wrapper == nullptr || wrapper->detector_ptr == nullptr) {
|
|
84
|
+
throw jsi::JSError(rt, "Invalid detector");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
std::string text = args[1].asString(rt).utf8(rt);
|
|
88
|
+
const char *error = nullptr;
|
|
89
|
+
|
|
90
|
+
char *result = lingua_detect_language(
|
|
91
|
+
static_cast<LinguaDetector *>(wrapper->detector_ptr), text.c_str(),
|
|
92
|
+
&error);
|
|
93
|
+
|
|
94
|
+
if (result == nullptr) {
|
|
95
|
+
return jsi::Value::null();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
std::string langCode(result);
|
|
99
|
+
lingua_free_string(result);
|
|
100
|
+
|
|
101
|
+
return jsi::String::createFromUtf8(rt, langCode);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// computeLanguageConfidence
|
|
105
|
+
auto computeLanguageConfidence = HOST_STATIC_FN("computeLanguageConfidence") {
|
|
106
|
+
if (count < 3 || !args[0].isObject() || !args[1].isString() ||
|
|
107
|
+
!args[2].isString()) {
|
|
108
|
+
throw jsi::JSError(
|
|
109
|
+
rt, "Invalid arguments: detector, text, and language code required");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
auto detectorObj = args[0].asObject(rt);
|
|
113
|
+
auto wrapper = detectorObj.getNativeState<LinguaDetectorWrapper>(rt);
|
|
114
|
+
|
|
115
|
+
if (wrapper == nullptr || wrapper->detector_ptr == nullptr) {
|
|
116
|
+
throw jsi::JSError(rt, "Invalid detector");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
std::string text = args[1].asString(rt).utf8(rt);
|
|
120
|
+
std::string langCode = args[2].asString(rt).utf8(rt);
|
|
121
|
+
const char *error = nullptr;
|
|
122
|
+
|
|
123
|
+
double confidence = lingua_compute_language_confidence(
|
|
124
|
+
static_cast<LinguaDetector *>(wrapper->detector_ptr), text.c_str(),
|
|
125
|
+
langCode.c_str(), &error);
|
|
126
|
+
|
|
127
|
+
return jsi::Value(confidence);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// computeLanguageConfidenceValues
|
|
131
|
+
auto computeLanguageConfidenceValues =
|
|
132
|
+
HOST_STATIC_FN("computeLanguageConfidenceValues") {
|
|
133
|
+
if (count < 2 || !args[0].isObject() || !args[1].isString()) {
|
|
134
|
+
throw jsi::JSError(rt, "Invalid arguments: detector and text required");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
auto detectorObj = args[0].asObject(rt);
|
|
138
|
+
auto wrapper = detectorObj.getNativeState<LinguaDetectorWrapper>(rt);
|
|
139
|
+
|
|
140
|
+
if (wrapper == nullptr || wrapper->detector_ptr == nullptr) {
|
|
141
|
+
throw jsi::JSError(rt, "Invalid detector");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
std::string text = args[1].asString(rt).utf8(rt);
|
|
145
|
+
const char *error = nullptr;
|
|
146
|
+
int count_val = 0;
|
|
147
|
+
|
|
148
|
+
ConfidenceValue *values = lingua_compute_language_confidence_values(
|
|
149
|
+
static_cast<LinguaDetector *>(wrapper->detector_ptr), text.c_str(),
|
|
150
|
+
&count_val, &error);
|
|
151
|
+
|
|
152
|
+
if (values == nullptr || count_val == 0) {
|
|
153
|
+
return jsi::Array(rt, 0);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
jsi::Array result(rt, count_val);
|
|
157
|
+
|
|
158
|
+
for (int i = 0; i < count_val; i++) {
|
|
159
|
+
jsi::Object obj(rt);
|
|
160
|
+
obj.setProperty(rt, "language",
|
|
161
|
+
jsi::String::createFromUtf8(rt, values[i].language_code));
|
|
162
|
+
obj.setProperty(rt, "confidence", jsi::Value(values[i].confidence));
|
|
163
|
+
result.setValueAtIndex(rt, i, std::move(obj));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
lingua_free_confidence_values(values, count_val);
|
|
167
|
+
|
|
168
|
+
return result;
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
linguaModule.setProperty(rt, "createDetectorForAllLanguages",
|
|
172
|
+
std::move(createDetectorForAllLanguages));
|
|
173
|
+
linguaModule.setProperty(rt, "createDetectorForLanguages",
|
|
174
|
+
std::move(createDetectorForLanguages));
|
|
175
|
+
linguaModule.setProperty(rt, "detectLanguage", std::move(detectLanguage));
|
|
176
|
+
linguaModule.setProperty(rt, "computeLanguageConfidence",
|
|
177
|
+
std::move(computeLanguageConfidence));
|
|
178
|
+
linguaModule.setProperty(rt, "computeLanguageConfidenceValues",
|
|
179
|
+
std::move(computeLanguageConfidenceValues));
|
|
180
|
+
|
|
181
|
+
rt.global().setProperty(rt, "__LinguaProxy", std::move(linguaModule));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
} // namespace lingua
|
package/cpp/lingua.h
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <ReactCommon/CallInvoker.h>
|
|
4
|
+
#include <jsi/jsi.h>
|
|
5
|
+
|
|
6
|
+
namespace lingua {
|
|
7
|
+
namespace jsi = facebook::jsi;
|
|
8
|
+
namespace react = facebook::react;
|
|
9
|
+
|
|
10
|
+
void install(jsi::Runtime &runtime,
|
|
11
|
+
std::shared_ptr<react::CallInvoker> invoker);
|
|
12
|
+
} // namespace lingua
|
package/cpp/macros.h
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#define HOST_FN(name) \
|
|
4
|
+
jsi::Function::createFromHostFunction( \
|
|
5
|
+
rt, \
|
|
6
|
+
jsi::PropNameID::forAscii(rt, name), \
|
|
7
|
+
0, \
|
|
8
|
+
[=, this](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value
|
|
9
|
+
|
|
10
|
+
#define HOST_STATIC_FN(name) \
|
|
11
|
+
jsi::Function::createFromHostFunction( \
|
|
12
|
+
rt, \
|
|
13
|
+
jsi::PropNameID::forAscii(rt, name), \
|
|
14
|
+
0, \
|
|
15
|
+
[=](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value
|
package/ios/Lingua.h
ADDED
package/ios/Lingua.mm
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#import "Lingua.h"
|
|
2
|
+
#import "../cpp/lingua.h"
|
|
3
|
+
#import <ReactCommon/CallInvoker.h>
|
|
4
|
+
#import <ReactCommon/RCTTurboModuleWithJSIBindings.h>
|
|
5
|
+
|
|
6
|
+
using namespace facebook;
|
|
7
|
+
|
|
8
|
+
@implementation Lingua {
|
|
9
|
+
bool _didInstall;
|
|
10
|
+
std::weak_ptr<react::CallInvoker> _callInvoker;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
RCT_EXPORT_MODULE()
|
|
14
|
+
|
|
15
|
+
- (void)installJSIBindingsWithRuntime:(jsi::Runtime &)runtime {
|
|
16
|
+
auto callInvoker = _callInvoker.lock();
|
|
17
|
+
if (callInvoker == nullptr) {
|
|
18
|
+
throw std::runtime_error("CallInvoker is missing");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
lingua::install(runtime, callInvoker);
|
|
22
|
+
_didInstall = true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// This function triggers the initialization of the turbo module
|
|
26
|
+
- (NSString *)install {
|
|
27
|
+
if (_didInstall) {
|
|
28
|
+
return nil;
|
|
29
|
+
} else {
|
|
30
|
+
return @"JSI Bindings could not be installed!";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
35
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params {
|
|
36
|
+
_callInvoker = params.jsInvoker;
|
|
37
|
+
return std::make_shared<facebook::react::NativeLinguaSpecJSI>(params);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeLingua.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAA0B,cAAc;AAMpE,eAAeA,mBAAmB,CAACC,YAAY,CAAO,QAAQ,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import Lingua from "./NativeLingua.js";
|
|
4
|
+
|
|
5
|
+
// Language Detector type
|
|
6
|
+
|
|
7
|
+
// Confidence value result
|
|
8
|
+
|
|
9
|
+
// Internal proxy that gets injected by JSI
|
|
10
|
+
|
|
11
|
+
// Install the JSI bindings
|
|
12
|
+
const errorMsg = Lingua.install();
|
|
13
|
+
if (errorMsg != null) {
|
|
14
|
+
console.error(`Lingua could not be installed: ${errorMsg}`);
|
|
15
|
+
}
|
|
16
|
+
const proxy = globalThis.__LinguaProxy;
|
|
17
|
+
if (proxy == null) {
|
|
18
|
+
console.error('Lingua could not install JSI functions. Please check the native module implementation.');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a language detector for all supported languages
|
|
23
|
+
*/
|
|
24
|
+
export function createDetectorForAllLanguages() {
|
|
25
|
+
if (!proxy) {
|
|
26
|
+
throw new Error('Lingua JSI proxy not available');
|
|
27
|
+
}
|
|
28
|
+
return proxy.createDetectorForAllLanguages();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Creates a language detector for specific languages
|
|
33
|
+
* @param languages Array of ISO 639-1 language codes (e.g., ['en', 'fr', 'de'])
|
|
34
|
+
*/
|
|
35
|
+
export function createDetectorForLanguages(languages) {
|
|
36
|
+
if (!proxy) {
|
|
37
|
+
throw new Error('Lingua JSI proxy not available');
|
|
38
|
+
}
|
|
39
|
+
const langString = languages.join(',');
|
|
40
|
+
return proxy.createDetectorForLanguages(langString);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Detects the language of the given text
|
|
45
|
+
* @param detector Language detector instance
|
|
46
|
+
* @param text Text to analyze
|
|
47
|
+
* @returns ISO 639-1 language code (e.g., 'en') or null if detection failed
|
|
48
|
+
*/
|
|
49
|
+
export function detectLanguage(detector, text) {
|
|
50
|
+
if (!proxy) {
|
|
51
|
+
throw new Error('Lingua JSI proxy not available');
|
|
52
|
+
}
|
|
53
|
+
return proxy.detectLanguage(detector, text);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Computes the confidence value for a specific language
|
|
58
|
+
* @param detector Language detector instance
|
|
59
|
+
* @param text Text to analyze
|
|
60
|
+
* @param languageCode ISO 639-1 language code (e.g., 'en')
|
|
61
|
+
* @returns Confidence value between 0.0 and 1.0
|
|
62
|
+
*/
|
|
63
|
+
export function computeLanguageConfidence(detector, text, languageCode) {
|
|
64
|
+
if (!proxy) {
|
|
65
|
+
throw new Error('Lingua JSI proxy not available');
|
|
66
|
+
}
|
|
67
|
+
return proxy.computeLanguageConfidence(detector, text, languageCode);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Computes confidence values for all languages
|
|
72
|
+
* @param detector Language detector instance
|
|
73
|
+
* @param text Text to analyze
|
|
74
|
+
* @returns Array of language confidence values, sorted by confidence (descending)
|
|
75
|
+
*/
|
|
76
|
+
export function computeLanguageConfidenceValues(detector, text) {
|
|
77
|
+
if (!proxy) {
|
|
78
|
+
throw new Error('Lingua JSI proxy not available');
|
|
79
|
+
}
|
|
80
|
+
return proxy.computeLanguageConfidenceValues(detector, text);
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Lingua","errorMsg","install","console","error","proxy","globalThis","__LinguaProxy","createDetectorForAllLanguages","Error","createDetectorForLanguages","languages","langString","join","detectLanguage","detector","text","computeLanguageConfidence","languageCode","computeLanguageConfidenceValues"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,OAAOA,MAAM,MAAM,mBAAgB;;AAEnC;;AAKA;;AAMA;;AAoBA;AACA,MAAMC,QAAQ,GAAGD,MAAM,CAACE,OAAO,CAAC,CAAC;AAEjC,IAAID,QAAQ,IAAI,IAAI,EAAE;EACpBE,OAAO,CAACC,KAAK,CAAC,kCAAkCH,QAAQ,EAAE,CAAC;AAC7D;AAEA,MAAMI,KAAK,GAAGC,UAAU,CAACC,aAAa;AAEtC,IAAIF,KAAK,IAAI,IAAI,EAAE;EACjBF,OAAO,CAACC,KAAK,CACX,wFACF,CAAC;AACH;;AAEA;AACA;AACA;AACA,OAAO,SAASI,6BAA6BA,CAAA,EAAqB;EAChE,IAAI,CAACH,KAAK,EAAE;IACV,MAAM,IAAII,KAAK,CAAC,gCAAgC,CAAC;EACnD;EACA,OAAOJ,KAAK,CAACG,6BAA6B,CAAC,CAAC;AAC9C;;AAEA;AACA;AACA;AACA;AACA,OAAO,SAASE,0BAA0BA,CACxCC,SAAmB,EACD;EAClB,IAAI,CAACN,KAAK,EAAE;IACV,MAAM,IAAII,KAAK,CAAC,gCAAgC,CAAC;EACnD;EACA,MAAMG,UAAU,GAAGD,SAAS,CAACE,IAAI,CAAC,GAAG,CAAC;EACtC,OAAOR,KAAK,CAACK,0BAA0B,CAACE,UAAU,CAAC;AACrD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASE,cAAcA,CAC5BC,QAA0B,EAC1BC,IAAY,EACG;EACf,IAAI,CAACX,KAAK,EAAE;IACV,MAAM,IAAII,KAAK,CAAC,gCAAgC,CAAC;EACnD;EACA,OAAOJ,KAAK,CAACS,cAAc,CAACC,QAAQ,EAAEC,IAAI,CAAC;AAC7C;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,yBAAyBA,CACvCF,QAA0B,EAC1BC,IAAY,EACZE,YAAoB,EACZ;EACR,IAAI,CAACb,KAAK,EAAE;IACV,MAAM,IAAII,KAAK,CAAC,gCAAgC,CAAC;EACnD;EACA,OAAOJ,KAAK,CAACY,yBAAyB,CAACF,QAAQ,EAAEC,IAAI,EAAEE,YAAY,CAAC;AACtE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,+BAA+BA,CAC7CJ,QAA0B,EAC1BC,IAAY,EACU;EACtB,IAAI,CAACX,KAAK,EAAE;IACV,MAAM,IAAII,KAAK,CAAC,gCAAgC,CAAC;EACnD;EACA,OAAOJ,KAAK,CAACc,+BAA+B,CAACJ,QAAQ,EAAEC,IAAI,CAAC;AAC9D","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeLingua.d.ts","sourceRoot":"","sources":["../../../src/NativeLingua.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAErE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,OAAO,IAAI,MAAM,GAAG,SAAS,CAAC;CAC/B;;AAED,wBAAgE"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type LanguageDetector = {
|
|
2
|
+
__nativeState: unknown;
|
|
3
|
+
};
|
|
4
|
+
export type LanguageConfidence = {
|
|
5
|
+
language: string;
|
|
6
|
+
confidence: number;
|
|
7
|
+
};
|
|
8
|
+
type LinguaProxy = {
|
|
9
|
+
createDetectorForAllLanguages: () => LanguageDetector;
|
|
10
|
+
createDetectorForLanguages: (languages: string) => LanguageDetector;
|
|
11
|
+
detectLanguage: (detector: LanguageDetector, text: string) => string | null;
|
|
12
|
+
computeLanguageConfidence: (detector: LanguageDetector, text: string, languageCode: string) => number;
|
|
13
|
+
computeLanguageConfidenceValues: (detector: LanguageDetector, text: string) => LanguageConfidence[];
|
|
14
|
+
};
|
|
15
|
+
declare global {
|
|
16
|
+
var __LinguaProxy: LinguaProxy | undefined;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a language detector for all supported languages
|
|
20
|
+
*/
|
|
21
|
+
export declare function createDetectorForAllLanguages(): LanguageDetector;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a language detector for specific languages
|
|
24
|
+
* @param languages Array of ISO 639-1 language codes (e.g., ['en', 'fr', 'de'])
|
|
25
|
+
*/
|
|
26
|
+
export declare function createDetectorForLanguages(languages: string[]): LanguageDetector;
|
|
27
|
+
/**
|
|
28
|
+
* Detects the language of the given text
|
|
29
|
+
* @param detector Language detector instance
|
|
30
|
+
* @param text Text to analyze
|
|
31
|
+
* @returns ISO 639-1 language code (e.g., 'en') or null if detection failed
|
|
32
|
+
*/
|
|
33
|
+
export declare function detectLanguage(detector: LanguageDetector, text: string): string | null;
|
|
34
|
+
/**
|
|
35
|
+
* Computes the confidence value for a specific language
|
|
36
|
+
* @param detector Language detector instance
|
|
37
|
+
* @param text Text to analyze
|
|
38
|
+
* @param languageCode ISO 639-1 language code (e.g., 'en')
|
|
39
|
+
* @returns Confidence value between 0.0 and 1.0
|
|
40
|
+
*/
|
|
41
|
+
export declare function computeLanguageConfidence(detector: LanguageDetector, text: string, languageCode: string): number;
|
|
42
|
+
/**
|
|
43
|
+
* Computes confidence values for all languages
|
|
44
|
+
* @param detector Language detector instance
|
|
45
|
+
* @param text Text to analyze
|
|
46
|
+
* @returns Array of language confidence values, sorted by confidence (descending)
|
|
47
|
+
*/
|
|
48
|
+
export declare function computeLanguageConfidenceValues(detector: LanguageDetector, text: string): LanguageConfidence[];
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAGA,MAAM,MAAM,gBAAgB,GAAG;IAC7B,aAAa,EAAE,OAAO,CAAC;CACxB,CAAC;AAGF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAGF,KAAK,WAAW,GAAG;IACjB,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;IACtD,0BAA0B,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,gBAAgB,CAAC;IACpE,cAAc,EAAE,CAAC,QAAQ,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC;IAC5E,yBAAyB,EAAE,CACzB,QAAQ,EAAE,gBAAgB,EAC1B,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,KACjB,MAAM,CAAC;IACZ,+BAA+B,EAAE,CAC/B,QAAQ,EAAE,gBAAgB,EAC1B,IAAI,EAAE,MAAM,KACT,kBAAkB,EAAE,CAAC;CAC3B,CAAC;AAEF,OAAO,CAAC,MAAM,CAAC;IACb,IAAI,aAAa,EAAE,WAAW,GAAG,SAAS,CAAC;CAC5C;AAiBD;;GAEG;AACH,wBAAgB,6BAA6B,IAAI,gBAAgB,CAKhE;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,MAAM,EAAE,GAClB,gBAAgB,CAMlB;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,gBAAgB,EAC1B,IAAI,EAAE,MAAM,GACX,MAAM,GAAG,IAAI,CAKf;AAED;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CACvC,QAAQ,EAAE,gBAAgB,EAC1B,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,GACnB,MAAM,CAKR;AAED;;;;;GAKG;AACH,wBAAgB,+BAA+B,CAC7C,QAAQ,EAAE,gBAAgB,EAC1B,IAAI,EAAE,MAAM,GACX,kBAAkB,EAAE,CAKtB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-native-lingua",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A lingua-rs wrapper for React Native",
|
|
5
|
+
"main": "./lib/module/index.js",
|
|
6
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"source": "./src/index.tsx",
|
|
10
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
11
|
+
"default": "./lib/module/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"lib",
|
|
18
|
+
"android",
|
|
19
|
+
"ios",
|
|
20
|
+
"cpp",
|
|
21
|
+
"*.podspec",
|
|
22
|
+
"react-native.config.js",
|
|
23
|
+
"!ios/build",
|
|
24
|
+
"!android/build",
|
|
25
|
+
"!android/gradle",
|
|
26
|
+
"!android/gradlew",
|
|
27
|
+
"!android/gradlew.bat",
|
|
28
|
+
"!android/local.properties",
|
|
29
|
+
"!**/__tests__",
|
|
30
|
+
"!**/__fixtures__",
|
|
31
|
+
"!**/__mocks__",
|
|
32
|
+
"!**/.*"
|
|
33
|
+
],
|
|
34
|
+
"workspaces": [
|
|
35
|
+
"example"
|
|
36
|
+
],
|
|
37
|
+
"keywords": [
|
|
38
|
+
"react-native",
|
|
39
|
+
"ios",
|
|
40
|
+
"android"
|
|
41
|
+
],
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/Hkmu/react-native-lingua/.git"
|
|
45
|
+
},
|
|
46
|
+
"author": "hej <jiehecd@gmail.com> (https://github.com/Hkmu/)",
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/Hkmu/react-native-lingua//issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/Hkmu/react-native-lingua/#readme",
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
54
|
+
"@eslint/compat": "^1.3.2",
|
|
55
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
56
|
+
"@eslint/js": "^9.35.0",
|
|
57
|
+
"@evilmartians/lefthook": "^1.12.3",
|
|
58
|
+
"@react-native-community/cli": "20.0.0",
|
|
59
|
+
"@react-native/babel-preset": "0.82.1",
|
|
60
|
+
"@react-native/eslint-config": "^0.82.1",
|
|
61
|
+
"@release-it/conventional-changelog": "^10.0.1",
|
|
62
|
+
"@types/jest": "^29.5.14",
|
|
63
|
+
"@types/react": "^19.1.1",
|
|
64
|
+
"commitlint": "^19.8.1",
|
|
65
|
+
"del-cli": "^6.0.0",
|
|
66
|
+
"eslint": "^9.35.0",
|
|
67
|
+
"eslint-config-prettier": "^10.1.8",
|
|
68
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
69
|
+
"jest": "^29.7.0",
|
|
70
|
+
"prettier": "^3.6.2",
|
|
71
|
+
"react": "19.1.1",
|
|
72
|
+
"react-native": "0.82.1",
|
|
73
|
+
"react-native-builder-bob": "^0.40.13",
|
|
74
|
+
"release-it": "^19.0.4",
|
|
75
|
+
"turbo": "^2.5.6",
|
|
76
|
+
"typescript": "^5.9.2"
|
|
77
|
+
},
|
|
78
|
+
"peerDependencies": {
|
|
79
|
+
"react": "*",
|
|
80
|
+
"react-native": "*"
|
|
81
|
+
},
|
|
82
|
+
"jest": {
|
|
83
|
+
"preset": "react-native",
|
|
84
|
+
"modulePathIgnorePatterns": [
|
|
85
|
+
"<rootDir>/example/node_modules",
|
|
86
|
+
"<rootDir>/lib/"
|
|
87
|
+
]
|
|
88
|
+
},
|
|
89
|
+
"commitlint": {
|
|
90
|
+
"extends": [
|
|
91
|
+
"@commitlint/config-conventional"
|
|
92
|
+
]
|
|
93
|
+
},
|
|
94
|
+
"release-it": {
|
|
95
|
+
"git": {
|
|
96
|
+
"commitMessage": "chore: release ${version}",
|
|
97
|
+
"tagName": "v${version}"
|
|
98
|
+
},
|
|
99
|
+
"npm": {
|
|
100
|
+
"publish": true
|
|
101
|
+
},
|
|
102
|
+
"github": {
|
|
103
|
+
"release": true
|
|
104
|
+
},
|
|
105
|
+
"plugins": {
|
|
106
|
+
"@release-it/conventional-changelog": {
|
|
107
|
+
"preset": {
|
|
108
|
+
"name": "angular"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
"prettier": {
|
|
114
|
+
"quoteProps": "consistent",
|
|
115
|
+
"singleQuote": true,
|
|
116
|
+
"tabWidth": 2,
|
|
117
|
+
"trailingComma": "es5",
|
|
118
|
+
"useTabs": false
|
|
119
|
+
},
|
|
120
|
+
"react-native-builder-bob": {
|
|
121
|
+
"source": "src",
|
|
122
|
+
"output": "lib",
|
|
123
|
+
"targets": [
|
|
124
|
+
[
|
|
125
|
+
"module",
|
|
126
|
+
{
|
|
127
|
+
"esm": true
|
|
128
|
+
}
|
|
129
|
+
],
|
|
130
|
+
[
|
|
131
|
+
"typescript",
|
|
132
|
+
{
|
|
133
|
+
"project": "tsconfig.build.json"
|
|
134
|
+
}
|
|
135
|
+
]
|
|
136
|
+
]
|
|
137
|
+
},
|
|
138
|
+
"codegenConfig": {
|
|
139
|
+
"name": "LinguaSpec",
|
|
140
|
+
"type": "modules",
|
|
141
|
+
"jsSrcsDir": "src",
|
|
142
|
+
"android": {
|
|
143
|
+
"javaPackageName": "com.lingua"
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
"create-react-native-library": {
|
|
147
|
+
"languages": "kotlin-objc",
|
|
148
|
+
"type": "turbo-module",
|
|
149
|
+
"version": "0.54.5"
|
|
150
|
+
},
|
|
151
|
+
"dependencies": {
|
|
152
|
+
"@babel/runtime": "^7.28.4"
|
|
153
|
+
},
|
|
154
|
+
"scripts": {
|
|
155
|
+
"example": "pnpm --filter react-native-lingua-example",
|
|
156
|
+
"test": "jest",
|
|
157
|
+
"typecheck": "tsc",
|
|
158
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
159
|
+
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
|
|
160
|
+
"build:rust": "cd rust && make all",
|
|
161
|
+
"postinstall": "npm run build:rust || echo 'Warning: Rust build failed. Pre-built binaries may be needed.'",
|
|
162
|
+
"release": "release-it --only-version"
|
|
163
|
+
}
|
|
164
|
+
}
|