-
Notifications
You must be signed in to change notification settings - Fork 1
Feat: add handlebars language link helper #56
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
35 changes: 35 additions & 0 deletions
35
lib/handlebars/helpers/__snapshots__/languageLink.test.js.snap
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,35 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`languageLink should return dialog with custom link and default to opposite language label (en -> sv) 1`] = ` | ||
| "<a class="kth-menu-item language" hreflang="sv-SE" href="">Svenska</a> | ||
| <dialog class="kth-translation"> | ||
| <div class="kth-translation__content"> | ||
| <button class="kth-icon-button close"> | ||
| <span class="kth-visually-hidden">Close</span> | ||
| </button> | ||
| <span>Den här sidan är inte översatt</span> | ||
| <a href="https://kth.se/sv">Startsida på svenska</a> | ||
| </div> | ||
| </dialog>" | ||
| `; | ||
|
|
||
| exports[`languageLink should return dialog with custom link and default to opposite language label (sv -> en) 1`] = ` | ||
| "<a class="kth-menu-item language" hreflang="en-US" href="">English</a> | ||
| <dialog class="kth-translation"> | ||
| <div class="kth-translation__content"> | ||
| <button class="kth-icon-button close"> | ||
| <span class="kth-visually-hidden">Stäng</span> | ||
| </button> | ||
| <span>This page is not translated</span> | ||
| <a href="https://kth.se/en">Start page in English</a> | ||
| </div> | ||
| </dialog>" | ||
| `; | ||
|
|
||
| exports[`languageLink should return link with query parameter and anchorMessageKey in opposite language (en -> sv) 1`] = `"<a class="kth-menu-item language" hreflang="sv-SE" href="?l=sv">Custom</a>"`; | ||
|
|
||
| exports[`languageLink should return link with query parameter and anchorMessageKey in opposite language (sv -> en) 1`] = `"<a class="kth-menu-item language" hreflang="en-US" href="?l=en">Anpassad</a>"`; | ||
|
|
||
| exports[`languageLink should return link with query parameter and default to opposite language label (en -> sv) 1`] = `"<a class="kth-menu-item language" hreflang="sv-SE" href="?l=sv">Svenska</a>"`; | ||
|
|
||
| exports[`languageLink should return link with query parameter and default to opposite language label (sv -> en) 1`] = `"<a class="kth-menu-item language" hreflang="en-US" href="?l=en">English</a>"`; |
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,80 @@ | ||
| const Handlebars = require('handlebars') | ||
| const i18n = require('kth-node-i18n') | ||
|
|
||
| const VALID_LANGUAGES = ['sv', 'en'] | ||
| const VALID_LOCALE = { sv: 'sv-SE', en: 'en-US' } | ||
|
|
||
| function resolveTranslationLanguage(lang) { | ||
| if (!VALID_LANGUAGES.includes(lang)) { | ||
| throw new Error(`[languageLink] helper requires first parameter to be a string matching a language, i.e. 'sv'.`) | ||
| } | ||
| return VALID_LANGUAGES[1 - VALID_LANGUAGES.indexOf(lang)] | ||
| } | ||
|
|
||
| function resolveDefaultLabel(lang) { | ||
| const translationLang = resolveTranslationLanguage(lang) | ||
| return i18n.message(`language_link_lang_${translationLang}`, lang) | ||
| } | ||
|
|
||
| function anchorElement(lang, anchorMessageKey, link) { | ||
| const label = typeof anchorMessageKey === 'string' ? i18n.message(anchorMessageKey, lang) : resolveDefaultLabel(lang) | ||
|
|
||
| const hreflang = VALID_LOCALE[resolveTranslationLanguage(lang)] | ||
| const output = `<a class="kth-menu-item language" hreflang="${hreflang}" href="${link}">${label}</a>` | ||
| return output | ||
| } | ||
|
|
||
| function dialogElement(lang, link, dialogMessageKey) { | ||
| const output = ` | ||
| <dialog class="kth-translation"> | ||
| <div class="kth-translation__content"> | ||
| <button class="kth-icon-button close"> | ||
| <span class="kth-visually-hidden">${i18n.message('language_link_button_close', lang)}</span> | ||
| </button> | ||
| <span>${i18n.message('language_link_not_translated', lang)}</span> | ||
| <a href="${link}">${i18n.message(dialogMessageKey, lang)}</a> | ||
| </div> | ||
| </dialog>` | ||
| return output | ||
| } | ||
|
|
||
| /** | ||
| * Generates a language link and an optional dialog element for language selection. | ||
| * | ||
| * Used i18n keys: | ||
| * - `language_link_lang_[sv/en]` - Default label for the anchor element's text, if a custom one isn’t provided. | ||
| * - `language_link_button_close` - The label for the close button in the dialog element. | ||
| * - `language_link_not_translated` - The label for the dialog element's text. | ||
| * | ||
| * @param {string} lang - The current language. | ||
| * @param {string} [anchorMessageKey] - The i18n key for the anchor element's text. Can be omitted for default label. | ||
| * @param {string} [link] - The URL to navigate to when the anchor is clicked. If provided, a dialog element is also generated. | ||
| * @param {string} [dialogMessageKey] - The i18n key for the dialog element's text. Required if `link` is provided. | ||
| * | ||
| * @returns {string} The generated HTML string containing the language link and optional dialog element. | ||
| * | ||
| * @throws {Error} If `lang` is not a valid language or if `link` is provided but `dialogMessageKey` is not. | ||
| */ | ||
| function languageLink(lang, anchorMessageKey, link, dialogMessageKey) { | ||
| // Custom link is missing, use a query parameter to change language | ||
| if (typeof link !== 'string') { | ||
| return anchorElement(lang, anchorMessageKey, `?l=${resolveTranslationLanguage(lang)}`) | ||
| } | ||
|
|
||
| // Link is provided, but dialog information is incomplete | ||
| if (typeof dialogMessageKey !== 'string') { | ||
| throw new Error(`[languageLink] helper requires a fourth parameter, if a third is provided.`) | ||
| } | ||
|
|
||
| // Link is provided, use custom link and dialog | ||
| return `${anchorElement(lang, anchorMessageKey, '')}${dialogElement(lang, link, dialogMessageKey)}` | ||
| } | ||
|
|
||
| function registerLanguageLinkHelper() { | ||
| Handlebars.registerHelper('languageLink', languageLink) | ||
| } | ||
|
|
||
| module.exports = { | ||
| registerLanguageLinkHelper, | ||
| languageLink, // Exported for testing | ||
| } |
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,92 @@ | ||
| const handlebars = require('handlebars') | ||
|
|
||
| const mockTranslations = { | ||
| en: { | ||
| language_link_lang_sv: 'Svenska', | ||
| label_custom: 'Custom', | ||
| label_locale_select_link_title: 'Visa översättning', | ||
| language_link_button_close: 'Close', | ||
| language_link_not_translated: 'Den här sidan är inte översatt', | ||
| label_start_page: 'Startsida på svenska', | ||
| }, | ||
| sv: { | ||
| language_link_lang_en: 'English', | ||
| label_custom: 'Anpassad', | ||
| label_locale_select_link_title: 'Show translation', | ||
| language_link_button_close: 'Stäng', | ||
| language_link_not_translated: 'This page is not translated', | ||
| label_start_page: 'Start page in English', | ||
| }, | ||
| } | ||
|
|
||
| jest.mock('kth-node-i18n', () => ({ | ||
| message: (key, lang) => mockTranslations[lang][key], | ||
| })) | ||
|
|
||
| const { registerLanguageLinkHelper, languageLink } = require('./languageLink') | ||
|
|
||
| jest.mock('handlebars') | ||
|
|
||
| describe('registerLanguageLinkHelper', () => { | ||
| it('registers language link helper', () => { | ||
| registerLanguageLinkHelper() | ||
| expect(handlebars.registerHelper).toHaveBeenCalledWith('languageLink', languageLink) | ||
| }) | ||
| }) | ||
|
|
||
| describe('languageLink', () => { | ||
| it('throws an error if lang parameter is missing', () => { | ||
| expect(() => languageLink()).toThrow( | ||
| new Error(`[languageLink] helper requires first parameter to be a string matching a language, i.e. 'sv'.`) | ||
| ) | ||
| }) | ||
|
|
||
| it('throws an error if link is provided, but not dialogMessageKey', () => { | ||
| const lang = 'en' | ||
| expect(() => languageLink(lang, '', 'https://kth.se')).toThrow( | ||
| new Error(`[languageLink] helper requires a fourth parameter, if a third is provided.`) | ||
| ) | ||
| }) | ||
|
|
||
| it('should return link with query parameter and default to opposite language label (en -> sv)', () => { | ||
| const lang = 'en' | ||
| const result = languageLink(lang) | ||
| expect(result).toMatchSnapshot() | ||
| }) | ||
|
|
||
| it('should return link with query parameter and default to opposite language label (sv -> en)', () => { | ||
| const lang = 'sv' | ||
| const result = languageLink(lang) | ||
| expect(result).toMatchSnapshot() | ||
| }) | ||
|
|
||
| it('should return link with query parameter and anchorMessageKey in opposite language (en -> sv)', () => { | ||
| const lang = 'en' | ||
| const anchorMessageKey = 'label_custom' | ||
| const result = languageLink(lang, anchorMessageKey) | ||
| expect(result).toMatchSnapshot() | ||
| }) | ||
|
|
||
| it('should return link with query parameter and anchorMessageKey in opposite language (sv -> en)', () => { | ||
| const lang = 'sv' | ||
| const anchorMessageKey = 'label_custom' | ||
| const result = languageLink(lang, anchorMessageKey) | ||
| expect(result).toMatchSnapshot() | ||
| }) | ||
|
|
||
| it('should return dialog with custom link and default to opposite language label (en -> sv)', () => { | ||
| const lang = 'en' | ||
| const link = 'https://kth.se/sv' | ||
| const dialogMessageKey = 'label_start_page' | ||
| const result = languageLink(lang, null, link, dialogMessageKey) | ||
| expect(result).toMatchSnapshot() | ||
| }) | ||
|
|
||
| it('should return dialog with custom link and default to opposite language label (sv -> en)', () => { | ||
| const lang = 'sv' | ||
| const link = 'https://kth.se/en' | ||
| const dialogMessageKey = 'label_start_page' | ||
| const result = languageLink(lang, null, link, dialogMessageKey) | ||
| expect(result).toMatchSnapshot() | ||
| }) | ||
| }) |
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.