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): add Intl DateTimeFormat, NumberFormat, and Collator #11698

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6f2feae
feat(android): add Intl DateTimeFormat, NumberFormat, and Collator
jquick-axway May 9, 2020
e11659c
Merge branch 'master' into TIMOB-27240
jquick-axway May 9, 2020
63b0645
chore: resolved linting issue in date.addontest.js
jquick-axway May 9, 2020
2d9f921
chore(android): improved engineering/scientific notation support
jquick-axway May 9, 2020
aafa0e9
feat(android): allow collator compare to be passed by reference
jquick-axway Jun 6, 2020
b52f5ae
chore: simplified Intl unit tests
jquick-axway Jun 6, 2020
3e24a82
Merge branch 'master' into TIMOB-27240
jquick-axway Jun 6, 2020
bbf3150
chore(android): create Intl types via functions
jquick-axway Jun 8, 2020
f6e7929
feat(android): optimized collator "case" compares
jquick-axway Jun 8, 2020
f3cb123
Merge branch 'master' into TIMOB-27240
jquick-axway Jun 9, 2020
194b7df
feat(android): added Intl.getCanonicalLocales() method
jquick-axway Jun 12, 2020
b63d390
chore(android): code cleanup of TIMOB-27240
jquick-axway Jun 12, 2020
f399715
doc: supported Intl APIs
jquick-axway Jun 12, 2020
3167d9a
chore(android): resolve JS linting for TIMOB-27240
jquick-axway Jun 12, 2020
6c545ea
docs: edited Intl APIs
jquick-axway Jun 12, 2020
49de7be
Merge branch 'master' into TIMOB-27240
jquick-axway Jun 12, 2020
702befc
docs: update Intl to use fenced code blocks
jquick-axway Jun 12, 2020
385ccb1
chore: updated Intl unit tests
jquick-axway Jun 12, 2020
9fcb533
docs: corrected Intl API doc mistakes
jquick-axway Jun 12, 2020
c1aa085
chore(android): changed Intl constructors handling
jquick-axway Jun 12, 2020
25cb186
feat(android): add locale methods to String type
jquick-axway Jun 13, 2020
d8b63e5
chore(android): updated resolvedOptions() handling
jquick-axway Jun 13, 2020
7e9f48e
Merge branch 'master' into TIMOB-27240
jquick-axway Jun 13, 2020
1e0d48d
chore(android): add numberingSystem support to Intl
jquick-axway Jun 15, 2020
226f5b1
Merge branch 'master' into TIMOB-27240
jquick-axway Jul 15, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2020 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.locale;

import java.text.Collator;
import java.text.Normalizer;
import java.util.Locale;
import java.util.Map;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiPlatformHelper;

/**
* Implements the JavaScript "Intl.Collator" type.
* Used to do localized string compares/sorts.
*/
@Kroll.proxy(creatableInModule = LocaleModule.class)
public class CollatorProxy extends KrollProxy
{
private static final String TAG = "CollatorProxy";

private Collator collator = Collator.getInstance();
private KrollDict resolvedOptions = new KrollDict();
private boolean isStrippingAccents;

@Override
public void handleCreationDict(KrollDict properties)
{
super.handleCreationDict(properties);

// Fetch the optional "locale" and "options" properties.
Locale locale = null;
Map options = null;
if (properties != null) {
Object value = properties.get(TiC.PROPERTY_LOCALE);
if (value instanceof String) {
locale = TiPlatformHelper.getInstance().getLocale((String) value);
}
value = properties.get(TiC.PROPERTY_OPTIONS);
if (value instanceof Map) {
options = (Map) value;
}
}
if (locale == null) {
locale = Locale.getDefault();
}
if (options == null) {
options = new KrollDict();
}

// Determine the collatior setting we need to use.
int strengthId;
int decompositionId = Collator.CANONICAL_DECOMPOSITION;
this.isStrippingAccents = false;
String sensitivityTypeId = TiConvert.toString(options.get("sensitivity"), "variant");
switch (sensitivityTypeId) {
case "accent":
strengthId = Collator.SECONDARY;
break;
case "base":
strengthId = Collator.PRIMARY;
break;
case "case":
strengthId = Collator.IDENTICAL;
this.isStrippingAccents = true;
break;
case "variant":
default:
strengthId = Collator.IDENTICAL;
decompositionId = Collator.NO_DECOMPOSITION;
break;
}

// Configure a new collator.
this.collator = Collator.getInstance(locale);
this.collator.setStrength(strengthId);
this.collator.setDecomposition(decompositionId);

// Store locale and options settings to be returned by this class' resolvedOptions() method.
this.resolvedOptions = new KrollDict();
this.resolvedOptions.putAll(options);
this.resolvedOptions.put(TiC.PROPERTY_LOCALE, locale.toString().replace("_", "-"));
}

@Kroll.method
public int compare(String string1, String string2)
{
// If comparing by "case", replace accented chars with non-accented chars so they'll be treated the same.
if (this.isStrippingAccents) {
string1 = stripAccents(string1);
string2 = stripAccents(string2);
}

// Compare strings using collator.
return this.collator.compare(string1, string2);
}

@Kroll.method
public KrollDict resolvedOptions()
{
return this.resolvedOptions;
}

private String stripAccents(String text)
{
if (text != null) {
text = Normalizer.normalize(text, Normalizer.Form.NFD);
text = text.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
}
return text;
}

@Override
public String getApiName()
{
return "Ti.Locale.Collator";
}
}