generated from StanfordBDHG/NextJSTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Implement proper locale check ## ♻️ Current situation & Problem Closes #20 ## ⚙️ Release Notes * Implement proper locale check ### Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
1 parent
bdcc14c
commit 1a8cd56
Showing
3 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,52 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
import { createLocalizationHelpers } from '@/modules/firebase/localizedText' | ||
|
||
const { parseLocalizedText, parseNilLocalizedText } = | ||
createLocalizationHelpers('pl-PL') | ||
|
||
describe('parseLocalizedText', () => { | ||
const plPL = 'pl-PL' | ||
const pl = 'pl' | ||
const en = 'en' | ||
const de = 'de' | ||
|
||
it('supports plain string', () => { | ||
expect(parseLocalizedText(en)).toBe(en) | ||
}) | ||
|
||
describe('localized text object', () => { | ||
it('fallbacks to empty string when no localizations', () => { | ||
expect(parseLocalizedText({})).toBe('') | ||
}) | ||
|
||
it('fallbacks to "en" if no matching client languages', () => { | ||
expect(parseLocalizedText({ de, en })).toBe(en) | ||
}) | ||
|
||
it('fallbacks to any translation if there is no "en" available', () => { | ||
expect(parseLocalizedText({ de })).toBe(de) | ||
}) | ||
|
||
it('resolves ISO-639-1 standard', () => { | ||
expect(parseLocalizedText({ pl, en })).toBe(pl) | ||
}) | ||
|
||
it('prioritizes ISO-639-2 if exists', () => { | ||
expect(parseLocalizedText({ 'pl-PL': plPL, pl, en })).toBe(plPL) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('parseNilLocalizedText', () => { | ||
it('supports nil values', () => { | ||
expect(parseNilLocalizedText(null)).toBe(null) | ||
expect(parseNilLocalizedText(undefined)).toBe(null) | ||
expect(parseNilLocalizedText('')).toBe('') | ||
}) | ||
}) |
This file contains 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 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,16 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ | ||
/** | ||
* Gets user locale, providing en-US as fallback | ||
* */ | ||
export const getNavigatorLanguage = () => { | ||
if (typeof window === 'undefined') return 'en-US' // Fallback for SSR | ||
return navigator.languages.at(0) ?? navigator.language ?? 'en-US' | ||
} | ||
/* eslint-enable @typescript-eslint/no-unnecessary-condition */ |