Repository navigation
feat(i18n): follow the OS language when no language is selected #168
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
Open
devs-rootstrap
wants to merge
2
commits into
master
Choose a base branch
from
feat/sync-language-with-os
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { getLocales } from 'expo-localization'; | ||
|
|
||
| import { storage } from '../../../src/lib/storage'; | ||
|
|
||
| jest.mock('../../../src/lib/storage', () => ({ | ||
| storage: { | ||
| getString: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('expo-localization', () => ({ | ||
| getLocales: jest.fn(), | ||
| })); | ||
|
|
||
| const mockGetString = storage.getString as jest.Mock; | ||
| const mockGetLocales = getLocales as jest.Mock; | ||
|
|
||
| function loadI18n() { | ||
| let i18n: typeof import('i18next').default | undefined; | ||
| jest.isolateModules(() => { | ||
| i18n = (require('../../../src/lib/i18n') as { default: typeof import('i18next').default }).default; | ||
| }); | ||
| return i18n!; | ||
| } | ||
|
|
||
| describe('i18n initialization', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockGetLocales.mockReturnValue([{ languageTag: 'es-ES' }]); | ||
| }); | ||
|
|
||
| it('should use the language selected in the app', () => { | ||
| mockGetString.mockReturnValue('en'); | ||
|
|
||
| expect(loadI18n().language).toBe('en'); | ||
| }); | ||
|
|
||
| it('should use the OS language when no language is selected', () => { | ||
| mockGetString.mockReturnValue(undefined); | ||
|
|
||
| expect(loadI18n().language).toBe('es-ES'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import type { AppStateStatus } from 'react-native'; | ||
| import { renderHook } from '@testing-library/react-native'; | ||
| import { getLocales } from 'expo-localization'; | ||
| import i18n from 'i18next'; | ||
| import { AppState } from 'react-native'; | ||
|
|
||
| import { storage } from '../../../src/lib/storage'; | ||
| import { | ||
| resolveLanguage, | ||
| syncLanguageWithOS, | ||
| useSyncLanguageWithOS, | ||
| } from '../../../src/lib/i18n/utils'; | ||
|
|
||
| jest.mock('../../../src/lib/storage', () => ({ | ||
| storage: { | ||
| getString: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('expo-localization', () => ({ | ||
| getLocales: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('i18next', () => { | ||
| const mockI18n = { | ||
| language: 'en-US', | ||
| changeLanguage: jest.fn(), | ||
| t: jest.fn(), | ||
| }; | ||
| return { | ||
| __esModule: true, | ||
| default: mockI18n, | ||
| changeLanguage: mockI18n.changeLanguage, | ||
| t: mockI18n.t, | ||
| }; | ||
| }); | ||
|
|
||
| const mockGetString = storage.getString as jest.Mock; | ||
| const mockGetLocales = getLocales as jest.Mock; | ||
| const mockChangeLanguage = i18n.changeLanguage as jest.Mock; | ||
|
|
||
| function mockDeviceLanguage(languageTag: string) { | ||
| mockGetLocales.mockReturnValue([{ languageTag }]); | ||
| } | ||
|
|
||
| describe('resolveLanguage', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should return the language selected in the app', () => { | ||
| mockGetString.mockReturnValue('en'); | ||
| mockDeviceLanguage('es-ES'); | ||
|
|
||
| expect(resolveLanguage()).toBe('en'); | ||
| }); | ||
|
|
||
| it('should return the OS language when no language is selected', () => { | ||
| mockGetString.mockReturnValue(undefined); | ||
| mockDeviceLanguage('es-ES'); | ||
|
|
||
| expect(resolveLanguage()).toBe('es-ES'); | ||
| }); | ||
|
|
||
| it('should return undefined when no language is available', () => { | ||
| mockGetString.mockReturnValue(undefined); | ||
| mockGetLocales.mockReturnValue([]); | ||
|
|
||
| expect(resolveLanguage()).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('syncLanguageWithOS', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| i18n.language = 'en-US'; | ||
| }); | ||
|
|
||
| it('should change the language when the OS language changed and none is selected', () => { | ||
| mockGetString.mockReturnValue(undefined); | ||
| mockDeviceLanguage('es-ES'); | ||
|
|
||
| syncLanguageWithOS(); | ||
|
|
||
| expect(mockChangeLanguage).toHaveBeenCalledWith('es-ES'); | ||
| }); | ||
|
|
||
| it('should not change the language when the OS language did not change', () => { | ||
| mockGetString.mockReturnValue(undefined); | ||
| mockDeviceLanguage('en-US'); | ||
|
|
||
| syncLanguageWithOS(); | ||
|
|
||
| expect(mockChangeLanguage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not change the language when a language is selected', () => { | ||
| mockGetString.mockReturnValue('en'); | ||
| mockDeviceLanguage('es-ES'); | ||
|
|
||
| syncLanguageWithOS(); | ||
|
|
||
| expect(mockChangeLanguage).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('useSyncLanguageWithOS', () => { | ||
| let listener: ((state: AppStateStatus) => void) | undefined; | ||
| const mockRemove = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| i18n.language = 'en-US'; | ||
| listener = undefined; | ||
| jest.spyOn(AppState, 'addEventListener').mockImplementation((_, handler) => { | ||
| listener = handler as (state: AppStateStatus) => void; | ||
| return { remove: mockRemove }; | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should update the language when the app comes to the foreground', () => { | ||
| mockGetString.mockReturnValue(undefined); | ||
| mockDeviceLanguage('es-ES'); | ||
| renderHook(() => useSyncLanguageWithOS()); | ||
|
|
||
| listener?.('active'); | ||
|
|
||
| expect(AppState.addEventListener).toHaveBeenCalledWith('change', expect.any(Function)); | ||
| expect(mockChangeLanguage).toHaveBeenCalledWith('es-ES'); | ||
| }); | ||
|
|
||
| it('should not update the language when the app goes to the background', () => { | ||
| mockGetString.mockReturnValue(undefined); | ||
| mockDeviceLanguage('es-ES'); | ||
| renderHook(() => useSyncLanguageWithOS()); | ||
|
|
||
| listener?.('background'); | ||
|
|
||
| expect(mockChangeLanguage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not update the language on foreground when a language is selected', () => { | ||
| mockGetString.mockReturnValue('en'); | ||
| mockDeviceLanguage('es-ES'); | ||
| renderHook(() => useSyncLanguageWithOS()); | ||
|
|
||
| listener?.('active'); | ||
|
|
||
| expect(mockChangeLanguage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should remove the listener on unmount', () => { | ||
| const { unmount } = renderHook(() => useSyncLanguageWithOS()); | ||
|
|
||
| unmount(); | ||
|
|
||
| expect(mockRemove).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.