-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ #7 - Formio renderer: implement mechanism to provide translations.
- Loading branch information
Sven van de Scheur
committed
Mar 1, 2023
1 parent
dd9faaa
commit befb83f
Showing
7 changed files
with
86 additions
and
27 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
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,28 @@ | ||
import { I18N } from '@types' | ||
|
||
/** | ||
* Returns translation for key based on locale in i18n. | ||
* @param {string} [key] | ||
* @param {I18N} [i18n] | ||
* @param {{[index: string]: any}} context {{ placeholder }} replacements. | ||
* @param {string} [locale] If omitted: an attempt is made to resolve a local from the browser. | ||
*/ | ||
export const gettext = ( | ||
key: string = '', | ||
i18n: I18N = {}, | ||
context: { [index: string]: any } = {}, | ||
locale: string = '' | ||
): string => { | ||
const navigateLocales = navigator?.languages.map((locale) => locale.split('-')[0]) || [] | ||
const locales = [locale, ...navigateLocales, Object.keys(i18n)[0] || ''] | ||
const resolvedLocale = Object.keys(i18n).find((key) => locales.indexOf(key) > -1) | ||
const dictionary = i18n[resolvedLocale as string] || {} | ||
const translation = dictionary[key] || key | ||
|
||
// Apply context. | ||
return Object.entries(context).reduce( | ||
(compiledTranslation, [key, value]) => | ||
compiledTranslation.replace(new RegExp(`{{\\s*?${key}\\s*?}}`, 'g'), String(value)), | ||
translation | ||
) | ||
} |
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 @@ | ||
export * from './gettext' |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './constants' | ||
export * from './i18n' | ||
export * from './renderer' | ||
export * from './utils' | ||
export * from './validation' |
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