Skip to content
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

i18n: Replace sprintf-js with @tannin/sprintf #65273

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
"@wordpress/viewport": "file:packages/viewport",
"@wordpress/warning": "file:packages/warning",
"@wordpress/widgets": "file:packages/widgets",
"@wordpress/wordcount": "file:packages/wordcount"
"@wordpress/wordcount": "file:packages/wordcount",
"@tannin/sprintf": "1.2.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: alphabetical order

},
"devDependencies": {
"@actions/core": "1.9.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function MenuTitleSearch( {
const placeholder = sprintf(
/* translators: placeholder for menu search box. %s: menu title */
__( 'Search %s' ),
title?.toLowerCase()
title?.toLowerCase() ?? ''
).trim();

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/palette-edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function Option< T extends Color | Gradient >( {
slugPrefix,
isGradient,
}: OptionProps< T > ) {
const value = isGradient ? element.gradient : element.color;
const value = isGradient ? element.gradient! : element.color!;
const [ isEditingColor, setIsEditingColor ] = useState( false );

// Use internal state instead of a ref to make sure that the component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const permanentlyDeletePost: Action< PostWithPermissions > = {
}
// If we were trying to permanently delete multiple posts
} else {
const errorMessages = new Set();
const errorMessages = new Set< string >();
const failedPromises = promiseResult.filter(
( { status } ) => status === 'rejected'
);
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/dataviews/actions/restore-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const restorePost: Action< PostWithPermissions > = {
}
// If we were trying to move multiple posts to the trash
} else {
const errorMessages = new Set();
const errorMessages = new Set< string >();
const failedPromises = promiseResult.filter(
( { status } ) => status === 'rejected'
);
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/dataviews/actions/trash-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const trashPost: Action< PostWithPermissions > = {
}
// If we were trying to delete multiple items.
} else {
const errorMessages = new Set();
const errorMessages = new Set< string >();
const failedPromises = promiseResult.filter(
( { status } ) => status === 'rejected'
);
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Returns a formatted string. If an error occurs in applying the format, the origi

_Related_

- <https://www.npmjs.com/package/sprintf-js>
- <https://www.npmjs.com/package/@tannin/sprintf>

_Parameters_

Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
},
"dependencies": {
"@babel/runtime": "^7.16.0",
"@tannin/sprintf": "^1.2.0",
"@wordpress/hooks": "file:../hooks",
"gettext-parser": "^1.3.1",
"memize": "^2.1.0",
"sprintf-js": "^1.1.1",
"tannin": "^1.2.0"
},
"publishConfig": {
Expand Down
25 changes: 3 additions & 22 deletions packages/i18n/src/sprintf.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/**
* External dependencies
*/
import memoize from 'memize';
import sprintfjs from 'sprintf-js';

/**
* Log to console, once per message; or more precisely, per referentially equal
* argument set. Because Jed throws errors, we log these to the console instead
* to avoid crashing the application.
*
* @param {...*} args Arguments to pass to `console.error`
*/
const logErrorOnce = memoize( console.error ); // eslint-disable-line no-console
import sprintf from '@tannin/sprintf';

/**
* Returns a formatted string. If an error occurs in applying the format, the
Expand All @@ -20,17 +10,8 @@ const logErrorOnce = memoize( console.error ); // eslint-disable-line no-console
* @param {string} format The format of the string to generate.
* @param {...*} args Arguments to apply to the format.
*
* @see https://www.npmjs.com/package/sprintf-js
* @see https://www.npmjs.com/package/@tannin/sprintf
*
* @return {string} The formatted string.
*/
export function sprintf( format, ...args ) {
try {
return sprintfjs.sprintf( format, ...args );
} catch ( error ) {
if ( error instanceof Error ) {
logErrorOnce( 'sprintf error: \n\n' + error.toString() );
}
return format;
}
}
export { sprintf };
9 changes: 0 additions & 9 deletions packages/i18n/src/test/sprintf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import { sprintf } from '../sprintf';

describe( 'i18n', () => {
describe( 'sprintf', () => {
it( 'absorbs errors', () => {
// Disable reason: Failing case is the purpose of the test.
// eslint-disable-next-line @wordpress/valid-sprintf
const result = sprintf( 'Hello %(placeholder-not-provided)s' );

expect( console ).toHaveErrored();
expect( result ).toBe( 'Hello %(placeholder-not-provided)s' );
} );

it( 'replaces placeholders', () => {
const result = sprintf( 'bonjour %s', 'Riad' );

Expand Down
12 changes: 12 additions & 0 deletions patches/@tannin+sprintf+1.2.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/node_modules/@tannin/sprintf/index.d.ts b/node_modules/@tannin/sprintf/index.d.ts
index 1ac529e..41d5e9b 100644
--- a/node_modules/@tannin/sprintf/index.d.ts
+++ b/node_modules/@tannin/sprintf/index.d.ts
@@ -19,6 +19,4 @@
*
* @return {string} Formatted string.
*/
-export default function sprintf(string: string, ...args: (string | string[] | {
- [x: string]: string;
-})[]): string;
+export default function sprintf(string: string, ...args: ReadonlyArray<string | number>): string;
Loading