From 2be0441c49969b6364bdb0722e39a68f8b9541fc Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 9 May 2020 08:46:04 +0200 Subject: [PATCH] Allow i18n download with empty strings (#2713) --- packages/page-settings/src/I18n/index.tsx | 41 +++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/page-settings/src/I18n/index.tsx b/packages/page-settings/src/I18n/index.tsx index 48b0117458..fcefa23733 100644 --- a/packages/page-settings/src/I18n/index.tsx +++ b/packages/page-settings/src/I18n/index.tsx @@ -5,8 +5,9 @@ import FileSaver from 'file-saver'; import React, { useCallback, useEffect, useState } from 'react'; import styled from 'styled-components'; -import { Button, Columar, Column, Dropdown, Progress, Spinner } from '@polkadot/react-components'; +import { Button, Columar, Column, Dropdown, Progress, Spinner, Toggle } from '@polkadot/react-components'; import languageCache from '@polkadot/react-components/i18n/cache'; +import { useToggle } from '@polkadot/react-hooks'; import uiSettings from '@polkadot/ui-settings'; import { useTranslation } from '../translate'; @@ -58,7 +59,17 @@ async function retrieveAll (): Promise { : []; missing.forEach((lng, index): void => { + // setup the language cache languageCache[lng] = translations[index]; + + // fill in all empty values (useful for download, filling in) + Object.keys(english).forEach((record): void => { + Object.keys(english[record]).forEach((key): void => { + if (!languageCache[lng][key]) { + languageCache[lng][key] = ''; + } + }); + }); }); return { @@ -96,11 +107,11 @@ function calcProgress (english: StringsMod, language: Strings): Progress { return [[done, total, 0], breakdown]; } -function doDownload (strings: Strings): void { +function doDownload (strings: Strings, withEmpty: boolean): void { const sanitized = Object.keys(strings).sort().reduce((result: Strings, key): Strings => { const sanitized = strings[key].trim(); - if (sanitized) { + if (sanitized || withEmpty) { result[key] = sanitized; } @@ -124,6 +135,7 @@ function progressDisplay ([done, total, _]: [number, number, number] = [0, 0, 0] function Translate ({ className }: Props): React.ReactElement { const { t } = useTranslation(); + const [withEmpty, toggleWithEmpty] = useToggle(); const [{ english, keys, modules }, setDefaults] = useState({ english: {}, keys: [], modules: [] }); const [lng, setLng] = useState('zh'); const [[modProgress, allProgress], setProgress] = useState([[0, 0, 0], {}]); @@ -174,8 +186,8 @@ function Translate ({ className }: Props): React.ReactElement { ); const _onDownload = useCallback( - () => doDownload(strings || {}), - [strings] + () => doDownload(strings || {}, withEmpty), + [strings, withEmpty] ); if (!keys.length) { @@ -218,6 +230,17 @@ function Translate ({ className }: Props): React.ReactElement { +
+ +