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

Conversation

AbdullahFaqeir
Copy link

@AbdullahFaqeir AbdullahFaqeir commented Mar 19, 2024

feat(android): via app compat delegate set application locales.
feat(android): via app compat delegate get application locales.
feat(android): via app compat delegate get default night mode
feat(android): via app compat delegate check if compat vector from resources enabled
feat(android): via app compat delegate set compat vector from recourses enabled or not.

Description:

This PR was raised from an issue appeared on android level 33 and later which caused Ti.Locale.setLanguage() not to work properly, which caused random pages not to be translated unless you recall Ti.Locale.setLanguage() on each page.

See here

Nonetheless, I've ported the rest of static methods from AppCompatDelegate.

edited by Michael: for a test code check #14008 (comment)

feat(android): via app compat delegate get default night mode
feat(android): via app compat delegate check if compat vector from resources enabled
feat(android): via app compat delegate set compat vector from recourses enabled or not.
feat(android): via app compat delegate set application locales.
feat(android): via app compat delegate get application locales.
@m1ga
Copy link
Contributor

m1ga commented Mar 19, 2024

Thanks for the PR! A few remarks:

  • please import androidx.appcompat.app.AppCompatDelegate; so you don't need to fully put the path in there all the time as you can see in other files https://github.com/search?q=repo%3Atidev%2Ftitanium-sdk%20AppCompatDelegate&type=code

  • use a different file to put your code in. E.g. the normal setLanguage is here:

    public void setLanguage(String language)
    {
    try {
    String[] parts = language.split("-");
    Locale locale = null;
    if (parts.length > 1) {
    locale = new Locale(parts[0], parts[1]);
    } else {
    locale = new Locale(parts[0]);
    }
    TiLocaleManager.setLocale(locale);
    } catch (Exception e) {
    Log.e(TAG, "Error trying to set language '" + language + "':", e);
    }
    }
    so you could duplicate that and adjust it for setAppLanguage . That way you can use Ti.Locale.appLanguage = "" later on. Make it just a setProperty/getProperty.

  • keep it for the locales parts only. E.g. isCompatVectorFromResourcesEnabled shouldn't be needed at all as it checks for <21 and the minSDK of Ti is 21. If it's only the locale stuff it makes it easier to test 😉

  • getDefaultNightMode is in Ti.UI.overrideUserInterfaceStyle already.

  • add some test code for the reviewer

  • update apidoc/Titanium/Locale/Locale.yml and add the properties

Copy link
Contributor

@m1ga m1ga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some code suggestions you can add in github with some minor code changes to adjust it to the rest of the SDK. It looks like much because I've added and if around it 😄

Ti.Locale.applicationLocales = "de";
console.log(Ti.Locale.applicationLocales);

as some demo code

@m1ga
Copy link
Contributor

m1ga commented Mar 20, 2024

@AbdullahFaqeir you have to click the Commit suggestions buttons to apply the code or apply it locally and do another push. The "Resolve conversation" button will only close it but not apply it :)

AbdullahFaqeir and others added 8 commits March 20, 2024 12:13
…aleModule.java

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
…aleModule.java

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
…aleModule.java

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
…aleModule.java

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
…aleModule.java

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
…aleModule.java

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
…aleModule.java

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
…aleModule.java

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
@AbdullahFaqeir
Copy link
Author

@AbdullahFaqeir you have to click the Commit suggestions buttons to apply the code or apply it locally and do another push. The "Resolve conversation" button will only close it but not apply it :)

Done :)

Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
@m1ga
Copy link
Contributor

m1ga commented Mar 20, 2024

It works but it's a bit tricky to use as it will restart the activity. I had an endless loop after I've added two setters below each other.

Here is a working demo

var currentLang = Ti.App.Properties.getString("lang", "de");

const win = Ti.UI.createWindow({});
const btn = Ti.UI.createButton({title: "switch lang"});
const lbl1 = Ti.UI.createLabel({top: 10,text: currentLang});
const lbl = Ti.UI.createLabel({top: 30,text: L("word")});
win.add([btn, lbl, lbl1]);

btn.addEventListener("click", function() {
  currentLang = (currentLang == "de") ? "en" : "de";
  Ti.App.Properties.setString("lang", currentLang);
	Ti.Locale.applicationLocale = currentLang;
})

win.addEventListener("open", function() {
  // is called when you switch the language
  //
  currentLang = Ti.App.Properties.getString("lang", "de");
  console.log(Ti.Locale.applicationLocales)
  lbl1.text = currentLang;
  lbl.text = L("word");
})

win.open();

app/i18n/de/strings.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
	<string name="word">DE</string>
</resources>

app/i18n/en/strings.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
	<string name="word">EN</string>
</resources>

one last step @AbdullahFaqeir :
Edit https://github.com/tidev/titanium-sdk/blob/master/apidoc/Titanium/Locale/Locale.yml and add your

- name: applicationLocale
...

before: https://github.com/tidev/titanium-sdk/blob/master/apidoc/Titanium/Locale/Locale.yml#L221-L227

Summary something like Sets the UI locale for the app
Description: You can set the UI locales using a value such as "en" or "fr". See the [ISO 639-1](http://en.wikipedia.org/wiki/ISO_639-1) section of wikipedia for reference. Note: This is only available on Android 33 and up. It will also restart your current activity when you switch your language, the open event is called again.

and this:

platforms: [android]
since: "12.4.0"

and

- name: applicationLocales
- permission: read-only
- platforms: [android]
- since: "12.4.0"
...

Summary something like 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.

NOTE:
While writing that I figured out it should be applicationLocale (singular) for the setter as you set only one language and applicationLocales (plural) for the getter as it might return multiple languages and and objects. I've adjusted the code already and my example

@m1ga m1ga changed the title feat(android): via app compat delegate set application locales. feat(android): getter/setter for applicationLocales in Android 12 and up Mar 20, 2024
m1ga and others added 2 commits March 20, 2024 11:10
Added documentation for applicationLocale and applicationLocales which are only available on Android level 33 and up.
@m1ga m1ga self-requested a review March 21, 2024 08:57
Copy link
Contributor

@m1ga m1ga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Thank you for the work

chore(android): let there be gradle 8.
@m1ga
Copy link
Contributor

m1ga commented Mar 21, 2024

@AbdullahFaqeir careful: you've pushed the gradle part into this PR. Please revert the last changes.
You could also close it and start a fresh one in a different branch, not the master branch. Then you can work on two PRs at the same time.

@AbdullahFaqeir
Copy link
Author

@AbdullahFaqeir careful: you've pushed the gradle part into this PR. Please revert the last changes. You could also close it and start a fresh one in a different branch, not the master branch. Then you can work on two PRs at the same time.

Yeah I did that by mistake, I forgot to close this PR, I'll fix it rightaway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants