@grafana/ui
Version:
Grafana Components Library
1 lines • 2.56 kB
Source Map (JSON)
{"version":3,"file":"i18n.mjs","sources":["../../../src/utils/i18n.tsx"],"sourcesContent":["import i18next from 'i18next';\nimport { ReactElement } from 'react';\nimport { Trans as I18NextTrans, initReactI18next } from 'react-i18next'; // eslint-disable-line no-restricted-imports\n\n// We want to translate grafana-ui without introducing any breaking changes for consumers\n// who use grafana-ui outside of grafana (such as grafana.com self serve). The other struggle\n// is that grafana-ui does not require a top-level provider component, so we don't get the\n// chance to do the mandatory i18next setup that <Trans /> and t() requires\n//\n// We wrap <Trans /> and t() and do a simple check if it hasn't already been set up\n// (Grafana will init i18next in app.ts), and just set it up with a minimal config\n// to use the default phrases in the source jsx.\n\n// Creates a default, english i18next instance when running outside of grafana.\n// we don't support changing the locale of grafana ui when outside of Grafana\nfunction initI18n() {\n // resources is undefined by default and set either by grafana app.ts or here\n if (typeof i18next.options.resources !== 'object') {\n i18next.use(initReactI18next).init({\n resources: {},\n returnEmptyString: false,\n lng: 'en-US', // this should be the locale of the phrases in our source JSX\n });\n }\n}\n\ntype I18NextTransType = typeof I18NextTrans;\ntype I18NextTransProps = Parameters<I18NextTransType>[0];\n\ninterface TransProps extends I18NextTransProps {\n i18nKey: string;\n}\n\nexport const Trans = (props: TransProps): ReactElement => {\n initI18n();\n return <I18NextTrans {...props} />;\n};\n\n// Reassign t() so i18next-parser doesn't warn on dynamic key, and we can have 'failOnWarnings' enabled\nconst tFunc = i18next.t;\n\nexport const t = (id: string, defaultMessage: string, values?: Record<string, unknown>) => {\n initI18n();\n\n return tFunc(id, defaultMessage, values);\n};\n"],"names":["I18NextTrans"],"mappings":";;;;AAeA,SAAS,QAAW,GAAA;AAElB,EAAA,IAAI,OAAO,OAAA,CAAQ,OAAQ,CAAA,SAAA,KAAc,QAAU,EAAA;AACjD,IAAQ,OAAA,CAAA,GAAA,CAAI,gBAAgB,CAAA,CAAE,IAAK,CAAA;AAAA,MACjC,WAAW,EAAC;AAAA,MACZ,iBAAmB,EAAA,KAAA;AAAA,MACnB,GAAK,EAAA;AAAA;AAAA,KACN,CAAA;AAAA;AAEL;AASa,MAAA,KAAA,GAAQ,CAAC,KAAoC,KAAA;AACxD,EAAS,QAAA,EAAA;AACT,EAAO,uBAAA,GAAA,CAACA,OAAc,EAAA,EAAA,GAAG,KAAO,EAAA,CAAA;AAClC;AAGA,MAAM,QAAQ,OAAQ,CAAA,CAAA;AAEf,MAAM,CAAI,GAAA,CAAC,EAAY,EAAA,cAAA,EAAwB,MAAqC,KAAA;AACzF,EAAS,QAAA,EAAA;AAET,EAAO,OAAA,KAAA,CAAM,EAAI,EAAA,cAAA,EAAgB,MAAM,CAAA;AACzC;;;;"}