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] Fix plugin internationalization #430

Merged
merged 17 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
3 changes: 3 additions & 0 deletions admin/class-create-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ function create_block_theme_enqueue() {
wp_enqueue_script(
'create-block-theme-slot-fill',
);

// Enable localization in the plugin sidebar.
wp_set_script_translations( 'create-block-theme-slot-fill', 'create-block-theme' );
}

function create_admin_menu() {
Expand Down
3 changes: 3 additions & 0 deletions admin/class-react-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public static function bootstrap() {
array_push( $asset_file['dependencies'], 'wp-i18n' );
wp_enqueue_script( 'create-block-theme-app', plugins_url( 'build/index.js', __DIR__ ), $asset_file['dependencies'], $asset_file['version'] );

// Enable localization in the app.
wp_set_script_translations( 'create-block-theme-app', 'create-block-theme' );

// Set google fonts json file url.
wp_localize_script(
'create-block-theme-app',
Expand Down
7 changes: 5 additions & 2 deletions admin/create-theme/theme-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ public static function create_admin_form_page() {
}

public static function form_script() {
Copy link
Member

Choose a reason for hiding this comment

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

This is a great catch; it was frustrating me quite a bit, as it wasn't working with the extra / present.

It's working for me now.

wp_enqueue_script( 'form-script', plugin_dir_url( dirname( __FILE__ ) ) . '/js/form-script.js' );
wp_enqueue_style( 'form-style', plugin_dir_url( dirname( __FILE__ ) ) . '/css/form.css' );
wp_enqueue_script( 'form-script', plugin_dir_url( dirname( __FILE__ ) ) . 'js/form-script.js' );
wp_enqueue_style( 'form-style', plugin_dir_url( dirname( __FILE__ ) ) . 'css/form.css' );

// Enable localization in the form.
wp_set_script_translations( 'form-script', 'create-block-theme' );
}
}
2 changes: 1 addition & 1 deletion src/demo-text-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function DemoTextInput( { axes, setAxes, resetAxes } ) {
</SelectControl>

<InputControl
label="Demo text"
label={ __( 'Demo text', 'create-block-theme' ) }
value={ demoText }
onChange={ handleDemoTextChange }
/>
Expand Down
2 changes: 1 addition & 1 deletion src/editor-sidebar/update-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const UpdateThemePanel = () => {
) }
<br />
<ExternalLink href="https://make.wordpress.org/themes/handbook/review/required/#6-plugins">
{ __( 'Read more.' ) }
{ __( 'Read more.', 'create-block-theme' ) }
</ExternalLink>
</>
}
Expand Down
63 changes: 38 additions & 25 deletions src/fonts-sidebar/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { _n } from '@wordpress/i18n';
import { useEffect, useState } from '@wordpress/element';
import { bytesToSize } from '../utils';
import { bytesToSize, getFontStyleLocalized } from '../utils';
import './fonts-sidebar.css';
import { Button } from '@wordpress/components';
import { trash } from '@wordpress/icons';
Expand Down Expand Up @@ -39,7 +39,7 @@
} );
setFileSizes( sizes );
} );
}, [ fontsOutline ] );

Check warning on line 42 in src/fonts-sidebar/index.js

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'flatfontsOutline'. Either include it or remove the dependency array

const variantsCount = Object.keys( fontsOutline ).reduce(
( acc, family ) => {
Expand Down Expand Up @@ -88,23 +88,31 @@
key
].faces.length && (
<>
({ ' ' }
{
fontsOutline[
key
].faces
.length
}{ ' ' }
{ _n(
'Variant',
'Variants',
fontsOutline[
key
].faces
.length,
'create-block-theme'
) }{ ' ' }
)
{ sprintf(
// translators: %s: Variants information.
__(
'( %s )',
'create-block-theme'
),
sprintf(
// translators: %d: Number of variants.
_n(
'%d Variant',
'%d Variants',
fontsOutline[
key
]
.faces
.length,
'create-block-theme'
),
fontsOutline[
key
]
.faces
.length
)
) }
</>
) }
</span>
Expand All @@ -130,7 +138,9 @@
>
<div className="variant">
{ face.weight }{ ' ' }
{ face.style }
{ getFontStyleLocalized(
face.style
) }
</div>
<div className="size">
{ getFileSize(
Expand Down Expand Up @@ -171,12 +181,15 @@

<div className="variants-total">
<div className="variant">
{ variantsCount }{ ' ' }
{ _n(
'Variant',
'Variants',
variantsCount,
'create-block-theme'
{ sprintf(
// translators: %d: Number of variants.
_n(
'%d Variant',
'%d Variants',
variantsCount,
'create-block-theme'
),
variantsCount
) }
</div>
<div className="size">{ totalSize }</div>
Expand Down
5 changes: 4 additions & 1 deletion src/google-fonts/font-variant.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect } from '@wordpress/element';
import Demo from '../demo-text-input/demo';
import { getFontStyleLocalized } from '../utils';

function FontVariant( { font, variant, isSelected, handleToggle } ) {
const style = variant.includes( 'italic' ) ? 'italic' : 'normal';
Expand Down Expand Up @@ -30,7 +31,7 @@
// eslint-disable-next-line
console.error( error );
} );
}, [ font, variant ] );

