@capawesome/capacitor-localization 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/CapawesomeCapacitorLocalization.podspec +17 -0
- package/LICENSE +21 -0
- package/Package.swift +28 -0
- package/README.md +193 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/Localization.java +108 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/LocalizationPlugin.java +85 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/classes/results/GetLocalesResult.java +29 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/classes/results/GetSettingsResult.java +31 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/classes/results/LocaleResult.java +75 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +333 -0
- package/dist/esm/definitions.d.ts +157 -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 +12 -0
- package/dist/esm/web.js +75 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +89 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +92 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Classes/Results/GetLocalesResult.swift +17 -0
- package/ios/Plugin/Classes/Results/GetSettingsResult.swift +22 -0
- package/ios/Plugin/Classes/Results/LocaleResult.swift +50 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Localization.swift +92 -0
- package/ios/Plugin/LocalizationPlugin.swift +53 -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 = 'CapawesomeCapacitorLocalization'
|
|
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: "CapawesomeCapacitorLocalization",
|
|
6
|
+
platforms: [.iOS(.v15)],
|
|
7
|
+
products: [
|
|
8
|
+
.library(
|
|
9
|
+
name: "CapawesomeCapacitorLocalization",
|
|
10
|
+
targets: ["LocalizationPlugin"])
|
|
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: "LocalizationPlugin",
|
|
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: "LocalizationPluginTests",
|
|
25
|
+
dependencies: ["LocalizationPlugin"],
|
|
26
|
+
path: "ios/PluginTests")
|
|
27
|
+
]
|
|
28
|
+
)
|
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# @capawesome/capacitor-localization
|
|
2
|
+
|
|
3
|
+
Capacitor plugin to read the user's localization preferences, such as preferred locales, time zone, and regional formatting settings.
|
|
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
|
+
- 🌍 **Preferred locales**: Read the user's preferred locales, ordered by preference.
|
|
14
|
+
- 💱 **Regional formatting**: Read currency, decimal, and grouping separators per locale.
|
|
15
|
+
- ↔️ **Text direction**: Detect whether a locale is written left-to-right or right-to-left.
|
|
16
|
+
- 📏 **Measurement system**: Detect the metric, US, or UK measurement system.
|
|
17
|
+
- 🕐 **Settings**: Read the time zone, clock format, and first day of the week.
|
|
18
|
+
- 🖥️ **Cross-platform**: Full support for Android, iOS, and Web.
|
|
19
|
+
- 📦 **CocoaPods & SPM**: Supports CocoaPods and Swift Package Manager for iOS.
|
|
20
|
+
- 🔁 **Up-to-date**: Always supports the latest Capacitor version.
|
|
21
|
+
|
|
22
|
+
Missing a feature? Just [open an issue](https://github.com/capawesome-team/capacitor-plugins/issues) and we'll take a look!
|
|
23
|
+
|
|
24
|
+
## Newsletter
|
|
25
|
+
|
|
26
|
+
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/).
|
|
27
|
+
|
|
28
|
+
## Compatibility
|
|
29
|
+
|
|
30
|
+
| Plugin Version | Capacitor Version | Status |
|
|
31
|
+
| -------------- | ----------------- | -------------- |
|
|
32
|
+
| 0.x.x | >=8.x.x | Active support |
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
You can use our **AI-Assisted Setup** to install the plugin.
|
|
37
|
+
Add the [Capawesome Skills](https://github.com/capawesome-team/skills) to your AI tool using the following command:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npx skills add capawesome-team/skills --skill capacitor-plugins
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then use the following prompt:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome/capacitor-localization` plugin in my project.
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
If you prefer **Manual Setup**, install the plugin by running the following commands and follow the platform-specific instructions below:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install @capawesome/capacitor-localization
|
|
53
|
+
npx cap sync
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
No additional permissions or configuration are required on any platform.
|
|
57
|
+
|
|
58
|
+
## Configuration
|
|
59
|
+
|
|
60
|
+
No configuration required for this plugin.
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { Localization } from '@capawesome/capacitor-localization';
|
|
66
|
+
|
|
67
|
+
const getLocales = async () => {
|
|
68
|
+
const { locales } = await Localization.getLocales();
|
|
69
|
+
return locales;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const getSettings = async () => {
|
|
73
|
+
const settings = await Localization.getSettings();
|
|
74
|
+
return settings;
|
|
75
|
+
};
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## API
|
|
79
|
+
|
|
80
|
+
<docgen-index>
|
|
81
|
+
|
|
82
|
+
* [`getLocales()`](#getlocales)
|
|
83
|
+
* [`getSettings()`](#getsettings)
|
|
84
|
+
* [Interfaces](#interfaces)
|
|
85
|
+
* [Type Aliases](#type-aliases)
|
|
86
|
+
|
|
87
|
+
</docgen-index>
|
|
88
|
+
|
|
89
|
+
<docgen-api>
|
|
90
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
91
|
+
|
|
92
|
+
### getLocales()
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
getLocales() => Promise<GetLocalesResult>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Get the user's preferred locales, ordered by preference.
|
|
99
|
+
|
|
100
|
+
The first entry is the most preferred locale.
|
|
101
|
+
|
|
102
|
+
**Returns:** <code>Promise<<a href="#getlocalesresult">GetLocalesResult</a>></code>
|
|
103
|
+
|
|
104
|
+
**Since:** 0.1.0
|
|
105
|
+
|
|
106
|
+
--------------------
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
### getSettings()
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
getSettings() => Promise<GetSettingsResult>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Get the user's regional formatting settings.
|
|
116
|
+
|
|
117
|
+
**Returns:** <code>Promise<<a href="#getsettingsresult">GetSettingsResult</a>></code>
|
|
118
|
+
|
|
119
|
+
**Since:** 0.1.0
|
|
120
|
+
|
|
121
|
+
--------------------
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
### Interfaces
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
#### GetLocalesResult
|
|
128
|
+
|
|
129
|
+
| Prop | Type | Description | Since |
|
|
130
|
+
| ------------- | --------------------- | -------------------------------------------------------------------------------------------------- | ----- |
|
|
131
|
+
| **`locales`** | <code>Locale[]</code> | The user's preferred locales, ordered by preference. The first entry is the most preferred locale. | 0.1.0 |
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
#### Locale
|
|
135
|
+
|
|
136
|
+
| Prop | Type | Description | Since |
|
|
137
|
+
| ----------------------- | ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
|
|
138
|
+
| **`currencyCode`** | <code>string \| null</code> | The ISO 4217 currency code of the locale. Returns `null` if the currency cannot be determined. | 0.1.0 |
|
|
139
|
+
| **`currencySymbol`** | <code>string \| null</code> | The currency symbol of the locale. Returns `null` if the currency symbol cannot be determined. | 0.1.0 |
|
|
140
|
+
| **`decimalSeparator`** | <code>string \| null</code> | The character used to separate the integer part from the fractional part of a number. Returns `null` if the separator cannot be determined. | 0.1.0 |
|
|
141
|
+
| **`groupingSeparator`** | <code>string \| null</code> | The character used to group digits of the integer part of a number. Returns `null` if the separator cannot be determined. | 0.1.0 |
|
|
142
|
+
| **`languageCode`** | <code>string</code> | The ISO 639 language code of the locale. | 0.1.0 |
|
|
143
|
+
| **`languageTag`** | <code>string</code> | The BCP 47 language tag of the locale. | 0.1.0 |
|
|
144
|
+
| **`measurementSystem`** | <code><a href="#measurementsystem">MeasurementSystem</a> \| null</code> | The measurement system used by the locale. Returns `null` if the measurement system cannot be determined. | 0.1.0 |
|
|
145
|
+
| **`regionCode`** | <code>string \| null</code> | The ISO 3166 region code of the locale. Returns `null` if the region cannot be determined. | 0.1.0 |
|
|
146
|
+
| **`textDirection`** | <code><a href="#textdirection">TextDirection</a></code> | The writing direction of the locale's language. | 0.1.0 |
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
#### GetSettingsResult
|
|
150
|
+
|
|
151
|
+
| Prop | Type | Description | Since |
|
|
152
|
+
| --------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ----- |
|
|
153
|
+
| **`firstDayOfWeek`** | <code>number</code> | The first day of the week as configured by the user. The value follows the ISO 8601 convention where `1` is Monday and `7` is Sunday. | 0.1.0 |
|
|
154
|
+
| **`timeZone`** | <code>string</code> | The identifier of the user's current time zone. | 0.1.0 |
|
|
155
|
+
| **`uses24HourClock`** | <code>boolean</code> | Whether the user prefers a 24-hour clock over a 12-hour clock. | 0.1.0 |
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
### Type Aliases
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
#### MeasurementSystem
|
|
162
|
+
|
|
163
|
+
The measurement system used by a locale.
|
|
164
|
+
|
|
165
|
+
- `metric`: The metric system (e.g. meters, kilograms).
|
|
166
|
+
- `us`: The United States customary system (e.g. feet, pounds).
|
|
167
|
+
- `uk`: The United Kingdom imperial system.
|
|
168
|
+
|
|
169
|
+
<code>'metric' | 'us' | 'uk'</code>
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
#### TextDirection
|
|
173
|
+
|
|
174
|
+
The writing direction of a language.
|
|
175
|
+
|
|
176
|
+
- `ltr`: Left-to-right (e.g. English, German).
|
|
177
|
+
- `rtl`: Right-to-left (e.g. Arabic, Hebrew).
|
|
178
|
+
|
|
179
|
+
<code>'ltr' | 'rtl'</code>
|
|
180
|
+
|
|
181
|
+
</docgen-api>
|
|
182
|
+
|
|
183
|
+
## Platform Support
|
|
184
|
+
|
|
185
|
+
The plugin returns `null` for any field that a platform cannot determine. On the **Web** platform, `currencyCode`, `currencySymbol`, and `measurementSystem` are always `null`, because the browser does not expose this information.
|
|
186
|
+
|
|
187
|
+
## Changelog
|
|
188
|
+
|
|
189
|
+
See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/localization/CHANGELOG.md).
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/localization/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.localization"
|
|
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
|
+
}
|
package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/Localization.java
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.localization;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.os.LocaleList;
|
|
5
|
+
import android.text.TextUtils;
|
|
6
|
+
import android.text.format.DateFormat;
|
|
7
|
+
import android.view.View;
|
|
8
|
+
import androidx.annotation.NonNull;
|
|
9
|
+
import androidx.annotation.Nullable;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.localization.classes.results.GetLocalesResult;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.localization.classes.results.GetSettingsResult;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.localization.classes.results.LocaleResult;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.localization.interfaces.NonEmptyResultCallback;
|
|
14
|
+
import java.text.DecimalFormatSymbols;
|
|
15
|
+
import java.util.ArrayList;
|
|
16
|
+
import java.util.Calendar;
|
|
17
|
+
import java.util.Currency;
|
|
18
|
+
import java.util.List;
|
|
19
|
+
import java.util.Locale;
|
|
20
|
+
import java.util.TimeZone;
|
|
21
|
+
|
|
22
|
+
public class Localization {
|
|
23
|
+
|
|
24
|
+
@NonNull
|
|
25
|
+
private final Context context;
|
|
26
|
+
|
|
27
|
+
public Localization(@NonNull Context context) {
|
|
28
|
+
this.context = context;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public void getLocales(@NonNull NonEmptyResultCallback<GetLocalesResult> callback) {
|
|
32
|
+
LocaleList localeList = LocaleList.getDefault();
|
|
33
|
+
List<LocaleResult> locales = new ArrayList<>();
|
|
34
|
+
for (int i = 0; i < localeList.size(); i++) {
|
|
35
|
+
locales.add(createLocaleResult(localeList.get(i)));
|
|
36
|
+
}
|
|
37
|
+
callback.success(new GetLocalesResult(locales));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public void getSettings(@NonNull NonEmptyResultCallback<GetSettingsResult> callback) {
|
|
41
|
+
String timeZone = TimeZone.getDefault().getID();
|
|
42
|
+
boolean uses24HourClock = DateFormat.is24HourFormat(context);
|
|
43
|
+
int firstDayOfWeek = convertToIsoDayOfWeek(Calendar.getInstance().getFirstDayOfWeek());
|
|
44
|
+
callback.success(new GetSettingsResult(timeZone, uses24HourClock, firstDayOfWeek));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private int convertToIsoDayOfWeek(int calendarDayOfWeek) {
|
|
48
|
+
return ((calendarDayOfWeek + 5) % 7) + 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@NonNull
|
|
52
|
+
private LocaleResult createLocaleResult(@NonNull Locale locale) {
|
|
53
|
+
String languageTag = locale.toLanguageTag();
|
|
54
|
+
String languageCode = locale.getLanguage();
|
|
55
|
+
String regionCode = locale.getCountry().isEmpty() ? null : locale.getCountry();
|
|
56
|
+
Currency currency = resolveCurrency(locale);
|
|
57
|
+
String currencyCode = currency == null ? null : currency.getCurrencyCode();
|
|
58
|
+
String currencySymbol = currency == null ? null : currency.getSymbol(locale);
|
|
59
|
+
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
|
|
60
|
+
String decimalSeparator = String.valueOf(symbols.getDecimalSeparator());
|
|
61
|
+
String groupingSeparator = String.valueOf(symbols.getGroupingSeparator());
|
|
62
|
+
String textDirection = resolveTextDirection(locale);
|
|
63
|
+
String measurementSystem = resolveMeasurementSystem(regionCode);
|
|
64
|
+
return new LocaleResult(
|
|
65
|
+
languageTag,
|
|
66
|
+
languageCode,
|
|
67
|
+
regionCode,
|
|
68
|
+
currencyCode,
|
|
69
|
+
currencySymbol,
|
|
70
|
+
decimalSeparator,
|
|
71
|
+
groupingSeparator,
|
|
72
|
+
textDirection,
|
|
73
|
+
measurementSystem
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@Nullable
|
|
78
|
+
private Currency resolveCurrency(@NonNull Locale locale) {
|
|
79
|
+
try {
|
|
80
|
+
return Currency.getInstance(locale);
|
|
81
|
+
} catch (IllegalArgumentException exception) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Nullable
|
|
87
|
+
private String resolveMeasurementSystem(@Nullable String regionCode) {
|
|
88
|
+
if (regionCode == null) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
switch (regionCode.toUpperCase(Locale.ROOT)) {
|
|
92
|
+
case "US":
|
|
93
|
+
case "LR":
|
|
94
|
+
case "MM":
|
|
95
|
+
return "us";
|
|
96
|
+
case "GB":
|
|
97
|
+
return "uk";
|
|
98
|
+
default:
|
|
99
|
+
return "metric";
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
@NonNull
|
|
104
|
+
private String resolveTextDirection(@NonNull Locale locale) {
|
|
105
|
+
int layoutDirection = TextUtils.getLayoutDirectionFromLocale(locale);
|
|
106
|
+
return layoutDirection == View.LAYOUT_DIRECTION_RTL ? "rtl" : "ltr";
|
|
107
|
+
}
|
|
108
|
+
}
|
package/android/src/main/java/io/capawesome/capacitorjs/plugins/localization/LocalizationPlugin.java
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.localization;
|
|
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.localization.classes.results.GetLocalesResult;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.localization.classes.results.GetSettingsResult;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.localization.interfaces.NonEmptyResultCallback;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.localization.interfaces.Result;
|
|
14
|
+
|
|
15
|
+
@CapacitorPlugin(name = "Localization")
|
|
16
|
+
public class LocalizationPlugin extends Plugin {
|
|
17
|
+
|
|
18
|
+
public static final String TAG = "Localization";
|
|
19
|
+
|
|
20
|
+
private static final String ERROR_UNKNOWN_ERROR = "An unknown error occurred.";
|
|
21
|
+
|
|
22
|
+
private Localization implementation;
|
|
23
|
+
|
|
24
|
+
@Override
|
|
25
|
+
public void load() {
|
|
26
|
+
implementation = new Localization(getContext());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@PluginMethod
|
|
30
|
+
public void getLocales(PluginCall call) {
|
|
31
|
+
try {
|
|
32
|
+
NonEmptyResultCallback<GetLocalesResult> callback = new NonEmptyResultCallback<>() {
|
|
33
|
+
@Override
|
|
34
|
+
public void success(@NonNull GetLocalesResult result) {
|
|
35
|
+
resolveCall(call, result);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Override
|
|
39
|
+
public void error(@NonNull Exception exception) {
|
|
40
|
+
rejectCall(call, exception);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
implementation.getLocales(callback);
|
|
44
|
+
} catch (Exception exception) {
|
|
45
|
+
rejectCall(call, exception);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@PluginMethod
|
|
50
|
+
public void getSettings(PluginCall call) {
|
|
51
|
+
try {
|
|
52
|
+
NonEmptyResultCallback<GetSettingsResult> callback = new NonEmptyResultCallback<>() {
|
|
53
|
+
@Override
|
|
54
|
+
public void success(@NonNull GetSettingsResult result) {
|
|
55
|
+
resolveCall(call, result);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Override
|
|
59
|
+
public void error(@NonNull Exception exception) {
|
|
60
|
+
rejectCall(call, exception);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
implementation.getSettings(callback);
|
|
64
|
+
} catch (Exception exception) {
|
|
65
|
+
rejectCall(call, exception);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
|
|
70
|
+
String message = exception.getMessage();
|
|
71
|
+
if (message == null) {
|
|
72
|
+
message = ERROR_UNKNOWN_ERROR;
|
|
73
|
+
}
|
|
74
|
+
Logger.error(TAG, message, exception);
|
|
75
|
+
call.reject(message);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private void resolveCall(@NonNull PluginCall call, @Nullable Result result) {
|
|
79
|
+
if (result == null) {
|
|
80
|
+
call.resolve();
|
|
81
|
+
} else {
|
|
82
|
+
call.resolve(result.toJSObject());
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.localization.classes.results;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSArray;
|
|
5
|
+
import com.getcapacitor.JSObject;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.localization.interfaces.Result;
|
|
7
|
+
import java.util.List;
|
|
8
|
+
|
|
9
|
+
public class GetLocalesResult implements Result {
|
|
10
|
+
|
|
11
|
+
@NonNull
|
|
12
|
+
private final List<LocaleResult> locales;
|
|
13
|
+
|
|
14
|
+
public GetLocalesResult(@NonNull List<LocaleResult> locales) {
|
|
15
|
+
this.locales = locales;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Override
|
|
19
|
+
@NonNull
|
|
20
|
+
public JSObject toJSObject() {
|
|
21
|
+
JSArray localesArray = new JSArray();
|
|
22
|
+
for (LocaleResult locale : locales) {
|
|
23
|
+
localesArray.put(locale.toJSObject());
|
|
24
|
+
}
|
|
25
|
+
JSObject result = new JSObject();
|
|
26
|
+
result.put("locales", localesArray);
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.localization.classes.results;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.localization.interfaces.Result;
|
|
6
|
+
|
|
7
|
+
public class GetSettingsResult implements Result {
|
|
8
|
+
|
|
9
|
+
private final int firstDayOfWeek;
|
|
10
|
+
|
|
11
|
+
@NonNull
|
|
12
|
+
private final String timeZone;
|
|
13
|
+
|
|
14
|
+
private final boolean uses24HourClock;
|
|
15
|
+
|
|
16
|
+
public GetSettingsResult(@NonNull String timeZone, boolean uses24HourClock, int firstDayOfWeek) {
|
|
17
|
+
this.timeZone = timeZone;
|
|
18
|
+
this.uses24HourClock = uses24HourClock;
|
|
19
|
+
this.firstDayOfWeek = firstDayOfWeek;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@Override
|
|
23
|
+
@NonNull
|
|
24
|
+
public JSObject toJSObject() {
|
|
25
|
+
JSObject result = new JSObject();
|
|
26
|
+
result.put("firstDayOfWeek", firstDayOfWeek);
|
|
27
|
+
result.put("timeZone", timeZone);
|
|
28
|
+
result.put("uses24HourClock", uses24HourClock);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.localization.classes.results;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
import com.getcapacitor.JSObject;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.localization.interfaces.Result;
|
|
7
|
+
import org.json.JSONObject;
|
|
8
|
+
|
|
9
|
+
public class LocaleResult implements Result {
|
|
10
|
+
|
|
11
|
+
@Nullable
|
|
12
|
+
private final String currencyCode;
|
|
13
|
+
|
|
14
|
+
@Nullable
|
|
15
|
+
private final String currencySymbol;
|
|
16
|
+
|
|
17
|
+
@Nullable
|
|
18
|
+
private final String decimalSeparator;
|
|
19
|
+
|
|
20
|
+
@Nullable
|
|
21
|
+
private final String groupingSeparator;
|
|
22
|
+
|
|
23
|
+
@NonNull
|
|
24
|
+
private final String languageCode;
|
|
25
|
+
|
|
26
|
+
@NonNull
|
|
27
|
+
private final String languageTag;
|
|
28
|
+
|
|
29
|
+
@Nullable
|
|
30
|
+
private final String measurementSystem;
|
|
31
|
+
|
|
32
|
+
@Nullable
|
|
33
|
+
private final String regionCode;
|
|
34
|
+
|
|
35
|
+
@NonNull
|
|
36
|
+
private final String textDirection;
|
|
37
|
+
|
|
38
|
+
public LocaleResult(
|
|
39
|
+
@NonNull String languageTag,
|
|
40
|
+
@NonNull String languageCode,
|
|
41
|
+
@Nullable String regionCode,
|
|
42
|
+
@Nullable String currencyCode,
|
|
43
|
+
@Nullable String currencySymbol,
|
|
44
|
+
@Nullable String decimalSeparator,
|
|
45
|
+
@Nullable String groupingSeparator,
|
|
46
|
+
@NonNull String textDirection,
|
|
47
|
+
@Nullable String measurementSystem
|
|
48
|
+
) {
|
|
49
|
+
this.languageTag = languageTag;
|
|
50
|
+
this.languageCode = languageCode;
|
|
51
|
+
this.regionCode = regionCode;
|
|
52
|
+
this.currencyCode = currencyCode;
|
|
53
|
+
this.currencySymbol = currencySymbol;
|
|
54
|
+
this.decimalSeparator = decimalSeparator;
|
|
55
|
+
this.groupingSeparator = groupingSeparator;
|
|
56
|
+
this.textDirection = textDirection;
|
|
57
|
+
this.measurementSystem = measurementSystem;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@Override
|
|
61
|
+
@NonNull
|
|
62
|
+
public JSObject toJSObject() {
|
|
63
|
+
JSObject result = new JSObject();
|
|
64
|
+
result.put("currencyCode", currencyCode == null ? JSONObject.NULL : currencyCode);
|
|
65
|
+
result.put("currencySymbol", currencySymbol == null ? JSONObject.NULL : currencySymbol);
|
|
66
|
+
result.put("decimalSeparator", decimalSeparator == null ? JSONObject.NULL : decimalSeparator);
|
|
67
|
+
result.put("groupingSeparator", groupingSeparator == null ? JSONObject.NULL : groupingSeparator);
|
|
68
|
+
result.put("languageCode", languageCode);
|
|
69
|
+
result.put("languageTag", languageTag);
|
|
70
|
+
result.put("measurementSystem", measurementSystem == null ? JSONObject.NULL : measurementSystem);
|
|
71
|
+
result.put("regionCode", regionCode == null ? JSONObject.NULL : regionCode);
|
|
72
|
+
result.put("textDirection", textDirection);
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
}
|