From aa1b5bce5f7efdcdf393ef8a6ed24d7e51a1641b Mon Sep 17 00:00:00 2001 From: "Rory Abraham (via MelvinBot)" Date: Mon, 24 Aug 2026 07:37:04 +0000 Subject: [PATCH 1/6] Localize Mapbox map labels to the user's preferred app locale Co-authored-by: Rory Abraham --- src/components/MapView/MapView.tsx | 9 +++++++- src/components/MapView/MapViewImpl.web.tsx | 24 +++++++++++++++++++++- src/components/MapView/utils.ts | 21 +++++++++++++++++++ tests/unit/MapViewUtilsTest.ts | 18 ++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/components/MapView/MapView.tsx b/src/components/MapView/MapView.tsx index 3e2eb57f87ca..08bdfa2f990c 100644 --- a/src/components/MapView/MapView.tsx +++ b/src/components/MapView/MapView.tsx @@ -57,7 +57,7 @@ function MapView({ const [userLocation] = useOnyx(ONYXKEYS.USER_LOCATION); const navigation = useNavigation(); const {isOffline} = useNetwork(); - const {translate} = useLocalize(); + const {translate, preferredLocale} = useLocalize(); const styles = useThemeStyles(); const theme = useTheme(); const expensifyIcons = useMemoizedLazyExpensifyIcons(['Crosshair', 'MapCurrentLocation']); @@ -261,11 +261,18 @@ function MapView({ const initCenterCoordinate = useMemo(() => (interactive ? centerCoordinate : undefined), [interactive, centerCoordinate]); const initBounds = useMemo(() => (interactive ? undefined : waypointsBounds), [interactive, waypointsBounds]); + // Localize the map labels to the user's preferred app locale so they match the rest of the app. + const localizeLabels = useMemo<{locale: string} | undefined>(() => { + const language = utils.getMapboxLanguage(preferredLocale); + return language ? {locale: language} : undefined; + }, [preferredLocale]); + return !isOffline && isAccessTokenReady && !!defaultSettings ? ( setUserInteractedWithMap(true)} diff --git a/src/components/MapView/MapViewImpl.web.tsx b/src/components/MapView/MapViewImpl.web.tsx index 1ac3081219bf..cbaafd53df07 100644 --- a/src/components/MapView/MapViewImpl.web.tsx +++ b/src/components/MapView/MapViewImpl.web.tsx @@ -65,7 +65,7 @@ function MapViewImpl({ const [userLocation] = useOnyx(ONYXKEYS.USER_LOCATION); const {isOffline} = useNetwork(); - const {translate} = useLocalize(); + const {translate, preferredLocale} = useLocalize(); const theme = useTheme(); const styles = useThemeStyles(); @@ -198,6 +198,28 @@ function MapViewImpl({ }; }, [mapRef]); + // cspell:ignore styledata + // Keep the map labels in the user's preferred app locale, reapplying whenever the map or locale changes. + useEffect(() => { + if (!mapRef) { + return; + } + + const map = mapRef.getMap(); + const applyLanguage = () => map.setLanguage(utils.getMapboxLanguage(preferredLocale)); + + if (map.isStyleLoaded()) { + applyLanguage(); + return; + } + + // The style must be loaded before labels can be localized, so defer until it is ready. + map.once('styledata', applyLanguage); + return () => { + map.off('styledata', applyLanguage); + }; + }, [mapRef, preferredLocale]); + useImperativeHandle( ref, () => ({ diff --git a/src/components/MapView/utils.ts b/src/components/MapView/utils.ts index 72c834bf444f..873b0cf87b6c 100644 --- a/src/components/MapView/utils.ts +++ b/src/components/MapView/utils.ts @@ -1,5 +1,25 @@ +import {LOCALES} from '@src/CONST/LOCALES'; +import type {Locale} from '@src/CONST/LOCALES'; import type {AlternateDirection, Coordinate} from './MapViewTypes'; +/** App locales whose value isn't already the BCP-47 code Mapbox expects for label localization. */ +const LOCALE_TO_MAPBOX_LANGUAGE: Partial> = { + [LOCALES.PT_BR]: 'pt', + [LOCALES.ZH_HANS]: 'zh-Hans', +}; + +/** + * Maps an app locale to the BCP-47 language code Mapbox uses to localize map labels. + * Most app locales are already valid Mapbox codes, so only a couple need remapping. + * Unsupported codes fall back to each label's local language on the Mapbox side. + */ +function getMapboxLanguage(locale: Locale | undefined): string | undefined { + if (!locale) { + return undefined; + } + return LOCALE_TO_MAPBOX_LANGUAGE[locale] ?? locale; +} + /** A geographic point as a plain longitude/latitude pair. Mapbox's `LngLat` became a class in mapbox-gl 3.x, but these helpers only read `.lng`/`.lat`, so a literal shape is all that's needed. */ type LngLatLiteral = {lng: number; lat: number}; @@ -241,4 +261,5 @@ export default { isSingleSegmentRoute, convertSegmentedRouteToSingleSegmentRoute, getCoordinatesFromAllDirections, + getMapboxLanguage, }; diff --git a/tests/unit/MapViewUtilsTest.ts b/tests/unit/MapViewUtilsTest.ts index b361b61fcecf..d58bece63b85 100644 --- a/tests/unit/MapViewUtilsTest.ts +++ b/tests/unit/MapViewUtilsTest.ts @@ -180,4 +180,22 @@ describe('MapView utils', () => { expect(utils.isSingleSegmentRoute([])).toBe(true); }); }); + + describe('getMapboxLanguage', () => { + it('passes locales that are already valid Mapbox codes through unchanged', () => { + expect(utils.getMapboxLanguage('en')).toBe('en'); + expect(utils.getMapboxLanguage('es')).toBe('es'); + expect(utils.getMapboxLanguage('fr')).toBe('fr'); + expect(utils.getMapboxLanguage('de')).toBe('de'); + }); + + it('remaps locales whose value differs from the Mapbox code', () => { + expect(utils.getMapboxLanguage('pt-BR')).toBe('pt'); + expect(utils.getMapboxLanguage('zh-hans')).toBe('zh-Hans'); + }); + + it('returns undefined when there is no locale', () => { + expect(utils.getMapboxLanguage(undefined)).toBeUndefined(); + }); + }); }); From 12684c7bf8bbb29e18754cc7c3cc78c8fdb9008d Mon Sep 17 00:00:00 2001 From: "Linh Vo (via MelvinBot)" Date: Fri, 4 Sep 2026 03:27:10 +0000 Subject: [PATCH 2/6] Set the Mapbox worldview from the user's country on web Mapbox draws disputed borders from the perspective of the worldview it is given. The map never set one, so every user saw the style's default. Web now sends the worldview matching the user's country alongside the label language. Co-authored-by: Rory Abraham Co-authored-by: Linh Vo --- src/components/MapView/MapViewImpl.web.tsx | 19 ++++++++++++------- src/components/MapView/utils.ts | 20 ++++++++++++++++++++ tests/unit/MapViewUtilsTest.ts | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/components/MapView/MapViewImpl.web.tsx b/src/components/MapView/MapViewImpl.web.tsx index cbaafd53df07..9c3c46df72a9 100644 --- a/src/components/MapView/MapViewImpl.web.tsx +++ b/src/components/MapView/MapViewImpl.web.tsx @@ -63,6 +63,7 @@ function MapViewImpl({ const hasAlternateDirection = !!alternateDirection?.coordinates?.length; const [userLocation] = useOnyx(ONYXKEYS.USER_LOCATION); + const [countryByIp] = useOnyx(ONYXKEYS.COUNTRY); const {isOffline} = useNetwork(); const {translate, preferredLocale} = useLocalize(); @@ -199,26 +200,30 @@ function MapViewImpl({ }, [mapRef]); // cspell:ignore styledata - // Keep the map labels in the user's preferred app locale, reapplying whenever the map or locale changes. + // Keep the map labels in the user's preferred app locale and its disputed borders drawn from the user's + // own worldview, reapplying whenever the map, the locale or the country changes. useEffect(() => { if (!mapRef) { return; } const map = mapRef.getMap(); - const applyLanguage = () => map.setLanguage(utils.getMapboxLanguage(preferredLocale)); + const applyLocalization = () => { + map.setLanguage(utils.getMapboxLanguage(preferredLocale)); + map.setWorldview(utils.getMapboxWorldview(countryByIp)); + }; if (map.isStyleLoaded()) { - applyLanguage(); + applyLocalization(); return; } - // The style must be loaded before labels can be localized, so defer until it is ready. - map.once('styledata', applyLanguage); + // The style must be loaded before labels and borders can be localized, so defer until it is ready. + map.once('styledata', applyLocalization); return () => { - map.off('styledata', applyLanguage); + map.off('styledata', applyLocalization); }; - }, [mapRef, preferredLocale]); + }, [mapRef, preferredLocale, countryByIp]); useImperativeHandle( ref, diff --git a/src/components/MapView/utils.ts b/src/components/MapView/utils.ts index 873b0cf87b6c..cd54bf6320ea 100644 --- a/src/components/MapView/utils.ts +++ b/src/components/MapView/utils.ts @@ -20,6 +20,25 @@ function getMapboxLanguage(locale: Locale | undefined): string | undefined { return LOCALE_TO_MAPBOX_LANGUAGE[locale] ?? locale; } +// cspell:ignore tileset +/** + * Worldviews the Mapbox Streets tileset behind our map style ships boundaries for. + * A worldview decides which side of a disputed border is drawn, so Mapbox only defines one for the + * handful of countries that dispute borders; every other country shares Mapbox's default worldview. + */ +const MAPBOX_WORLDVIEWS = new Set(['CN', 'IN', 'JP', 'US']); + +/** + * Maps the user's country to the Mapbox worldview used to draw disputed borders. + * Returns undefined for countries Mapbox has no dedicated worldview for, which leaves the style's default in place. + */ +function getMapboxWorldview(country: string | undefined): string | undefined { + if (!country || !MAPBOX_WORLDVIEWS.has(country)) { + return undefined; + } + return country; +} + /** A geographic point as a plain longitude/latitude pair. Mapbox's `LngLat` became a class in mapbox-gl 3.x, but these helpers only read `.lng`/`.lat`, so a literal shape is all that's needed. */ type LngLatLiteral = {lng: number; lat: number}; @@ -262,4 +281,5 @@ export default { convertSegmentedRouteToSingleSegmentRoute, getCoordinatesFromAllDirections, getMapboxLanguage, + getMapboxWorldview, }; diff --git a/tests/unit/MapViewUtilsTest.ts b/tests/unit/MapViewUtilsTest.ts index d58bece63b85..a21dc5569e44 100644 --- a/tests/unit/MapViewUtilsTest.ts +++ b/tests/unit/MapViewUtilsTest.ts @@ -198,4 +198,23 @@ describe('MapView utils', () => { expect(utils.getMapboxLanguage(undefined)).toBeUndefined(); }); }); + + describe('getMapboxWorldview', () => { + it('returns the country when Mapbox defines a worldview for it', () => { + expect(utils.getMapboxWorldview('US')).toBe('US'); + expect(utils.getMapboxWorldview('CN')).toBe('CN'); + expect(utils.getMapboxWorldview('IN')).toBe('IN'); + expect(utils.getMapboxWorldview('JP')).toBe('JP'); + }); + + it('returns undefined for countries that share the default worldview', () => { + expect(utils.getMapboxWorldview('DE')).toBeUndefined(); + expect(utils.getMapboxWorldview('AU')).toBeUndefined(); + }); + + it('returns undefined when the country is unknown or empty', () => { + expect(utils.getMapboxWorldview(undefined)).toBeUndefined(); + expect(utils.getMapboxWorldview('')).toBeUndefined(); + }); + }); }); From e73ea1d3320757f974942ec3a486eda7ad09c393 Mon Sep 17 00:00:00 2001 From: "Rory Abraham (via MelvinBot)" Date: Fri, 4 Sep 2026 03:31:57 +0000 Subject: [PATCH 3/6] Apply oxfmt import grouping to MapView utils Co-authored-by: Rory Abraham --- src/components/MapView/utils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/MapView/utils.ts b/src/components/MapView/utils.ts index cd54bf6320ea..12588e2b2921 100644 --- a/src/components/MapView/utils.ts +++ b/src/components/MapView/utils.ts @@ -1,5 +1,6 @@ import {LOCALES} from '@src/CONST/LOCALES'; import type {Locale} from '@src/CONST/LOCALES'; + import type {AlternateDirection, Coordinate} from './MapViewTypes'; /** App locales whose value isn't already the BCP-47 code Mapbox expects for label localization. */ From bde9321698133a7a03b15d611ffbc38ec0ae515a Mon Sep 17 00:00:00 2001 From: "Linh Vo (via MelvinBot)" Date: Fri, 4 Sep 2026 03:33:53 +0000 Subject: [PATCH 4/6] Pass any country code as the worldview instead of an allowlist Mapbox falls back to the style's default worldview for country codes it has no worldview for, so an allowlist only adds a list that goes stale as Mapbox adds worldviews. Only values that aren't country codes at all still need dropping, since those raise an error. Co-authored-by: Rory Abraham Co-authored-by: Linh Vo --- src/components/MapView/utils.ts | 16 +++++++--------- tests/unit/MapViewUtilsTest.ts | 12 +++++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/components/MapView/utils.ts b/src/components/MapView/utils.ts index cd54bf6320ea..0069e20ee41e 100644 --- a/src/components/MapView/utils.ts +++ b/src/components/MapView/utils.ts @@ -20,20 +20,18 @@ function getMapboxLanguage(locale: Locale | undefined): string | undefined { return LOCALE_TO_MAPBOX_LANGUAGE[locale] ?? locale; } -// cspell:ignore tileset -/** - * Worldviews the Mapbox Streets tileset behind our map style ships boundaries for. - * A worldview decides which side of a disputed border is drawn, so Mapbox only defines one for the - * handful of countries that dispute borders; every other country shares Mapbox's default worldview. - */ -const MAPBOX_WORLDVIEWS = new Set(['CN', 'IN', 'JP', 'US']); +/** A worldview is an ISO 3166-1 alpha-2 country code, so anything that isn't two letters can't be one. */ +const ISO_ALPHA_2_COUNTRY = /^[A-Z]{2}$/; /** * Maps the user's country to the Mapbox worldview used to draw disputed borders. - * Returns undefined for countries Mapbox has no dedicated worldview for, which leaves the style's default in place. + * Mapbox only defines a worldview for the handful of countries that dispute borders and falls back to the + * style's default for every other country code, so the country is passed straight through rather than + * matched against a list that would go stale as Mapbox adds worldviews. Anything that isn't a country code + * is dropped, because Mapbox raises an error for codes it can't parse. */ function getMapboxWorldview(country: string | undefined): string | undefined { - if (!country || !MAPBOX_WORLDVIEWS.has(country)) { + if (!country || !ISO_ALPHA_2_COUNTRY.test(country)) { return undefined; } return country; diff --git a/tests/unit/MapViewUtilsTest.ts b/tests/unit/MapViewUtilsTest.ts index a21dc5569e44..c2a201174943 100644 --- a/tests/unit/MapViewUtilsTest.ts +++ b/tests/unit/MapViewUtilsTest.ts @@ -200,21 +200,23 @@ describe('MapView utils', () => { }); describe('getMapboxWorldview', () => { - it('returns the country when Mapbox defines a worldview for it', () => { + it('passes country codes Mapbox defines a worldview for through unchanged', () => { expect(utils.getMapboxWorldview('US')).toBe('US'); expect(utils.getMapboxWorldview('CN')).toBe('CN'); expect(utils.getMapboxWorldview('IN')).toBe('IN'); expect(utils.getMapboxWorldview('JP')).toBe('JP'); }); - it('returns undefined for countries that share the default worldview', () => { - expect(utils.getMapboxWorldview('DE')).toBeUndefined(); - expect(utils.getMapboxWorldview('AU')).toBeUndefined(); + it('passes countries without a dedicated worldview through so Mapbox falls back to the style default', () => { + expect(utils.getMapboxWorldview('DE')).toBe('DE'); + expect(utils.getMapboxWorldview('AU')).toBe('AU'); }); - it('returns undefined when the country is unknown or empty', () => { + it('drops anything that is not a country code, which Mapbox would reject', () => { expect(utils.getMapboxWorldview(undefined)).toBeUndefined(); expect(utils.getMapboxWorldview('')).toBeUndefined(); + expect(utils.getMapboxWorldview('USA')).toBeUndefined(); + expect(utils.getMapboxWorldview('us')).toBeUndefined(); }); }); }); From a076325d8d0c8d5d3d3942120cb0798f9ed8007a Mon Sep 17 00:00:00 2001 From: "Linh Vo (via MelvinBot)" Date: Fri, 9 Oct 2026 07:15:14 +0000 Subject: [PATCH 5/6] Document the Mapbox locale and worldview tests with Given/When/Then Co-authored-by: Linh Vo --- tests/unit/MapViewUtilsTest.ts | 59 ++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/tests/unit/MapViewUtilsTest.ts b/tests/unit/MapViewUtilsTest.ts index ee9fe8719d2a..859ca35a5ede 100644 --- a/tests/unit/MapViewUtilsTest.ts +++ b/tests/unit/MapViewUtilsTest.ts @@ -197,40 +197,65 @@ describe('MapView utils', () => { describe('getMapboxLanguage', () => { it('passes locales that are already valid Mapbox codes through unchanged', () => { - expect(utils.getMapboxLanguage('en')).toBe('en'); - expect(utils.getMapboxLanguage('es')).toBe('es'); - expect(utils.getMapboxLanguage('fr')).toBe('fr'); - expect(utils.getMapboxLanguage('de')).toBe('de'); + // Given app locales whose values are already BCP-47 codes that Mapbox can localize labels to + + // When each locale is mapped to the language the map labels are rendered in + const languages = [utils.getMapboxLanguage('en'), utils.getMapboxLanguage('es'), utils.getMapboxLanguage('fr'), utils.getMapboxLanguage('de')]; + + // Then every locale is passed through as is, because Mapbox already understands it and the labels should match the app's language + expect(languages).toEqual(['en', 'es', 'fr', 'de']); }); it('remaps locales whose value differs from the Mapbox code', () => { - expect(utils.getMapboxLanguage('pt-BR')).toBe('pt'); - expect(utils.getMapboxLanguage('zh-hans')).toBe('zh-Hans'); + // Given app locales whose values are not the codes Mapbox expects, so Mapbox would not recognize them as is + + // When each locale is mapped to the language the map labels are rendered in + const languages = [utils.getMapboxLanguage('pt-BR'), utils.getMapboxLanguage('zh-hans')]; + + // Then each locale is converted to the matching Mapbox code, so the labels are still shown in the user's language + expect(languages).toEqual(['pt', 'zh-Hans']); }); it('returns undefined when there is no locale', () => { - expect(utils.getMapboxLanguage(undefined)).toBeUndefined(); + // Given a user whose preferred locale has not loaded yet + + // When the missing locale is mapped to a Mapbox language + const language = utils.getMapboxLanguage(undefined); + + // Then no language is returned, so the map keeps its default labels instead of being set to an invalid language + expect(language).toBeUndefined(); }); }); describe('getMapboxWorldview', () => { it('passes country codes Mapbox defines a worldview for through unchanged', () => { - expect(utils.getMapboxWorldview('US')).toBe('US'); - expect(utils.getMapboxWorldview('CN')).toBe('CN'); - expect(utils.getMapboxWorldview('IN')).toBe('IN'); - expect(utils.getMapboxWorldview('JP')).toBe('JP'); + // Given countries that Mapbox draws disputed borders for from their own point of view + + // When each country is mapped to the worldview the map's borders are drawn from + const worldviews = [utils.getMapboxWorldview('US'), utils.getMapboxWorldview('CN'), utils.getMapboxWorldview('IN'), utils.getMapboxWorldview('JP')]; + + // Then every country is passed through as is, so users see the borders as their own country recognizes them + expect(worldviews).toEqual(['US', 'CN', 'IN', 'JP']); }); it('passes countries without a dedicated worldview through so Mapbox falls back to the style default', () => { - expect(utils.getMapboxWorldview('DE')).toBe('DE'); - expect(utils.getMapboxWorldview('AU')).toBe('AU'); + // Given countries that Mapbox has no dedicated worldview for + + // When each country is mapped to the worldview the map's borders are drawn from + const worldviews = [utils.getMapboxWorldview('DE'), utils.getMapboxWorldview('AU')]; + + // Then the countries are still passed through, because Mapbox falls back to the style's default worldview and a hardcoded list of supported countries would go stale + expect(worldviews).toEqual(['DE', 'AU']); }); it('drops anything that is not a country code, which Mapbox would reject', () => { - expect(utils.getMapboxWorldview(undefined)).toBeUndefined(); - expect(utils.getMapboxWorldview('')).toBeUndefined(); - expect(utils.getMapboxWorldview('USA')).toBeUndefined(); - expect(utils.getMapboxWorldview('us')).toBeUndefined(); + // Given values that are not two-letter uppercase country codes, such as a missing country or a three-letter code + + // When each value is mapped to the worldview the map's borders are drawn from + const worldviews = [utils.getMapboxWorldview(undefined), utils.getMapboxWorldview(''), utils.getMapboxWorldview('USA'), utils.getMapboxWorldview('us')]; + + // Then no worldview is returned for any of them, because Mapbox raises an error for codes it can't parse + expect(worldviews).toEqual([undefined, undefined, undefined, undefined]); }); }); }); From 3b0f983213d64fe74e72a7ae4f05c2426c922f3e Mon Sep 17 00:00:00 2001 From: "Linh Vo (via MelvinBot)" Date: Fri, 9 Oct 2026 09:57:51 +0000 Subject: [PATCH 6/6] Skip native Mapbox label localization for languages the native SDKs reject Co-authored-by: Linh Vo --- src/components/MapView/MapView.tsx | 2 +- src/components/MapView/utils.ts | 18 +++++++++++++++++- tests/unit/MapViewUtilsTest.ts | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/components/MapView/MapView.tsx b/src/components/MapView/MapView.tsx index 94658ab5d480..986566808ba8 100644 --- a/src/components/MapView/MapView.tsx +++ b/src/components/MapView/MapView.tsx @@ -263,7 +263,7 @@ function MapView({ // Localize the map labels to the user's preferred app locale so they match the rest of the app. const localizeLabels = useMemo<{locale: string} | undefined>(() => { - const language = utils.getMapboxLanguage(preferredLocale); + const language = utils.getNativeMapboxLanguage(preferredLocale); return language ? {locale: language} : undefined; }, [preferredLocale]); diff --git a/src/components/MapView/utils.ts b/src/components/MapView/utils.ts index 7ea938a34c10..9f8b1ac447a0 100644 --- a/src/components/MapView/utils.ts +++ b/src/components/MapView/utils.ts @@ -9,10 +9,16 @@ const LOCALE_TO_MAPBOX_LANGUAGE: Partial> = { [LOCALES.ZH_HANS]: 'zh-Hans', }; +/** + * Languages the native Mapbox Maps SDKs can localize labels to, i.e. the Mapbox Streets v8 `name_*` fields. + * Unlike mapbox-gl on web, the native SDKs reject any other language: iOS throws and Android logs a warning on every map load. + */ +const NATIVE_MAPBOX_SUPPORTED_LANGUAGES = new Set(['ar', 'en', 'es', 'fr', 'de', 'it', 'pt', 'ru', 'ja', 'ko', 'vi', 'zh-Hans', 'zh-Hant']); + /** * Maps an app locale to the BCP-47 language code Mapbox uses to localize map labels. * Most app locales are already valid Mapbox codes, so only a couple need remapping. - * Unsupported codes fall back to each label's local language on the Mapbox side. + * On web, unsupported codes fall back to each label's local language on the Mapbox side. */ function getMapboxLanguage(locale: Locale | undefined): string | undefined { if (!locale) { @@ -21,6 +27,15 @@ function getMapboxLanguage(locale: Locale | undefined): string | undefined { return LOCALE_TO_MAPBOX_LANGUAGE[locale] ?? locale; } +/** + * Maps an app locale to the language the native Mapbox SDKs localize labels to, or undefined when they can't. + * Leaving the language unset keeps the style's default labels instead of raising a native error. + */ +function getNativeMapboxLanguage(locale: Locale | undefined): string | undefined { + const language = getMapboxLanguage(locale); + return language && NATIVE_MAPBOX_SUPPORTED_LANGUAGES.has(language) ? language : undefined; +} + /** A worldview is an ISO 3166-1 alpha-2 country code, so anything that isn't two letters can't be one. */ const ISO_ALPHA_2_COUNTRY = /^[A-Z]{2}$/; @@ -288,5 +303,6 @@ export default { convertSegmentedRouteToSingleSegmentRoute, getCoordinatesFromAllDirections, getMapboxLanguage, + getNativeMapboxLanguage, getMapboxWorldview, }; diff --git a/tests/unit/MapViewUtilsTest.ts b/tests/unit/MapViewUtilsTest.ts index 859ca35a5ede..ced46cf891b0 100644 --- a/tests/unit/MapViewUtilsTest.ts +++ b/tests/unit/MapViewUtilsTest.ts @@ -227,6 +227,28 @@ describe('MapView utils', () => { }); }); + describe('getNativeMapboxLanguage', () => { + it('returns the Mapbox language for locales the native SDKs can localize labels to', () => { + // Given app locales whose Mapbox language is one of the Mapbox Streets languages the native SDKs support + + // When each locale is mapped to the language the native map labels are rendered in + const languages = [utils.getNativeMapboxLanguage('en'), utils.getNativeMapboxLanguage('ja'), utils.getNativeMapboxLanguage('pt-BR'), utils.getNativeMapboxLanguage('zh-hans')]; + + // Then the same language as on web is returned, so the native labels match the app's language + expect(languages).toEqual(['en', 'ja', 'pt', 'zh-Hans']); + }); + + it('returns undefined for locales the native SDKs reject', () => { + // Given app locales whose language the native Mapbox SDKs don't support, which makes iOS throw and Android log a warning + + // When each locale is mapped to the language the native map labels are rendered in + const languages = [utils.getNativeMapboxLanguage('nl'), utils.getNativeMapboxLanguage('pl'), utils.getNativeMapboxLanguage('el')]; + + // Then no language is returned, so the native map keeps its default labels without raising an error + expect(languages).toEqual([undefined, undefined, undefined]); + }); + }); + describe('getMapboxWorldview', () => { it('passes country codes Mapbox defines a worldview for through unchanged', () => { // Given countries that Mapbox draws disputed borders for from their own point of view