Check warning on line 34 in src/google-fonts/font-variant.js

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has missing dependencies: 'style', 'variantUrl', and 'weight'. Either include them or remove the dependency array

const formattedFontFamily = font.family.toLowerCase().replace( ' ', '-' );
const fontId = `${ formattedFontFamily }-${ variant }`;
Expand All @@ -51,7 +52,9 @@
<label htmlFor={ fontId }>{ weight }</label>
</td>
<td className="">
<label htmlFor={ fontId }>{ style }</label>
<label htmlFor={ fontId }>
{ getFontStyleLocalized( style ) }
</label>
</td>
<td className="demo-cell">
<label htmlFor={ fontId }>
Expand Down
11 changes: 8 additions & 3 deletions src/local-fonts/upload-font-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { Font } from 'lib-font';
import { __ } from '@wordpress/i18n';
import { variableAxesToCss } from '../demo-text-input/utils';
import { getFontStyleLocalized } from '../utils';

function UploadFontForm( {
formData,
Expand Down Expand Up @@ -168,8 +169,12 @@ function UploadFontForm( {
setFormData( { ...formData, style: val } )
}
>
<option value="normal">Normal</option>
<option value="italic">Italic</option>
<option value="normal">
{ getFontStyleLocalized( 'normal' ) }
</option>
<option value="italic">
{ getFontStyleLocalized( 'italic' ) }
</option>
</SelectControl>
</div>

Expand All @@ -180,7 +185,7 @@ function UploadFontForm( {
name="font-weight"
id="font-weight"
placeholder={ __(
'Font weight:',
'Font weight',
'create-block-theme'
) }
value={ formData.weight || '' }
Expand Down
3 changes: 2 additions & 1 deletion src/manage-fonts/confirm-delete-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
__experimentalConfirmDialog as ConfirmDialog,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { getFontStyleLocalized } from '../utils';

function ConfirmDeleteModal( { isOpen, onConfirm, onCancel, fontToDelete } ) {
const deleteFontFaceMessage = sprintf(
Expand All @@ -12,7 +13,7 @@ function ConfirmDeleteModal( { isOpen, onConfirm, onCancel, fontToDelete } ) {
'create-block-theme'
),
fontToDelete?.weight,
fontToDelete?.style,
getFontStyleLocalized( fontToDelete?.style ),
fontToDelete?.fontFamily
);

Expand Down
5 changes: 3 additions & 2 deletions src/manage-fonts/font-face.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Button } from '@wordpress/components';
import Demo from '../demo-text-input/demo';
const { __ } = wp.i18n;
import { __ } from '@wordpress/i18n';
import { getFontStyleLocalized } from '../utils';

function FontFace( { face, deleteFont, shouldBeRemoved, isFamilyOpen } ) {
const demoStyles = {
Expand All @@ -21,7 +22,7 @@ function FontFace( { face, deleteFont, shouldBeRemoved, isFamilyOpen } ) {

return (
<tr className="font-face">
<td>{ face.fontStyle }</td>
<td>{ getFontStyleLocalized( face.fontStyle ) }</td>
<td>{ face.fontWeight }</td>
<td className="demo-cell">
<Demo style={ demoStyles } />
Expand Down
24 changes: 15 additions & 9 deletions src/manage-fonts/font-family.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useContext } from '@wordpress/element';
import { Button, Icon } from '@wordpress/components';
import FontFace from './font-face';
import { ManageFontsContext } from '../fonts-context';
import { __, _n } from '@wordpress/i18n';

const { __, _n } = wp.i18n;
function FontFamily( { fontFamily, deleteFont } ) {
const { familiesOpen, handleToggleFamily } =
useContext( ManageFontsContext );
Expand Down Expand Up @@ -36,14 +36,20 @@ function FontFamily( { fontFamily, deleteFont } ) {
{ hasFontFaces && (
<span className="variants-count">
{ ' ' }
( { fontFamily.fontFace.length }{ ' ' }
{ _n(
'Variant',
'Variants',
fontFamily.fontFace.length,
'create-block-theme'
) }{ ' ' }
)
{ sprintf(
// translators: %s: Variants information.
__( '( %s )', 'create-block-theme' ),
sprintf(
// translators: %d: Number of variants.
_n(
'%d Variant',
'%d Variants',
fontFamily.fontFace.length,
'create-block-theme'
),
fontFamily.fontFace.length
)
) }
</span>
) }
</div>
Expand Down
34 changes: 26 additions & 8 deletions src/plugin-sidebar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerPlugin } from '@wordpress/plugins';
import { PluginSidebar, PluginSidebarMoreMenuItem } from '@wordpress/edit-site';
import { __ } from '@wordpress/i18n';
import { __, _x } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { downloadFile } from './utils';
import { useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -88,7 +88,8 @@ const CreateBlockThemePlugin = () => {
error.message && error.code !== 'unknown_error'
? error.message
: __(
'An error occurred while attempting to export the theme.'
'An error occurred while attempting to export the theme.',
'create-block-theme'
);
createErrorNotice( errorMessage, { type: 'snackbar' } );
}
Expand All @@ -102,12 +103,20 @@ const CreateBlockThemePlugin = () => {
target="create-block-theme-sidebar"
icon={ tool }
>
{ __( 'Create Block Theme' ) }
{ _x(
'Create Block Theme',
'UI String',
'create-block-theme'
) }
</PluginSidebarMoreMenuItem>
<PluginSidebar
name="create-block-theme-sidebar"
icon={ tool }
title={ __( 'Create Block Theme' ) }
title={ _x(
'Create Block Theme',
'UI String',
'create-block-theme'
) }
>
<NavigatorProvider initialPath="/">
<NavigatorScreen path="/">
Expand All @@ -117,7 +126,10 @@ const CreateBlockThemePlugin = () => {
icon={ archive }
onClick={ handleSaveClick }
>
{ __( 'Save Changes' ) }
{ __(
'Save Changes',
'create-block-theme'
) }
</Button>
<Text variant="muted">
{ __(
Expand All @@ -130,7 +142,7 @@ const CreateBlockThemePlugin = () => {
icon={ download }
onClick={ handleExportClick }
>
{ __( 'Export Zip' ) }
{ __( 'Export Zip', 'create-block-theme' ) }
</Button>
<Text variant="muted">
{ __(
Expand All @@ -143,7 +155,10 @@ const CreateBlockThemePlugin = () => {
<Spacer />
<HStack justify="space-between">
<FlexItem>
{ __( 'Theme Info' ) }
{ __(
'Theme Info',
'create-block-theme'
) }
</FlexItem>
<Icon icon={ chevronRight } />
</HStack>
Expand All @@ -159,7 +174,10 @@ const CreateBlockThemePlugin = () => {
<Spacer />
<HStack>
<FlexItem>
{ __( 'Create Theme' ) }
{ __(
'Create Theme',
'create-block-theme'
) }
</FlexItem>
<Icon icon={ chevronRight } />
</HStack>
Expand Down
12 changes: 11 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { __, _x } from '@wordpress/i18n';

export function getStyleFromGoogleVariant( variant ) {
return variant.includes( 'italic' ) ? 'italic' : 'normal';
}
Expand All @@ -21,13 +23,21 @@ export function getGoogleVariantFromStyleAndWeight( style, weight ) {
return weight + style;
}

export function getFontStyleLocalized( style ) {
vcanales marked this conversation as resolved.
Show resolved Hide resolved
const styles = {
normal: _x( 'Normal', 'Font style', 'create-block-theme' ),
italic: _x( 'Italic', 'Font style', 'create-block-theme' ),
};
return styles[ style ] !== undefined ? styles[ style ] : style;
}

export function forceHttps( url ) {
return url.replace( 'http://', 'https://' );
}

export function bytesToSize( bytes ) {
const sizes = [ 'Bytes', 'KB', 'MB', 'GB', 'TB' ];
if ( bytes === 0 ) return 'n/a';
if ( bytes === 0 ) return __( 'n/a', 'create-block-theme' );
const i = parseInt( Math.floor( Math.log( bytes ) / Math.log( 1024 ) ) );
if ( i === 0 ) return bytes + ' ' + sizes[ i ];
return ( bytes / Math.pow( 1024, i ) ).toFixed( 1 ) + ' ' + sizes[ i ];
Expand Down
Loading