fix(v2): fix bad theme pluralization rules for some labels (#4304)

* Pluralization test!

* Simplify usePluralForm usage with | plural message separator

* fix interpolate bug with falsy values like 0

* fix interpolate bug with falsy values like 0

* Order plural forms + allow to not provide the last plural forms if they are not used

* fix typo

* revert test!

* plurals and typo of the SearchPage

* update some labels

* improve the update-code-translations cli + update translations

* pluralize blog reading time label

* ensure base.json contains message descriptions: helps the user to provide the translations

* remove russian production locale
This commit is contained in:
Sébastien Lorber 2021-03-03 17:05:21 +01:00 committed by GitHub
parent 6c73f51f94
commit 364d4dbf01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 358 additions and 75 deletions

View file

@ -16,17 +16,30 @@ import clsx from 'clsx';
import Head from '@docusaurus/Head';
import Link from '@docusaurus/Link';
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import {useTitleFormatter} from '@docusaurus/theme-common';
import {useTitleFormatter, usePluralForm} from '@docusaurus/theme-common';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import {useAllDocsData} from '@theme/hooks/useDocs';
import useSearchQuery from '@theme/hooks/useSearchQuery';
import Layout from '@theme/Layout';
import Translate, {translate} from '@docusaurus/Translate';
import styles from './styles.module.css';
function pluralize(count, word) {
return count > 1 ? `${word}s` : word;
// Very simple pluralization: probably good enough for now
function useDocumentsFoundPlural() {
const {selectMessage} = usePluralForm();
return (count) =>
selectMessage(
count,
translate(
{
id: 'theme.SearchPage.documentsFound.plurals',
description:
'Pluralized label for "{count} documents found". Use as much plural forms (separated by "|") as your language support (see https://www.unicode.org/cldr/cldr-aux/charts/34/supplemental/language_plural_rules.html)',
message: 'One document found|{count} documents found',
},
{count},
),
);
}
function useDocsSearchVersionsHelpers() {
@ -104,6 +117,7 @@ function SearchPage() {
const {
siteConfig: {themeConfig: {algolia: {appId, apiKey, indexName} = {}}} = {},
} = useDocusaurusContext();
const documentsFoundPlural = useDocumentsFoundPlural();
const docsSearchVersionsHelpers = useDocsSearchVersionsHelpers();
const {searchValue, updateSearchPath} = useSearchQuery();
@ -236,14 +250,16 @@ function SearchPage() {
const getTitle = () =>
searchQuery
? translate({
id: 'theme.SearchPage.existingResultsTitle',
message: 'Search results for "{query}"',
description: 'The search page title for non-empty query',
values: {
? translate(
{
id: 'theme.SearchPage.existingResultsTitle',
message: 'Search results for "{query}"',
description: 'The search page title for non-empty query',
},
{
query: searchQuery,
},
})
)
: translate({
id: 'theme.SearchPage.emptyResultsTitle',
message: 'Search the documentation',
@ -357,8 +373,7 @@ function SearchPage() {
<div className={clsx('col', 'col--8', styles.searchResultsColumn)}>
{!!searchResultState.totalResults && (
<strong>
{searchResultState.totalResults}{' '}
{pluralize(searchResultState.totalResults, 'document')} found
{documentsFoundPlural(searchResultState.totalResults)}
</strong>
)}
</div>