Skip to content

Commit

Permalink
[I18n] Fix plugin internationalization (#430)
Browse files Browse the repository at this point in the history
* Sets translated strings for the script 'create-block-theme-app'

* Use proper sprintf on %d Variants

* Add missing gettext to UI string

* Fix placeholder string

* Update i18n

* Update comment according to upstream.

* Fix assets path

* Enable localization in the plugin sidebar

* Enable localization in the form

* Add missing text domains to the plugin sidebar

* Fix loading of 'Remove' l10n

* Localize Font Styles

* Add missing text domain

* Add missing gettext wrapper

* Remove duplicated import

* Rename getFontStyleLocalized to localizeFontStyles

* Fix typo

---------

Co-authored-by: Vicente Canales <[email protected]>
  • Loading branch information
pedro-mendonca and Vicente Canales authored Jul 28, 2023
1 parent 3a7774a commit e9c82db
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 54 deletions.
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() {
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, localizeFontStyle } from '../utils';
import './fonts-sidebar.css';
import { Button } from '@wordpress/components';
import { trash } from '@wordpress/icons';
Expand Down Expand Up @@ -88,23 +88,31 @@ function FontsSidebar( {
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 @@ function FontsSidebar( {
>
<div className="variant">
{ face.weight }{ ' ' }
{ face.style }
{ localizeFontStyle(
face.style
) }
</div>
<div className="size">
{ getFileSize(
Expand Down Expand Up @@ -171,12 +181,15 @@ function FontsSidebar( {

<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
3 changes: 2 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 { localizeFontStyle } from '../utils';

function FontVariant( { font, variant, isSelected, handleToggle } ) {
const style = variant.includes( 'italic' ) ? 'italic' : 'normal';
Expand Down Expand Up @@ -51,7 +52,7 @@ function FontVariant( { font, variant, isSelected, handleToggle } ) {
<label htmlFor={ fontId }>{ weight }</label>
</td>
<td className="">
<label htmlFor={ fontId }>{ style }</label>
<label htmlFor={ fontId }>{ localizeFontStyle( 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 { localizeFontStyle } 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">
{ localizeFontStyle( 'normal' ) }
</option>
<option value="italic">
{ localizeFontStyle( '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 { localizeFontStyle } 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,
localizeFontStyle( 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 { localizeFontStyle } 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>{ localizeFontStyle( 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 localizeFontStyle( style ) {
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

0 comments on commit e9c82db

Please sign in to comment.