Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): getter/setter for applicationLocales in Android 12 and up #14008

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
2434943
feat(android): via app compat delegate set default night mode
AbdullahFaqeir Mar 19, 2024
2722197
feat(android): ti locale new property applicationLocales
AbdullahFaqeir Mar 19, 2024
db250da
Merge branch 'master' into master
m1ga Mar 19, 2024
c1db1e8
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
AbdullahFaqeir Mar 20, 2024
9158cda
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
AbdullahFaqeir Mar 20, 2024
85e48b1
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
AbdullahFaqeir Mar 20, 2024
94d91bb
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
AbdullahFaqeir Mar 20, 2024
556a721
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
AbdullahFaqeir Mar 20, 2024
940e54f
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
AbdullahFaqeir Mar 20, 2024
2f3b590
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
AbdullahFaqeir Mar 20, 2024
d5e1379
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
AbdullahFaqeir Mar 20, 2024
e6707e1
Update tests/Resources/ti.locale.test.js
AbdullahFaqeir Mar 20, 2024
dcde5d7
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
m1ga Mar 20, 2024
6468f89
Update android/modules/locale/src/java/ti/modules/titanium/locale/Loc…
m1ga Mar 20, 2024
7f17e50
Update Locale.yml for PR #14008
AbdullahFaqeir Mar 21, 2024
81a4f65
Update apidoc/Titanium/Locale/Locale.yml
m1ga Mar 21, 2024
56b4b9a
fix(android): add .idea to .gitignore,
AbdullahFaqeir Mar 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,8 +10,10 @@
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
Expand All @@ -21,8 +23,12 @@
import org.appcelerator.titanium.util.TiPlatformHelper;
import org.appcelerator.titanium.util.TiRHelper;

import android.os.Build;
import android.telephony.PhoneNumberUtils;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.os.LocaleListCompat;
AbdullahFaqeir marked this conversation as resolved.
Show resolved Hide resolved

@Kroll.module
public class LocaleModule extends KrollModule
{
Expand Down Expand Up @@ -51,6 +57,63 @@ public String getCurrentLocale()
return TiPlatformHelper.getInstance().getLocale();
}

@Kroll.setProperty
public void setApplicationLocale(String locales)
{
if (Build.VERSION.SDK_INT >= 33) {
LocaleListCompat appLocale = LocaleListCompat.forLanguageTags(locales);
AppCompatDelegate.setApplicationLocales(appLocale);
} else {
Log.w(TAG, "Only available for Android 33 and later");
}
}

@Kroll.getProperty
public KrollDict[] getApplicationLocales()
{
if (Build.VERSION.SDK_INT < 33) {
Log.w(TAG, "Only available for Android 33 and later");
return null;
}

LocaleListCompat localeListCompat = AppCompatDelegate.getApplicationLocales();
int size = localeListCompat.size();
KrollDict[] locales = new KrollDict[size];
for (int i = 0; i < size; i++) {
Locale locale = localeListCompat.get(i);
if (locale != null) {
KrollDict localeObj = new KrollDict();
localeObj.put("country", locale.getCountry());
localeObj.put("iso3_country", locale.getISO3Country());
localeObj.put("display_country", locale.getDisplayCountry());
localeObj.put("language", locale.getLanguage());
localeObj.put("iso3_language", locale.getISO3Language());
localeObj.put("display_language", locale.getDisplayLanguage());
localeObj.put("variant", locale.getVariant());
localeObj.put("display_variant", locale.getDisplayVariant());
localeObj.put("script", locale.getScript());
localeObj.put("display_script", locale.getDisplayScript());
localeObj.put("display_name", locale.getDisplayName());
localeObj.put("language_tag", locale.toLanguageTag());
Character[] extensionKeys = new Character[locale.getExtensionKeys().size()];
String[] extensions = new String[locale.getExtensionKeys().size()];
Iterator<Character> extensionKeysSize = locale.getExtensionKeys().iterator();
int l = 0;
while (extensionKeysSize.hasNext()) {
extensionKeys[l] = extensionKeysSize.next();
extensions[l] = locale.getExtension(extensionKeys[l]);
l++;
}
localeObj.put("extension_keys", extensionKeys);
localeObj.put("extensions", extensions);
locales[i] = localeObj;
} else {
locales[i] = null;
}
}
return locales;
}

@Kroll.method
public String getCurrencyCode(String localeString)
{
Expand Down
37 changes: 37 additions & 0 deletions apidoc/Titanium/Locale/Locale.yml
Expand Up @@ -217,7 +217,44 @@ properties:
platforms: [android, iphone, ipad, macos]
type: String
permission: read-only

- name: applicationLocale
summary: Set the UI locale for the app.
description: |
Set the UI current active locale for the app.
When changed, it automatically causes the activity to be restarted.
m1ga marked this conversation as resolved.
Show resolved Hide resolved
platforms: [android]
since: "12.4.0"
type: String
examples:
- title: Setting the UI locale of the app
example: |
``` javascript
// This sets the application UI locale.
Ti.Locale.applicationLocale = 'en-US';
// or
Ti.Locale.applicationLocale = 'de';
```

- name: applicationLocales
permission: read-only
summary: Gets the UI locales for the app.
description: |
Returns an array of objects with the current app UI locales.
Note: This is only available on Android 33 and up.
platforms: [android]
since: "12.4.0"
type: Array
examples:
- title: Getting UI locale(s) of the app
example: |
``` javascript
// This returns an array of objects for each locale defined for the application's UI.
let appLocales = Ti.Locale.applicationLocales;
console.log(appLocales[0].language)
console.log(appLocales);
```

- name: currentLanguage
summary: Language of the current system locale, as an ISO 2-letter code.
description: |
Expand Down
17 changes: 17 additions & 0 deletions tests/Resources/ti.locale.test.js
Expand Up @@ -44,6 +44,12 @@ describe('Titanium.Locale', () => {
});
});

describe.android('.applicationLocales', () => {
it('is a Array', () => {
should(Ti.Locale).have.a.property('applicationLocales').which.is.an.Array();
});
});

describe('.currentLanguage', () => {
it('is a String', () => {
should(Ti.Locale).have.a.readOnlyProperty('currentLanguage').which.is.a.String();
Expand Down Expand Up @@ -278,6 +284,17 @@ describe('Titanium.Locale', () => {
});
});

describe.android('#set applicationLocales(String)', () => {
it('has a setter', () => {
should(Ti.Locale).have.a.setter('applicationLocales');
});

it('changes .applicationLocales', () => {
Ti.Locale.applicationLocales = 'ar';
should(Ti.Locale.applicationLocales[0].language).eql('ar');
});
});

describe('#set language(String)', () => {
it('has a setter', () => {
should(Ti.Locale).have.a.setter('language');
Expand Down