@nitro-mlkit/translation 0.1.0-beta.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 +21 -0
- package/NitroMLKitTranslate.podspec +34 -0
- package/README.md +66 -0
- package/android/CMakeLists.txt +12 -0
- package/android/build.gradle +77 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/cpp/cpp-adapter.cpp +9 -0
- package/android/src/main/kotlin/com/margelo/nitro/nitromlkit/translate/HybridTranslator.kt +67 -0
- package/android/src/main/kotlin/com/nitromlkit/translate/NitroMLKitTranslatePackage.kt +19 -0
- package/expo-module.config.json +9 -0
- package/ios/HybridTranslator.swift +160 -0
- package/nitro.json +16 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NitroMLKitTranslate+autolinking.cmake +81 -0
- package/nitrogen/generated/android/NitroMLKitTranslate+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NitroMLKitTranslateOnLoad.cpp +54 -0
- package/nitrogen/generated/android/NitroMLKitTranslateOnLoad.hpp +34 -0
- package/nitrogen/generated/android/c++/JHybridTranslatorSpec.cpp +144 -0
- package/nitrogen/generated/android/c++/JHybridTranslatorSpec.hpp +68 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitromlkit/translate/HybridTranslatorSpec.kt +75 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitromlkit/translate/NitroMLKitTranslateOnLoad.kt +35 -0
- package/nitrogen/generated/ios/NitroMLKitTranslate+autolinking.rb +62 -0
- package/nitrogen/generated/ios/NitroMLKitTranslate-Swift-Cxx-Bridge.cpp +73 -0
- package/nitrogen/generated/ios/NitroMLKitTranslate-Swift-Cxx-Bridge.hpp +261 -0
- package/nitrogen/generated/ios/NitroMLKitTranslate-Swift-Cxx-Umbrella.hpp +46 -0
- package/nitrogen/generated/ios/NitroMLKitTranslateAutolinking.mm +33 -0
- package/nitrogen/generated/ios/NitroMLKitTranslateAutolinking.swift +26 -0
- package/nitrogen/generated/ios/c++/HybridTranslatorSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridTranslatorSpecSwift.hpp +124 -0
- package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
- package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +46 -0
- package/nitrogen/generated/ios/swift/HybridTranslatorSpec.swift +60 -0
- package/nitrogen/generated/ios/swift/HybridTranslatorSpec_cxx.swift +239 -0
- package/nitrogen/generated/shared/c++/HybridTranslatorSpec.cpp +26 -0
- package/nitrogen/generated/shared/c++/HybridTranslatorSpec.hpp +69 -0
- package/package.json +54 -0
- package/src/index.ts +14 -0
- package/src/specs/Translator.nitro.ts +23 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// Func_void_std__string.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import NitroModules
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wraps a Swift `(_ value: String) -> Void` as a class.
|
|
12
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
13
|
+
*/
|
|
14
|
+
public final class Func_void_std__string {
|
|
15
|
+
public typealias bridge = margelo.nitro.mlkit.translate.bridge.swift
|
|
16
|
+
|
|
17
|
+
private let closure: (_ value: String) -> Void
|
|
18
|
+
|
|
19
|
+
public init(_ closure: @escaping (_ value: String) -> Void) {
|
|
20
|
+
self.closure = closure
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@inline(__always)
|
|
24
|
+
public func call(value: std.string) -> Void {
|
|
25
|
+
self.closure(String(value))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
30
|
+
* This acquires one additional strong reference on the object!
|
|
31
|
+
*/
|
|
32
|
+
@inline(__always)
|
|
33
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
34
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Casts an unsafe pointer to a `Func_void_std__string`.
|
|
39
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_std__string>`.
|
|
40
|
+
* This removes one strong reference from the object!
|
|
41
|
+
*/
|
|
42
|
+
@inline(__always)
|
|
43
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__string {
|
|
44
|
+
return Unmanaged<Func_void_std__string>.fromOpaque(pointer).takeRetainedValue()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// Func_void_std__vector_std__string_.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import NitroModules
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wraps a Swift `(_ value: [String]) -> Void` as a class.
|
|
12
|
+
* This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`.
|
|
13
|
+
*/
|
|
14
|
+
public final class Func_void_std__vector_std__string_ {
|
|
15
|
+
public typealias bridge = margelo.nitro.mlkit.translate.bridge.swift
|
|
16
|
+
|
|
17
|
+
private let closure: (_ value: [String]) -> Void
|
|
18
|
+
|
|
19
|
+
public init(_ closure: @escaping (_ value: [String]) -> Void) {
|
|
20
|
+
self.closure = closure
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@inline(__always)
|
|
24
|
+
public func call(value: bridge.std__vector_std__string_) -> Void {
|
|
25
|
+
self.closure(value.map({ __item in String(__item) }))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
30
|
+
* This acquires one additional strong reference on the object!
|
|
31
|
+
*/
|
|
32
|
+
@inline(__always)
|
|
33
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
34
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Casts an unsafe pointer to a `Func_void_std__vector_std__string_`.
|
|
39
|
+
* The pointer has to be a retained opaque `Unmanaged<Func_void_std__vector_std__string_>`.
|
|
40
|
+
* This removes one strong reference from the object!
|
|
41
|
+
*/
|
|
42
|
+
@inline(__always)
|
|
43
|
+
public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__vector_std__string_ {
|
|
44
|
+
return Unmanaged<Func_void_std__vector_std__string_>.fromOpaque(pointer).takeRetainedValue()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridTranslatorSpec.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import NitroModules
|
|
9
|
+
|
|
10
|
+
/// See ``HybridTranslatorSpec``
|
|
11
|
+
public protocol HybridTranslatorSpec_protocol: HybridObject {
|
|
12
|
+
// Properties
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
// Methods
|
|
16
|
+
func translate(text: String, sourceLanguage: String, targetLanguage: String) throws -> Promise<String>
|
|
17
|
+
func downloadModel(language: String, requireWifi: Bool) throws -> Promise<Void>
|
|
18
|
+
func isModelDownloaded(language: String) throws -> Promise<Bool>
|
|
19
|
+
func deleteModel(language: String) throws -> Promise<Void>
|
|
20
|
+
func getDownloadedModels() throws -> Promise<[String]>
|
|
21
|
+
func isAvailable() throws -> Bool
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public extension HybridTranslatorSpec_protocol {
|
|
25
|
+
/// Default implementation of ``HybridObject.toString``
|
|
26
|
+
func toString() -> String {
|
|
27
|
+
return "[HybridObject Translator]"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// See ``HybridTranslatorSpec``
|
|
32
|
+
open class HybridTranslatorSpec_base {
|
|
33
|
+
private weak var cxxWrapper: HybridTranslatorSpec_cxx? = nil
|
|
34
|
+
public init() { }
|
|
35
|
+
public func getCxxWrapper() -> HybridTranslatorSpec_cxx {
|
|
36
|
+
#if DEBUG
|
|
37
|
+
guard self is any HybridTranslatorSpec else {
|
|
38
|
+
fatalError("`self` is not a `HybridTranslatorSpec`! Did you accidentally inherit from `HybridTranslatorSpec_base` instead of `HybridTranslatorSpec`?")
|
|
39
|
+
}
|
|
40
|
+
#endif
|
|
41
|
+
if let cxxWrapper = self.cxxWrapper {
|
|
42
|
+
return cxxWrapper
|
|
43
|
+
} else {
|
|
44
|
+
let cxxWrapper = HybridTranslatorSpec_cxx(self as! any HybridTranslatorSpec)
|
|
45
|
+
self.cxxWrapper = cxxWrapper
|
|
46
|
+
return cxxWrapper
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A Swift base-protocol representing the Translator HybridObject.
|
|
53
|
+
* Implement this protocol to create Swift-based instances of Translator.
|
|
54
|
+
* ```swift
|
|
55
|
+
* class HybridTranslator : HybridTranslatorSpec {
|
|
56
|
+
* // ...
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
public typealias HybridTranslatorSpec = HybridTranslatorSpec_protocol & HybridTranslatorSpec_base
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridTranslatorSpec_cxx.swift
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
import NitroModules
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A class implementation that bridges HybridTranslatorSpec over to C++.
|
|
12
|
+
* In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined.
|
|
13
|
+
*
|
|
14
|
+
* Also, some Swift types need to be bridged with special handling:
|
|
15
|
+
* - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330)
|
|
16
|
+
* - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
|
|
17
|
+
* - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
|
|
18
|
+
*/
|
|
19
|
+
open class HybridTranslatorSpec_cxx {
|
|
20
|
+
/**
|
|
21
|
+
* The Swift <> C++ bridge's namespace (`margelo::nitro::mlkit::translate::bridge::swift`)
|
|
22
|
+
* from `NitroMLKitTranslate-Swift-Cxx-Bridge.hpp`.
|
|
23
|
+
* This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
|
|
24
|
+
*/
|
|
25
|
+
public typealias bridge = margelo.nitro.mlkit.translate.bridge.swift
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Holds an instance of the `HybridTranslatorSpec` Swift protocol.
|
|
29
|
+
*/
|
|
30
|
+
private var __implementation: any HybridTranslatorSpec
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Holds a weak pointer to the C++ class that wraps the Swift class.
|
|
34
|
+
*/
|
|
35
|
+
private var __cxxPart: bridge.std__weak_ptr_HybridTranslatorSpec_
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Create a new `HybridTranslatorSpec_cxx` that wraps the given `HybridTranslatorSpec`.
|
|
39
|
+
* All properties and methods bridge to C++ types.
|
|
40
|
+
*/
|
|
41
|
+
public init(_ implementation: any HybridTranslatorSpec) {
|
|
42
|
+
self.__implementation = implementation
|
|
43
|
+
self.__cxxPart = .init()
|
|
44
|
+
/* no base class */
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get the actual `HybridTranslatorSpec` instance this class wraps.
|
|
49
|
+
*/
|
|
50
|
+
@inline(__always)
|
|
51
|
+
public func getHybridTranslatorSpec() -> any HybridTranslatorSpec {
|
|
52
|
+
return __implementation
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Casts this instance to a retained unsafe raw pointer.
|
|
57
|
+
* This acquires one additional strong reference on the object!
|
|
58
|
+
*/
|
|
59
|
+
public func toUnsafe() -> UnsafeMutableRawPointer {
|
|
60
|
+
return Unmanaged.passRetained(self).toOpaque()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Casts an unsafe pointer to a `HybridTranslatorSpec_cxx`.
|
|
65
|
+
* The pointer has to be a retained opaque `Unmanaged<HybridTranslatorSpec_cxx>`.
|
|
66
|
+
* This removes one strong reference from the object!
|
|
67
|
+
*/
|
|
68
|
+
public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridTranslatorSpec_cxx {
|
|
69
|
+
return Unmanaged<HybridTranslatorSpec_cxx>.fromOpaque(pointer).takeRetainedValue()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Gets (or creates) the C++ part of this Hybrid Object.
|
|
74
|
+
* The C++ part is a `std::shared_ptr<HybridTranslatorSpec>`.
|
|
75
|
+
*/
|
|
76
|
+
public func getCxxPart() -> bridge.std__shared_ptr_HybridTranslatorSpec_ {
|
|
77
|
+
let cachedCxxPart = self.__cxxPart.lock()
|
|
78
|
+
if Bool(fromCxx: cachedCxxPart) {
|
|
79
|
+
return cachedCxxPart
|
|
80
|
+
} else {
|
|
81
|
+
let newCxxPart = bridge.create_std__shared_ptr_HybridTranslatorSpec_(self.toUnsafe())
|
|
82
|
+
__cxxPart = bridge.weakify_std__shared_ptr_HybridTranslatorSpec_(newCxxPart)
|
|
83
|
+
return newCxxPart
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get the memory size of the Swift class (plus size of any other allocations)
|
|
91
|
+
* so the JS VM can properly track it and garbage-collect the JS object if needed.
|
|
92
|
+
*/
|
|
93
|
+
@inline(__always)
|
|
94
|
+
public var memorySize: Int {
|
|
95
|
+
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Compares this object with the given [other] object for reference equality.
|
|
100
|
+
*/
|
|
101
|
+
@inline(__always)
|
|
102
|
+
public func equals(other: HybridTranslatorSpec_cxx) -> Bool {
|
|
103
|
+
return self.__implementation === other.__implementation
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Call dispose() on the Swift class.
|
|
108
|
+
* This _may_ be called manually from JS.
|
|
109
|
+
*/
|
|
110
|
+
@inline(__always)
|
|
111
|
+
public func dispose() {
|
|
112
|
+
self.__implementation.dispose()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Call toString() on the Swift class.
|
|
117
|
+
*/
|
|
118
|
+
@inline(__always)
|
|
119
|
+
public func toString() -> String {
|
|
120
|
+
return self.__implementation.toString()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Properties
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
// Methods
|
|
127
|
+
@inline(__always)
|
|
128
|
+
public final func translate(text: std.string, sourceLanguage: std.string, targetLanguage: std.string) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
|
|
129
|
+
do {
|
|
130
|
+
let __result = try self.__implementation.translate(text: String(text), sourceLanguage: String(sourceLanguage), targetLanguage: String(targetLanguage))
|
|
131
|
+
let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
|
|
132
|
+
let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
|
|
133
|
+
let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__string__(__promise)
|
|
134
|
+
__result
|
|
135
|
+
.then({ __result in __promiseHolder.resolve(std.string(__result)) })
|
|
136
|
+
.catch({ __error in __promiseHolder.reject(__error.toCpp()) })
|
|
137
|
+
return __promise
|
|
138
|
+
}()
|
|
139
|
+
return bridge.create_Result_std__shared_ptr_Promise_std__string___(__resultCpp)
|
|
140
|
+
} catch (let __error) {
|
|
141
|
+
let __exceptionPtr = __error.toCpp()
|
|
142
|
+
return bridge.create_Result_std__shared_ptr_Promise_std__string___(__exceptionPtr)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
@inline(__always)
|
|
147
|
+
public final func downloadModel(language: std.string, requireWifi: Bool) -> bridge.Result_std__shared_ptr_Promise_void___ {
|
|
148
|
+
do {
|
|
149
|
+
let __result = try self.__implementation.downloadModel(language: String(language), requireWifi: requireWifi)
|
|
150
|
+
let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
|
|
151
|
+
let __promise = bridge.create_std__shared_ptr_Promise_void__()
|
|
152
|
+
let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
|
|
153
|
+
__result
|
|
154
|
+
.then({ __result in __promiseHolder.resolve() })
|
|
155
|
+
.catch({ __error in __promiseHolder.reject(__error.toCpp()) })
|
|
156
|
+
return __promise
|
|
157
|
+
}()
|
|
158
|
+
return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
|
|
159
|
+
} catch (let __error) {
|
|
160
|
+
let __exceptionPtr = __error.toCpp()
|
|
161
|
+
return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
@inline(__always)
|
|
166
|
+
public final func isModelDownloaded(language: std.string) -> bridge.Result_std__shared_ptr_Promise_bool___ {
|
|
167
|
+
do {
|
|
168
|
+
let __result = try self.__implementation.isModelDownloaded(language: String(language))
|
|
169
|
+
let __resultCpp = { () -> bridge.std__shared_ptr_Promise_bool__ in
|
|
170
|
+
let __promise = bridge.create_std__shared_ptr_Promise_bool__()
|
|
171
|
+
let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_bool__(__promise)
|
|
172
|
+
__result
|
|
173
|
+
.then({ __result in __promiseHolder.resolve(__result) })
|
|
174
|
+
.catch({ __error in __promiseHolder.reject(__error.toCpp()) })
|
|
175
|
+
return __promise
|
|
176
|
+
}()
|
|
177
|
+
return bridge.create_Result_std__shared_ptr_Promise_bool___(__resultCpp)
|
|
178
|
+
} catch (let __error) {
|
|
179
|
+
let __exceptionPtr = __error.toCpp()
|
|
180
|
+
return bridge.create_Result_std__shared_ptr_Promise_bool___(__exceptionPtr)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
@inline(__always)
|
|
185
|
+
public final func deleteModel(language: std.string) -> bridge.Result_std__shared_ptr_Promise_void___ {
|
|
186
|
+
do {
|
|
187
|
+
let __result = try self.__implementation.deleteModel(language: String(language))
|
|
188
|
+
let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
|
|
189
|
+
let __promise = bridge.create_std__shared_ptr_Promise_void__()
|
|
190
|
+
let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
|
|
191
|
+
__result
|
|
192
|
+
.then({ __result in __promiseHolder.resolve() })
|
|
193
|
+
.catch({ __error in __promiseHolder.reject(__error.toCpp()) })
|
|
194
|
+
return __promise
|
|
195
|
+
}()
|
|
196
|
+
return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
|
|
197
|
+
} catch (let __error) {
|
|
198
|
+
let __exceptionPtr = __error.toCpp()
|
|
199
|
+
return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
@inline(__always)
|
|
204
|
+
public final func getDownloadedModels() -> bridge.Result_std__shared_ptr_Promise_std__vector_std__string____ {
|
|
205
|
+
do {
|
|
206
|
+
let __result = try self.__implementation.getDownloadedModels()
|
|
207
|
+
let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__vector_std__string___ in
|
|
208
|
+
let __promise = bridge.create_std__shared_ptr_Promise_std__vector_std__string___()
|
|
209
|
+
let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__vector_std__string___(__promise)
|
|
210
|
+
__result
|
|
211
|
+
.then({ __result in __promiseHolder.resolve({ () -> bridge.std__vector_std__string_ in
|
|
212
|
+
var __vector = bridge.create_std__vector_std__string_(__result.count)
|
|
213
|
+
for __item in __result {
|
|
214
|
+
__vector.push_back(std.string(__item))
|
|
215
|
+
}
|
|
216
|
+
return __vector
|
|
217
|
+
}()) })
|
|
218
|
+
.catch({ __error in __promiseHolder.reject(__error.toCpp()) })
|
|
219
|
+
return __promise
|
|
220
|
+
}()
|
|
221
|
+
return bridge.create_Result_std__shared_ptr_Promise_std__vector_std__string____(__resultCpp)
|
|
222
|
+
} catch (let __error) {
|
|
223
|
+
let __exceptionPtr = __error.toCpp()
|
|
224
|
+
return bridge.create_Result_std__shared_ptr_Promise_std__vector_std__string____(__exceptionPtr)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
@inline(__always)
|
|
229
|
+
public final func isAvailable() -> bridge.Result_bool_ {
|
|
230
|
+
do {
|
|
231
|
+
let __result = try self.__implementation.isAvailable()
|
|
232
|
+
let __resultCpp = __result
|
|
233
|
+
return bridge.create_Result_bool_(__resultCpp)
|
|
234
|
+
} catch (let __error) {
|
|
235
|
+
let __exceptionPtr = __error.toCpp()
|
|
236
|
+
return bridge.create_Result_bool_(__exceptionPtr)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridTranslatorSpec.cpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#include "HybridTranslatorSpec.hpp"
|
|
9
|
+
|
|
10
|
+
namespace margelo::nitro::mlkit::translate {
|
|
11
|
+
|
|
12
|
+
void HybridTranslatorSpec::loadHybridMethods() {
|
|
13
|
+
// load base methods/properties
|
|
14
|
+
HybridObject::loadHybridMethods();
|
|
15
|
+
// load custom methods/properties
|
|
16
|
+
registerHybrids(this, [](Prototype& prototype) {
|
|
17
|
+
prototype.registerHybridMethod("translate", &HybridTranslatorSpec::translate);
|
|
18
|
+
prototype.registerHybridMethod("downloadModel", &HybridTranslatorSpec::downloadModel);
|
|
19
|
+
prototype.registerHybridMethod("isModelDownloaded", &HybridTranslatorSpec::isModelDownloaded);
|
|
20
|
+
prototype.registerHybridMethod("deleteModel", &HybridTranslatorSpec::deleteModel);
|
|
21
|
+
prototype.registerHybridMethod("getDownloadedModels", &HybridTranslatorSpec::getDownloadedModels);
|
|
22
|
+
prototype.registerHybridMethod("isAvailable", &HybridTranslatorSpec::isAvailable);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
} // namespace margelo::nitro::mlkit::translate
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
///
|
|
2
|
+
/// HybridTranslatorSpec.hpp
|
|
3
|
+
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
|
|
4
|
+
/// https://github.com/mrousavy/nitro
|
|
5
|
+
/// Copyright © Marc Rousavy @ Margelo
|
|
6
|
+
///
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#if __has_include(<NitroModules/HybridObject.hpp>)
|
|
11
|
+
#include <NitroModules/HybridObject.hpp>
|
|
12
|
+
#else
|
|
13
|
+
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
#include <string>
|
|
19
|
+
#include <NitroModules/Promise.hpp>
|
|
20
|
+
#include <vector>
|
|
21
|
+
|
|
22
|
+
namespace margelo::nitro::mlkit::translate {
|
|
23
|
+
|
|
24
|
+
using namespace margelo::nitro;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* An abstract base class for `Translator`
|
|
28
|
+
* Inherit this class to create instances of `HybridTranslatorSpec` in C++.
|
|
29
|
+
* You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
|
|
30
|
+
* @example
|
|
31
|
+
* ```cpp
|
|
32
|
+
* class HybridTranslator: public HybridTranslatorSpec {
|
|
33
|
+
* public:
|
|
34
|
+
* HybridTranslator(...): HybridObject(TAG) { ... }
|
|
35
|
+
* // ...
|
|
36
|
+
* };
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
class HybridTranslatorSpec: public virtual HybridObject {
|
|
40
|
+
public:
|
|
41
|
+
// Constructor
|
|
42
|
+
explicit HybridTranslatorSpec(): HybridObject(TAG) { }
|
|
43
|
+
|
|
44
|
+
// Destructor
|
|
45
|
+
~HybridTranslatorSpec() override = default;
|
|
46
|
+
|
|
47
|
+
public:
|
|
48
|
+
// Properties
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
public:
|
|
52
|
+
// Methods
|
|
53
|
+
virtual std::shared_ptr<Promise<std::string>> translate(const std::string& text, const std::string& sourceLanguage, const std::string& targetLanguage) = 0;
|
|
54
|
+
virtual std::shared_ptr<Promise<void>> downloadModel(const std::string& language, bool requireWifi) = 0;
|
|
55
|
+
virtual std::shared_ptr<Promise<bool>> isModelDownloaded(const std::string& language) = 0;
|
|
56
|
+
virtual std::shared_ptr<Promise<void>> deleteModel(const std::string& language) = 0;
|
|
57
|
+
virtual std::shared_ptr<Promise<std::vector<std::string>>> getDownloadedModels() = 0;
|
|
58
|
+
virtual bool isAvailable() = 0;
|
|
59
|
+
|
|
60
|
+
protected:
|
|
61
|
+
// Hybrid Setup
|
|
62
|
+
void loadHybridMethods() override;
|
|
63
|
+
|
|
64
|
+
protected:
|
|
65
|
+
// Tag for logging
|
|
66
|
+
static constexpr auto TAG = "Translator";
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
} // namespace margelo::nitro::mlkit::translate
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nitro-mlkit/translation",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "High-performance on-device translation for React Native — Google ML Kit + Nitro. Translate between 50+ languages, all on-device once the model is downloaded.",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"types": "src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"codegen": "nitrogen",
|
|
9
|
+
"build": "tsc --noEmit"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"react-native",
|
|
13
|
+
"nitro",
|
|
14
|
+
"mlkit",
|
|
15
|
+
"translation",
|
|
16
|
+
"translate",
|
|
17
|
+
"nlp",
|
|
18
|
+
"machine-learning",
|
|
19
|
+
"on-device",
|
|
20
|
+
"expo"
|
|
21
|
+
],
|
|
22
|
+
"author": "pologonzalo",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/pologonzalo/react-native-nitro-mlkit",
|
|
27
|
+
"directory": "packages/translation"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"expo-modules-core": "*",
|
|
31
|
+
"react": ">=18",
|
|
32
|
+
"react-native": ">=0.76",
|
|
33
|
+
"react-native-nitro-modules": ">=0.20"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"expo-modules-core": "*",
|
|
37
|
+
"nitro-codegen": "*",
|
|
38
|
+
"react-native-nitro-modules": "*",
|
|
39
|
+
"typescript": "~5.9.0"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"src",
|
|
43
|
+
"ios/**/*.swift",
|
|
44
|
+
"android/src",
|
|
45
|
+
"android/build.gradle",
|
|
46
|
+
"android/CMakeLists.txt",
|
|
47
|
+
"expo-module.config.json",
|
|
48
|
+
"nitrogen/generated",
|
|
49
|
+
"*.podspec",
|
|
50
|
+
"nitro.json",
|
|
51
|
+
"README.md",
|
|
52
|
+
"LICENSE"
|
|
53
|
+
]
|
|
54
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { type Translator } from "./specs/Translator.nitro";
|
|
2
|
+
|
|
3
|
+
import { Platform } from "react-native";
|
|
4
|
+
import { requireOptionalNativeModule } from "expo-modules-core";
|
|
5
|
+
import { NitroModules } from "react-native-nitro-modules";
|
|
6
|
+
import type { Translator } from "./specs/Translator.nitro";
|
|
7
|
+
|
|
8
|
+
if (Platform.OS === "android") {
|
|
9
|
+
requireOptionalNativeModule("NitroMLKitTranslate");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Get the shared Translator instance. */
|
|
13
|
+
export const NitroTranslate =
|
|
14
|
+
NitroModules.createHybridObject<Translator>("Translator");
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { HybridObject } from "react-native-nitro-modules";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* On-device translation powered by Google ML Kit. 50+ languages, all on-device
|
|
5
|
+
* once the language model is downloaded. Language codes are BCP-47 (e.g. "en",
|
|
6
|
+
* "es", "fr", "de", "ja").
|
|
7
|
+
*/
|
|
8
|
+
export interface Translator extends HybridObject<{ ios: "swift"; android: "kotlin" }> {
|
|
9
|
+
/**
|
|
10
|
+
* Translate text from source → target language. Downloads the required model
|
|
11
|
+
* on first use (may take a few seconds / need network).
|
|
12
|
+
*/
|
|
13
|
+
translate(text: string, sourceLanguage: string, targetLanguage: string): Promise<string>;
|
|
14
|
+
/** Download a language model ahead of time. */
|
|
15
|
+
downloadModel(language: string, requireWifi: boolean): Promise<void>;
|
|
16
|
+
/** Whether a language model is already downloaded. */
|
|
17
|
+
isModelDownloaded(language: string): Promise<boolean>;
|
|
18
|
+
/** Delete a downloaded language model. */
|
|
19
|
+
deleteModel(language: string): Promise<void>;
|
|
20
|
+
/** BCP-47 tags of all currently downloaded models. */
|
|
21
|
+
getDownloadedModels(): Promise<string[]>;
|
|
22
|
+
isAvailable(): boolean;
|
|
23
|
+
}
|