-
Notifications
You must be signed in to change notification settings - Fork 319
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
Locale distance formatter #668
Conversation
…way, the special handling for returning the unspanned string can be removed
UNIT_STRINGS.put(UNIT_KILOMETERS, "km"); | ||
UNIT_STRINGS.put(UNIT_METERS, "m"); | ||
UNIT_STRINGS.put(UNIT_MILES, "mi"); | ||
UNIT_STRINGS.put(UNIT_FEET, "ft"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep in mind that we'll eventually need to localize these for certain languages, so if we can make the "km", "m", "mi", "ft" here easily localizable then 👍:
1,2 km (fr-FR) = 1.2公里 (zh-CN) = 1,2 км (ru-RU)
That being said, the only languages on this list (https://www.mapbox.com/api-documentation/#instructions-languages) that don't support the above hardcoded unit strings would be Chinese and Russian, so this isn't a priority we need to worry too much about right now if it turns out to be a lot of work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ericrwolfe good catch, should be easy.
… made DistanceUtils instantiable so that the user doesn't have to pass in fields every time
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great so far. ❤️ the tests you added. Let's run with your idea of using SharedPreferences
for the locale value. This way devs can override it on their side if they need to for whatever reason
@@ -115,6 +121,7 @@ public static Builder builder() { | |||
.enableNotification(true) | |||
.isFromNavigationUi(false) | |||
.isDebugLoggingEnabled(false) | |||
.unitType(NavigationUnitType.TYPE_IMPERIAL); | |||
.unitType(NavigationUnitType.TYPE_IMPERIAL) | |||
.locale(Locale.US); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use Locale.getDefault()
for the builder()
here
@@ -108,6 +108,7 @@ public InstructionView(Context context, @Nullable AttributeSet attrs) { | |||
|
|||
public InstructionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |||
super(context, attrs, defStyleAttr); | |||
locale = context.getResources().getConfiguration().locale; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace with extraction from SharedPreferences
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danesfeder I was thinking just pass the context to DistanceUtils
, and then DistanceUtils
can get the locale from SharedPreferences
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@devotaaabel yeah that sounds good to me - just need to make sure we aren't calling DistanceUtils
somewhere where we don't have Context
. Or have a backup method that can be called without context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danesfeder To localize the strings like Eric said we'll need the context, but it shouldn't be an issue anyway
@@ -51,6 +49,7 @@ public SummaryBottomSheet(Context context, AttributeSet attrs) { | |||
|
|||
public SummaryBottomSheet(Context context, AttributeSet attrs, int defStyleAttr) { | |||
super(context, attrs, defStyleAttr); | |||
locale = context.getResources().getConfiguration().locale; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here with the SharedPref
deal
@@ -118,6 +118,7 @@ private void extractConfiguration(NavigationViewOptions.Builder options) { | |||
MapboxNavigationOptions navigationOptions = MapboxNavigationOptions.builder() | |||
.unitType(preferences.getInt(NavigationConstants.NAVIGATION_VIEW_UNIT_TYPE, | |||
NavigationUnitType.TYPE_IMPERIAL)) | |||
.locale(getResources().getConfiguration().locale) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great call here using the configuration locale here instead of Locale.getDefault()
@@ -118,6 +118,7 @@ private void extractConfiguration(NavigationViewOptions.Builder options) { | |||
MapboxNavigationOptions navigationOptions = MapboxNavigationOptions.builder() | |||
.unitType(preferences.getInt(NavigationConstants.NAVIGATION_VIEW_UNIT_TYPE, | |||
NavigationUnitType.TYPE_IMPERIAL)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be cool to get a default unit type based on the local here (if one isn't set in the preferences) with something like https://stackoverflow.com/questions/4898237/using-locale-settings-to-detect-wheter-to-use-imperial-units.
Current catch is the unit type isn't currently nullable in MapboxNavigationOptions
, so we can switch that to @Nullable
and if someone doesn't set it, we dont set the preference and get the default here when setting the default value for the SharedPreference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, just a few final nit-picks then feel free to merge so you are unblocked for the voice instruction locale PR 🚀
editor.putString(NavigationConstants.NAVIGATION_VIEW_LOCALE_COUNTRY, options.locale().getCountry()); | ||
} | ||
|
||
editor.apply(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's extract the above lines 217-225
into a private method for readability
stepList = new ArrayList<>(); | ||
decimalFormat = new DecimalFormat(NavigationConstants.DECIMAL_FORMAT); | ||
this.context = context; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than store Context
here as a field, would it be okay to create the instance of DistanceUtils
here with the Context
and assign that to a field? Context
can cause leaks sometimes when storing it like this.
return unitType; | ||
} | ||
|
||
// If unit type isn't specified, use default for locale |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great!
Refactored the distance formatter for simplicity, and also added
locale
to options, so that the numbers can be formatted properly for the given locale. I also added a test to verify that the refactored code behavior matched the previous behavior, and to test the locale formatting. Unit type is still specified in the options separately. We could potentially use the locale to pick the unit type if no unit type is specified.Output from tests: