@capawesome/capacitor-app-language 0.0.1
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/CapawesomeCapacitorAppLanguage.podspec +17 -0
- package/LICENSE +21 -0
- package/Package.swift +28 -0
- package/README.md +269 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/AppLanguage.java +65 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/AppLanguagePlugin.java +136 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/classes/CustomException.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/classes/CustomExceptions.java +10 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/classes/options/SetLanguageOptions.java +33 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/classes/results/GetLanguageResult.java +25 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/interfaces/EmptyCallback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +146 -0
- package/dist/esm/definitions.d.ts +73 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +8 -0
- package/dist/esm/web.js +16 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +30 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +33 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/AppLanguage.swift +36 -0
- package/ios/Plugin/AppLanguagePlugin.swift +71 -0
- package/ios/Plugin/Classes/Results/GetLanguageResult.swift +16 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Protocols/Result.swift +5 -0
- package/package.json +91 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'CapawesomeCapacitorAppLanguage'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['repository']['url']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '15.0'
|
|
15
|
+
s.dependency 'Capacitor'
|
|
16
|
+
s.swift_version = '5.1'
|
|
17
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robin Genz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Package.swift
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
|
|
4
|
+
let package = Package(
|
|
5
|
+
name: "CapawesomeCapacitorAppLanguage",
|
|
6
|
+
platforms: [.iOS(.v15)],
|
|
7
|
+
products: [
|
|
8
|
+
.library(
|
|
9
|
+
name: "CapawesomeCapacitorAppLanguage",
|
|
10
|
+
targets: ["AppLanguagePlugin"])
|
|
11
|
+
],
|
|
12
|
+
dependencies: [
|
|
13
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0")
|
|
14
|
+
],
|
|
15
|
+
targets: [
|
|
16
|
+
.target(
|
|
17
|
+
name: "AppLanguagePlugin",
|
|
18
|
+
dependencies: [
|
|
19
|
+
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
20
|
+
.product(name: "Cordova", package: "capacitor-swift-pm")
|
|
21
|
+
],
|
|
22
|
+
path: "ios/Plugin"),
|
|
23
|
+
.testTarget(
|
|
24
|
+
name: "AppLanguagePluginTests",
|
|
25
|
+
dependencies: ["AppLanguagePlugin"],
|
|
26
|
+
path: "ios/PluginTests")
|
|
27
|
+
]
|
|
28
|
+
)
|
package/README.md
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
# @capawesome/capacitor-app-language
|
|
2
|
+
|
|
3
|
+
Capacitor plugin to manage the app's own language override, independent of the device language.
|
|
4
|
+
|
|
5
|
+
<div class="capawesome-z29o10a">
|
|
6
|
+
<a href="https://cloud.capawesome.io/" target="_blank">
|
|
7
|
+
<img alt="Deliver Live Updates to your Capacitor app with Capawesome Cloud" src="https://cloud.capawesome.io/assets/banners/cloud-build-and-deploy-capacitor-apps.png?t=1" />
|
|
8
|
+
</a>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🌐 **Language override**: Set the app's language independently of the device language (Android).
|
|
14
|
+
- 🔎 **Read language**: Read the app's current language override.
|
|
15
|
+
- ♻️ **Reset**: Clear the override so the app follows the device language again (Android).
|
|
16
|
+
- ⚙️ **Settings**: Deep-link to the app's system settings page where the user can change the app language.
|
|
17
|
+
- 📦 **CocoaPods & SPM**: Supports CocoaPods and Swift Package Manager for iOS.
|
|
18
|
+
- 🔁 **Up-to-date**: Always supports the latest Capacitor version.
|
|
19
|
+
- 🔗 **Compatibility**: Works alongside the [Localization](https://capawesome.io/plugins/localization/) plugin, which reads the user's localization preferences.
|
|
20
|
+
|
|
21
|
+
Missing a feature? Just [open an issue](https://github.com/capawesome-team/capacitor-plugins/issues) and we'll take a look!
|
|
22
|
+
|
|
23
|
+
## Newsletter
|
|
24
|
+
|
|
25
|
+
Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our [Capawesome Newsletter](https://cloud.capawesome.io/newsletter/).
|
|
26
|
+
|
|
27
|
+
## Compatibility
|
|
28
|
+
|
|
29
|
+
| Plugin Version | Capacitor Version | Status |
|
|
30
|
+
| -------------- | ----------------- | -------------- |
|
|
31
|
+
| 0.x.x | >=8.x.x | Active support |
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
You can use our **AI-Assisted Setup** to install the plugin.
|
|
36
|
+
Add the [Capawesome Skills](https://github.com/capawesome-team/skills) to your AI tool using the following command:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npx skills add capawesome-team/skills --skill capacitor-plugins
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Then use the following prompt:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome/capacitor-app-language` plugin in my project.
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
If you prefer **Manual Setup**, install the plugin by running the following commands and follow the platform-specific instructions below:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm install @capawesome/capacitor-app-language
|
|
52
|
+
npx cap sync
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This plugin only affects **natively rendered** strings (e.g. permission dialogs, notifications, or plugin-presented sheets). The language of your web UI should be handled by your app's i18n library.
|
|
56
|
+
|
|
57
|
+
### Android
|
|
58
|
+
|
|
59
|
+
#### Variables
|
|
60
|
+
|
|
61
|
+
This plugin will use the following project variables (defined in your app's `variables.gradle` file):
|
|
62
|
+
|
|
63
|
+
- `$androidxAppCompatVersion` version of `androidx.appcompat:appcompat` (default: `1.7.1`)
|
|
64
|
+
|
|
65
|
+
#### Locale Configuration
|
|
66
|
+
|
|
67
|
+
To integrate the app language into the system settings (Android 13+), declare the supported locales in a `res/xml/locales_config.xml` file and reference it from the `<application>` tag in your `AndroidManifest.xml`:
|
|
68
|
+
|
|
69
|
+
```xml
|
|
70
|
+
<!-- res/xml/locales_config.xml -->
|
|
71
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
72
|
+
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
|
|
73
|
+
<locale android:name="en" />
|
|
74
|
+
<locale android:name="de" />
|
|
75
|
+
</locale-config>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```xml
|
|
79
|
+
<application
|
|
80
|
+
...
|
|
81
|
+
android:localeConfig="@xml/locales_config">
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
To persist the selected language across app restarts on Android 12 and below, add the following `<service>` inside the `<application>` tag of your `AndroidManifest.xml`:
|
|
85
|
+
|
|
86
|
+
```xml
|
|
87
|
+
<service
|
|
88
|
+
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
|
|
89
|
+
android:enabled="false"
|
|
90
|
+
android:exported="false">
|
|
91
|
+
<meta-data
|
|
92
|
+
android:name="autoStoreLocales"
|
|
93
|
+
android:value="true" />
|
|
94
|
+
</service>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
> [!NOTE]
|
|
98
|
+
> Setting the language recreates the current activity, which reloads the web view.
|
|
99
|
+
|
|
100
|
+
### iOS
|
|
101
|
+
|
|
102
|
+
On iOS, the app language can only be changed by the **user** in the system settings. The `setLanguage(...)` and `resetLanguage(...)` methods are therefore not available. Use `openSettings(...)` to deep-link the user to the app's settings page.
|
|
103
|
+
|
|
104
|
+
The per-app language row is only shown in the system settings if the app bundle provides **more than one** localization. Declare the supported localizations in the `Info.plist` file of your app:
|
|
105
|
+
|
|
106
|
+
```xml
|
|
107
|
+
<key>CFBundleLocalizations</key>
|
|
108
|
+
<array>
|
|
109
|
+
<string>en</string>
|
|
110
|
+
<string>de</string>
|
|
111
|
+
</array>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Configuration
|
|
115
|
+
|
|
116
|
+
No configuration required for this plugin.
|
|
117
|
+
|
|
118
|
+
## Usage
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { AppLanguage } from '@capawesome/capacitor-app-language';
|
|
122
|
+
|
|
123
|
+
const getLanguage = async () => {
|
|
124
|
+
const { languageTag } = await AppLanguage.getLanguage();
|
|
125
|
+
return languageTag;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const setLanguage = async () => {
|
|
129
|
+
await AppLanguage.setLanguage({ languageTag: 'de-DE' });
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const resetLanguage = async () => {
|
|
133
|
+
await AppLanguage.resetLanguage();
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const openSettings = async () => {
|
|
137
|
+
await AppLanguage.openSettings();
|
|
138
|
+
};
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## API
|
|
142
|
+
|
|
143
|
+
<docgen-index>
|
|
144
|
+
|
|
145
|
+
* [`getLanguage()`](#getlanguage)
|
|
146
|
+
* [`openSettings()`](#opensettings)
|
|
147
|
+
* [`resetLanguage()`](#resetlanguage)
|
|
148
|
+
* [`setLanguage(...)`](#setlanguage)
|
|
149
|
+
* [Interfaces](#interfaces)
|
|
150
|
+
|
|
151
|
+
</docgen-index>
|
|
152
|
+
|
|
153
|
+
<docgen-api>
|
|
154
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
155
|
+
|
|
156
|
+
### getLanguage()
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
getLanguage() => Promise<GetLanguageResult>
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Get the app's current language override.
|
|
163
|
+
|
|
164
|
+
The language override is independent of the device language and only
|
|
165
|
+
affects natively rendered strings (e.g. permission dialogs).
|
|
166
|
+
|
|
167
|
+
**Returns:** <code>Promise<<a href="#getlanguageresult">GetLanguageResult</a>></code>
|
|
168
|
+
|
|
169
|
+
**Since:** 0.1.0
|
|
170
|
+
|
|
171
|
+
--------------------
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
### openSettings()
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
openSettings() => Promise<void>
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Open the app's settings page in the system settings.
|
|
181
|
+
|
|
182
|
+
On iOS, this is where the user can change the app's language (the language
|
|
183
|
+
row is only shown if the app bundle provides more than one localization).
|
|
184
|
+
|
|
185
|
+
**Since:** 0.1.0
|
|
186
|
+
|
|
187
|
+
--------------------
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
### resetLanguage()
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
resetLanguage() => Promise<void>
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Reset the app's language override so the app follows the device language
|
|
197
|
+
again.
|
|
198
|
+
|
|
199
|
+
Resetting the language recreates the current activity, which reloads the
|
|
200
|
+
web view.
|
|
201
|
+
|
|
202
|
+
Only available on Android.
|
|
203
|
+
|
|
204
|
+
**Since:** 0.1.0
|
|
205
|
+
|
|
206
|
+
--------------------
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
### setLanguage(...)
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
setLanguage(options: SetLanguageOptions) => Promise<void>
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Set the app's language override.
|
|
216
|
+
|
|
217
|
+
Setting the language recreates the current activity, which reloads the
|
|
218
|
+
web view.
|
|
219
|
+
|
|
220
|
+
On iOS, the language can only be changed by the user in the system
|
|
221
|
+
settings (see the `openSettings(...)` method).
|
|
222
|
+
|
|
223
|
+
Only available on Android.
|
|
224
|
+
|
|
225
|
+
| Param | Type |
|
|
226
|
+
| ------------- | ----------------------------------------------------------------- |
|
|
227
|
+
| **`options`** | <code><a href="#setlanguageoptions">SetLanguageOptions</a></code> |
|
|
228
|
+
|
|
229
|
+
**Since:** 0.1.0
|
|
230
|
+
|
|
231
|
+
--------------------
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
### Interfaces
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
#### GetLanguageResult
|
|
238
|
+
|
|
239
|
+
| Prop | Type | Description | Since |
|
|
240
|
+
| ----------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ----- |
|
|
241
|
+
| **`languageTag`** | <code>string \| null</code> | The BCP 47 language tag of the app's language override. Returns `null` if no override is set and the app follows the device language. | 0.1.0 |
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
#### SetLanguageOptions
|
|
245
|
+
|
|
246
|
+
| Prop | Type | Description | Since |
|
|
247
|
+
| ----------------- | ------------------- | -------------------------------------------------------------- | ----- |
|
|
248
|
+
| **`languageTag`** | <code>string</code> | The BCP 47 language tag to set as the app's language override. | 0.1.0 |
|
|
249
|
+
|
|
250
|
+
</docgen-api>
|
|
251
|
+
|
|
252
|
+
## Platform Behavior
|
|
253
|
+
|
|
254
|
+
Setting the app language programmatically is only supported on Android. On iOS, the language can only be changed by the user in the system settings.
|
|
255
|
+
|
|
256
|
+
| Method | Android | iOS | Web |
|
|
257
|
+
| ------------------ | ------- | ---------------- | ---------------- |
|
|
258
|
+
| `getLanguage()` | ✅ | ✅ | ❌ Unimplemented |
|
|
259
|
+
| `setLanguage(...)` | ✅ | ❌ Unimplemented | ❌ Unimplemented |
|
|
260
|
+
| `resetLanguage()` | ✅ | ❌ Unimplemented | ❌ Unimplemented |
|
|
261
|
+
| `openSettings()` | ✅ | ✅ | ❌ Unimplemented |
|
|
262
|
+
|
|
263
|
+
## Changelog
|
|
264
|
+
|
|
265
|
+
See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/app-language/CHANGELOG.md).
|
|
266
|
+
|
|
267
|
+
## License
|
|
268
|
+
|
|
269
|
+
See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/app-language/LICENSE).
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
dependencies {
|
|
14
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
apply plugin: 'com.android.library'
|
|
19
|
+
|
|
20
|
+
android {
|
|
21
|
+
namespace = "io.capawesome.capacitorjs.plugins.applanguage"
|
|
22
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
25
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.0"
|
|
28
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
29
|
+
}
|
|
30
|
+
buildTypes {
|
|
31
|
+
release {
|
|
32
|
+
minifyEnabled false
|
|
33
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
lintOptions {
|
|
37
|
+
abortOnError = false
|
|
38
|
+
}
|
|
39
|
+
compileOptions {
|
|
40
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
repositories {
|
|
46
|
+
google()
|
|
47
|
+
mavenCentral()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
dependencies {
|
|
52
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
53
|
+
implementation project(':capacitor-android')
|
|
54
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
55
|
+
testImplementation "junit:junit:$junitVersion"
|
|
56
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
57
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
58
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.applanguage;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.content.Context;
|
|
5
|
+
import android.content.Intent;
|
|
6
|
+
import android.net.Uri;
|
|
7
|
+
import android.os.Build;
|
|
8
|
+
import android.provider.Settings;
|
|
9
|
+
import androidx.annotation.NonNull;
|
|
10
|
+
import androidx.appcompat.app.AppCompatDelegate;
|
|
11
|
+
import androidx.core.os.LocaleListCompat;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.applanguage.classes.options.SetLanguageOptions;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.applanguage.classes.results.GetLanguageResult;
|
|
14
|
+
import io.capawesome.capacitorjs.plugins.applanguage.interfaces.EmptyCallback;
|
|
15
|
+
import io.capawesome.capacitorjs.plugins.applanguage.interfaces.NonEmptyResultCallback;
|
|
16
|
+
|
|
17
|
+
public class AppLanguage {
|
|
18
|
+
|
|
19
|
+
@NonNull
|
|
20
|
+
private final AppLanguagePlugin plugin;
|
|
21
|
+
|
|
22
|
+
public AppLanguage(@NonNull AppLanguagePlugin plugin) {
|
|
23
|
+
this.plugin = plugin;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public void getLanguage(@NonNull NonEmptyResultCallback<GetLanguageResult> callback) {
|
|
27
|
+
LocaleListCompat locales = AppCompatDelegate.getApplicationLocales();
|
|
28
|
+
String languageTag = locales.isEmpty() ? null : locales.get(0).toLanguageTag();
|
|
29
|
+
callback.success(new GetLanguageResult(languageTag));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public void openSettings(@NonNull EmptyCallback callback) {
|
|
33
|
+
Context context = plugin.getContext();
|
|
34
|
+
Uri uri = Uri.fromParts("package", context.getPackageName(), null);
|
|
35
|
+
Intent intent;
|
|
36
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
37
|
+
intent = new Intent(Settings.ACTION_APP_LOCALE_SETTINGS);
|
|
38
|
+
} else {
|
|
39
|
+
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
40
|
+
}
|
|
41
|
+
intent.setData(uri);
|
|
42
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
43
|
+
context.startActivity(intent);
|
|
44
|
+
callback.success();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public void resetLanguage(@NonNull EmptyCallback callback) {
|
|
48
|
+
setApplicationLocales(LocaleListCompat.getEmptyLocaleList());
|
|
49
|
+
callback.success();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public void setLanguage(@NonNull SetLanguageOptions options, @NonNull EmptyCallback callback) {
|
|
53
|
+
setApplicationLocales(LocaleListCompat.forLanguageTags(options.getLanguageTag()));
|
|
54
|
+
callback.success();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private void setApplicationLocales(@NonNull LocaleListCompat locales) {
|
|
58
|
+
Activity activity = plugin.getActivity();
|
|
59
|
+
if (activity == null) {
|
|
60
|
+
AppCompatDelegate.setApplicationLocales(locales);
|
|
61
|
+
} else {
|
|
62
|
+
activity.runOnUiThread(() -> AppCompatDelegate.setApplicationLocales(locales));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
package/android/src/main/java/io/capawesome/capacitorjs/plugins/applanguage/AppLanguagePlugin.java
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.applanguage;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
import com.getcapacitor.Logger;
|
|
6
|
+
import com.getcapacitor.Plugin;
|
|
7
|
+
import com.getcapacitor.PluginCall;
|
|
8
|
+
import com.getcapacitor.PluginMethod;
|
|
9
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.applanguage.classes.CustomException;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.applanguage.classes.options.SetLanguageOptions;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.applanguage.classes.results.GetLanguageResult;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.applanguage.interfaces.EmptyCallback;
|
|
14
|
+
import io.capawesome.capacitorjs.plugins.applanguage.interfaces.NonEmptyResultCallback;
|
|
15
|
+
import io.capawesome.capacitorjs.plugins.applanguage.interfaces.Result;
|
|
16
|
+
|
|
17
|
+
@CapacitorPlugin(name = "AppLanguage")
|
|
18
|
+
public class AppLanguagePlugin extends Plugin {
|
|
19
|
+
|
|
20
|
+
public static final String TAG = "AppLanguage";
|
|
21
|
+
|
|
22
|
+
private static final String ERROR_UNKNOWN_ERROR = "An unknown error occurred.";
|
|
23
|
+
|
|
24
|
+
private AppLanguage implementation;
|
|
25
|
+
|
|
26
|
+
@Override
|
|
27
|
+
public void load() {
|
|
28
|
+
implementation = new AppLanguage(this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@PluginMethod
|
|
32
|
+
public void getLanguage(PluginCall call) {
|
|
33
|
+
try {
|
|
34
|
+
NonEmptyResultCallback<GetLanguageResult> callback = new NonEmptyResultCallback<>() {
|
|
35
|
+
@Override
|
|
36
|
+
public void success(@NonNull GetLanguageResult result) {
|
|
37
|
+
resolveCall(call, result);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@Override
|
|
41
|
+
public void error(@NonNull Exception exception) {
|
|
42
|
+
rejectCall(call, exception);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
implementation.getLanguage(callback);
|
|
46
|
+
} catch (Exception exception) {
|
|
47
|
+
rejectCall(call, exception);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@PluginMethod
|
|
52
|
+
public void openSettings(PluginCall call) {
|
|
53
|
+
try {
|
|
54
|
+
EmptyCallback callback = new EmptyCallback() {
|
|
55
|
+
@Override
|
|
56
|
+
public void success() {
|
|
57
|
+
resolveCall(call);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@Override
|
|
61
|
+
public void error(@NonNull Exception exception) {
|
|
62
|
+
rejectCall(call, exception);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
implementation.openSettings(callback);
|
|
66
|
+
} catch (Exception exception) {
|
|
67
|
+
rejectCall(call, exception);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@PluginMethod
|
|
72
|
+
public void resetLanguage(PluginCall call) {
|
|
73
|
+
try {
|
|
74
|
+
EmptyCallback callback = new EmptyCallback() {
|
|
75
|
+
@Override
|
|
76
|
+
public void success() {
|
|
77
|
+
resolveCall(call);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@Override
|
|
81
|
+
public void error(@NonNull Exception exception) {
|
|
82
|
+
rejectCall(call, exception);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
implementation.resetLanguage(callback);
|
|
86
|
+
} catch (Exception exception) {
|
|
87
|
+
rejectCall(call, exception);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@PluginMethod
|
|
92
|
+
public void setLanguage(PluginCall call) {
|
|
93
|
+
try {
|
|
94
|
+
SetLanguageOptions options = new SetLanguageOptions(call);
|
|
95
|
+
EmptyCallback callback = new EmptyCallback() {
|
|
96
|
+
@Override
|
|
97
|
+
public void success() {
|
|
98
|
+
resolveCall(call);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@Override
|
|
102
|
+
public void error(@NonNull Exception exception) {
|
|
103
|
+
rejectCall(call, exception);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
implementation.setLanguage(options, callback);
|
|
107
|
+
} catch (Exception exception) {
|
|
108
|
+
rejectCall(call, exception);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
|
|
113
|
+
String message = exception.getMessage();
|
|
114
|
+
if (message == null) {
|
|
115
|
+
message = ERROR_UNKNOWN_ERROR;
|
|
116
|
+
}
|
|
117
|
+
String code = null;
|
|
118
|
+
if (exception instanceof CustomException) {
|
|
119
|
+
code = ((CustomException) exception).getCode();
|
|
120
|
+
}
|
|
121
|
+
Logger.error(TAG, message, exception);
|
|
122
|
+
call.reject(message, code);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private void resolveCall(@NonNull PluginCall call) {
|
|
126
|
+
call.resolve();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private void resolveCall(@NonNull PluginCall call, @Nullable Result result) {
|
|
130
|
+
if (result == null) {
|
|
131
|
+
call.resolve();
|
|
132
|
+
} else {
|
|
133
|
+
call.resolve(result.toJSObject());
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.applanguage.classes;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
|
|
6
|
+
public class CustomException extends Exception {
|
|
7
|
+
|
|
8
|
+
@Nullable
|
|
9
|
+
private final String code;
|
|
10
|
+
|
|
11
|
+
public CustomException(@Nullable String code, @NonNull String message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Nullable
|
|
17
|
+
public String getCode() {
|
|
18
|
+
return code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.applanguage.classes;
|
|
2
|
+
|
|
3
|
+
public class CustomExceptions {
|
|
4
|
+
|
|
5
|
+
public static final CustomException LANGUAGE_TAG_INVALID = new CustomException(
|
|
6
|
+
null,
|
|
7
|
+
"languageTag must be a valid BCP 47 language tag."
|
|
8
|
+
);
|
|
9
|
+
public static final CustomException LANGUAGE_TAG_MISSING = new CustomException(null, "languageTag must be provided.");
|
|
10
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.applanguage.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.PluginCall;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.applanguage.classes.CustomExceptions;
|
|
6
|
+
import java.util.Locale;
|
|
7
|
+
|
|
8
|
+
public class SetLanguageOptions {
|
|
9
|
+
|
|
10
|
+
@NonNull
|
|
11
|
+
private final String languageTag;
|
|
12
|
+
|
|
13
|
+
public SetLanguageOptions(@NonNull PluginCall call) throws Exception {
|
|
14
|
+
this.languageTag = SetLanguageOptions.getLanguageTagFromCall(call);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@NonNull
|
|
18
|
+
public String getLanguageTag() {
|
|
19
|
+
return languageTag;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@NonNull
|
|
23
|
+
private static String getLanguageTagFromCall(@NonNull PluginCall call) throws Exception {
|
|
24
|
+
String languageTag = call.getString("languageTag");
|
|
25
|
+
if (languageTag == null || languageTag.trim().isEmpty()) {
|
|
26
|
+
throw CustomExceptions.LANGUAGE_TAG_MISSING;
|
|
27
|
+
}
|
|
28
|
+
if (Locale.forLanguageTag(languageTag).getLanguage().isEmpty()) {
|
|
29
|
+
throw CustomExceptions.LANGUAGE_TAG_INVALID;
|
|
30
|
+
}
|
|
31
|
+
return languageTag;
|
|
32
|
+
}
|
|
33
|
+
}
|