Skip to content

[I18n] Fix issues and add improvements to JavaScript internationalization#2034

Merged
Crabcyborg merged 5 commits into
Strategy11:masterfrom
pedro-mendonca:i18n
Oct 18, 2024
Merged

[I18n] Fix issues and add improvements to JavaScript internationalization#2034
Crabcyborg merged 5 commits into
Strategy11:masterfrom
pedro-mendonca:i18n

Conversation

@pedro-mendonca
Copy link
Copy Markdown
Contributor

@pedro-mendonca pedro-mendonca commented Oct 8, 2024

Hi,

This PR improves the JavaScript i18n of the plugin with the below items:

  • Fix: Add missing script translations (wp_set_script_translations()).
  • Improvement: Use standard sprintf() instead of .replace().
  • Improvement: Add missing comment to translators.
  • Improvement: Add missing space before placeholder in translation string.
  • Fix: Use correct placeholder numbering in comment to translators.
  • Fix: Add missing textdomain.

Here is a screenshot of the dropdown as currently is:
imagem

Here is a screenshot after adding wp_set_script_translations( 'formidable_admin', 'formidable' );
imagem


Fixes #2033

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 8, 2024

Walkthrough

The pull request introduces several changes across multiple files, primarily focusing on enhancing internationalization and user interface functionality. Key modifications include the addition of methods in the FrmAppController for managing promotional menu items, updates to script translation handling in the FrmAddonsController, and improvements in string formatting using sprintf in various JavaScript files. Additionally, minor adjustments were made to the review message format in the FrmReviews class.

Changes

File Path Change Summary
classes/controllers/FrmAddonsController.php Added wp_set_script_translations in enqueue_assets method for script localization.
classes/controllers/FrmAppController.php Added methods for Black Friday menu management and updated the menu method.
classes/models/FrmReviews.php Adjusted string formatting in the review method.
js/admin/applications.js Updated to use sprintf for string formatting.
js/admin/embed.js Updated to use sprintf for string formatting.
js/admin/style.js Updated to use sprintf for string formatting.
js/formidable_admin.js Updated multiple function signatures for improved localization support.
stripe/helpers/FrmTransLiteListHelper.php Increased placeholders in sprintf call in get_views method.

Assessment against linked issues

Objective Addressed Explanation
Ensure JavaScript strings are properly translated (2033)

Possibly related PRs

Suggested labels

run analysis, run tests, action: needs qa

Suggested reviewers

  • Crabcyborg

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (6)
js/admin/embed.js (2)

132-138: LGTM: Consistent use of sprintf for string formatting

The string formatting in the getModalOptions function has been updated to use sprintf consistently. This change improves the handling of translations with placeholders and aligns with WordPress internationalization best practices.

Consider extracting the repeated sprintf(__(...), typeDescription) pattern into a helper function to reduce code duplication and improve maintainability. For example:

function formatWithType(string) {
    return sprintf(__(string, 'formidable'), typeDescription);
}

// Usage
const existingPageDescription = formatWithType('Embed your %s into an existing page.');

Line range hint 1-516: Overall assessment: Improved internationalization and consistency

The changes in this file consistently update string formatting to use sprintf from wp.i18n. These modifications align with WordPress internationalization best practices, improving the handling of translations with placeholders throughout the code. The updates also enhance code consistency and potentially improve accessibility, particularly in the case of the aria-label formatting.

To further improve the code:

  1. Consider extracting repeated sprintf patterns into helper functions to reduce duplication.
  2. Ensure that all translatable strings in the file have been updated to use this new formatting approach for consistency.
  3. If not already done, consider adding translator comments to provide context for these strings, which can aid in accurate translations.
js/admin/applications.js (2)

213-213: LGTM: Improved internationalization using sprintf

The use of sprintf here improves the internationalization of the string. This change allows for better handling of pluralization and positioning of variables in different languages.

Consider adding a translator comment to provide context for translators:

+		// translators: %d: Number of application templates.
		return sprintf( __( 'All Items (%d)', 'formidable' ), state.templates.length );

377-378: LGTM: Improved accessibility with internationalized aria-description

The use of sprintf for the aria-description attribute improves accessibility and internationalization. This change allows for better positioning of the template name in different languages.

Consider adding a translator comment to provide context for translators:

+		// translators: %s: Application Template Name
		const ariaDescription = sprintf( __( '%s Template', 'formidable' ), data.name );
classes/controllers/FrmAppController.php (2)

Line range hint 28-51: Avoid using anonymous functions in hooks to enhance maintainability

In maybe_add_black_friday_submenu_item(), an anonymous function is used as a callback for add_action(). This can make it difficult for other developers to remove or modify this action if needed because the function is not easily referenceable. Consider defining a named callback method instead.

Apply this diff to refactor the code:

+private static function add_black_friday_submenu() {
+   $is_black_friday = self::is_black_friday();
+   $is_cyber_monday = self::is_cyber_monday();
+
+   if ( ! $is_black_friday && ! $is_cyber_monday ) {
+       return;
+   }
+
+   $black_friday_menu_label = $is_black_friday ? __( 'Black Friday!', 'formidable' ) : __( 'Cyber Monday!', 'formidable' );
+   $black_friday_menu_label = '<span class="frm-orange-text">' . esc_html( $black_friday_menu_label ) . '</span>';
+
+   add_submenu_page( 'formidable', 'Formidable', $black_friday_menu_label, 'frm_change_settings', 'formidable-black-friday', array( __CLASS__, 'redirect_blackfriday' ) );
+}
+
-add_action(
-    'admin_menu',
-    function () use ( $black_friday_menu_label ) {
-        add_submenu_page( 'formidable', 'Formidable', $black_friday_menu_label, 'frm_change_settings', 'formidable-black-friday', 'FrmAppController::redirect_blackfriday' );
-    },
-    1000
-);
+add_action( 'admin_menu', array( __CLASS__, 'add_black_friday_submenu' ), 1000 );

Line range hint 53-63: Make sale date ranges configurable for easier maintenance

The methods is_black_friday() and is_cyber_monday() have hardcoded date ranges. To enhance flexibility and reduce the need for code changes each year, consider defining these date ranges as class constants or retrieving them from a configuration file or database.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fa47aeb and 063d09b.

📒 Files selected for processing (8)
  • classes/controllers/FrmAddonsController.php (1 hunks)
  • classes/controllers/FrmAppController.php (1 hunks)
  • classes/models/FrmReviews.php (1 hunks)
  • js/admin/applications.js (4 hunks)
  • js/admin/embed.js (6 hunks)
  • js/admin/style.js (3 hunks)
  • js/formidable_admin.js (3 hunks)
  • stripe/helpers/FrmTransLiteListHelper.php (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • classes/models/FrmReviews.php
🧰 Additional context used
🔇 Additional comments (21)
js/admin/embed.js (5)

8-8: LGTM: Import statement updated correctly

The import statement has been updated to include sprintf along with __ from wp.i18n. This change is necessary to support the new string formatting approach and follows good practices by destructuring the imports.


77-77: LGTM: Improved string formatting with sprintf

The string formatting has been updated to use sprintf, which is more consistent with WordPress internationalization best practices. This change improves code readability and makes it easier to handle translations with placeholders.


181-181: LGTM: Consistent use of sprintf in addExistingPageDropdown

The string formatting in the addExistingPageDropdown function has been updated to use sprintf, maintaining consistency with the other changes in the file. This improves the handling of translations with placeholders.


192-192: LGTM: Button text formatting updated consistently

The button text formatting in the addExistingPageDropdown function has been updated to use sprintf, maintaining consistency with the other changes in the file. This improves the handling of translations with placeholders for the button text.


451-451: LGTM: Improved accessibility with formatted aria-label

The aria-label formatting in the getCopyIcon function has been updated to use sprintf, consistent with other changes. This improvement not only maintains consistency in internationalization but also enhances accessibility by providing a properly formatted label for screen readers.

stripe/helpers/FrmTransLiteListHelper.php (1)

142-144: Improved internationalization and translation flexibility.

The changes in this section enhance the internationalization capabilities:

  1. A translator comment has been added, providing context for the string.
  2. The sprintf function now uses four placeholders instead of three, separating the closing span tag.

These modifications allow for more flexibility in translations where the order of elements might need to change, which is a good practice for internationalization.

js/admin/applications.js (3)

8-8: LGTM: Import of sprintf added

The addition of sprintf to the import statement is appropriate and aligns with the PR objective of improving string formatting. This change sets the foundation for the subsequent improvements in internationalization throughout the file.


422-423: LGTM: Improved internationalization of warning message

The use of sprintf for the warning message improves internationalization. This change allows for better positioning of the required plan name in different languages. The existing translator comment provides good context for translators.


Line range hint 1-524: Overall improvements in internationalization and string formatting

The changes in this file consistently improve internationalization and string formatting by replacing string concatenation with sprintf. These modifications align well with the PR objectives and address the issues mentioned in the linked issue #2033.

Key improvements:

  1. Addition of sprintf import
  2. Use of sprintf for better handling of variable positioning in translated strings
  3. Improved accessibility with internationalized aria-description

These changes will enhance the plugin's compatibility with different languages and improve the overall user experience for non-English speakers.

classes/controllers/FrmAddonsController.php (1)

72-72: LGTM! This change implements the missing script translations.

The addition of wp_set_script_translations( self::SCRIPT_HANDLE, 'formidable' ); correctly implements internationalization for the script, addressing one of the main objectives of this PR. This change follows WordPress best practices for script translation and will allow for proper localization of the add-ons page.

js/admin/style.js (4)

13-13: Update import statement to use object destructuring.

The import statement has been updated to include sprintf along with __. This change allows for better string formatting in internationalized messages.


155-156: Improved string formatting for internationalization.

The code now uses sprintf for formatting the "Show all" text with the number of hidden items. This change enhances readability and makes it easier to translate the string with a dynamic number.


Line range hint 1034-1067: New function to handle select placeholder colors.

This new setSelectPlaceholderColor function improves the user interface by dynamically setting the color of select placeholders. It uses modern JavaScript features like querySelectorAll, forEach, and arrow functions. The function also adds event listeners to handle future changes, which is a good practice for maintaining consistent behavior.


Line range hint 1-1073: Overall improvements to style handling and internationalization.

This update to the style.js file includes several enhancements:

  1. Improved internationalization support with the addition of sprintf for better string formatting.
  2. Enhanced pagination handling for style cards.
  3. New functionality for managing select placeholder colors.
  4. Various minor improvements and code modernization throughout the file.

These changes contribute to a better user experience and more maintainable code.

classes/controllers/FrmAppController.php (3)

Line range hint 14-14: Properly integrated Black Friday submenu item into the menu

Adding self::maybe_add_black_friday_submenu_item(); in the menu() method ensures that the promotional submenu item is conditionally added during the sale period. The implementation is correct and aligns with the intended functionality.


822-822: Internationalization: Set script translations for 'formidable_admin'

The use of wp_set_script_translations( 'formidable_admin', 'formidable' ); correctly prepares the 'formidable_admin' script for translation, ensuring that localized strings are properly loaded.


824-824: Internationalization: Set script translations for 'formidable_embed'

Similarly, wp_set_script_translations( 'formidable_embed', 'formidable' ); ensures that the 'formidable_embed' script can load the appropriate translation files, aligning with best practices for internationalization.

js/formidable_admin.js (4)

3643-3645: Looks good!

The function logic is correct and the implementation accurately generates the confirmation message using sprintf().


6113-6114: ⚠️ Potential issue

Minor typo in the error message.

There is a minor typo in the error message where "Duplicate" is misspelled as "Duplciate".

- /* translators: %s: The detected option value. */
+ /* translators: %s: The detected duplicate option value. */

Likely invalid or redundant comment.


6855-6856: ⚠️ Potential issue

Typo in the translator comment.

There is a typo in the translator comment. It should say "are not installed" instead of "is not installed".

- /* translators: %s: Form Setting section name (ie Form Permissions, Form Scheduling). */
+ /* translators: %s: Form Setting section name (e.g. Form Permissions, Form Scheduling). */

Likely invalid or redundant comment.


Line range hint 3647-3662: Verify the impact of deleting a section on the entire form.

The code looks fine for deleting a section. However, deleting a section is a significant change that can impact the entire form structure and any conditional logic or calculations that depend on fields inside the section.

I recommend adding a verification step before deleting the section:

If the script returns any results, display a warning to the user about the potential impact and confirm they still want to proceed with the deletion.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
js/formidable_admin.js (3)

3643-3643: Nitpick: Add a space after the opening curly brace.

For consistency with the existing code style, add a space after the opening curly brace.

confirmFieldsDeleteMessage( numberOfFields ) {

6113-6114: Nitpick: Add a space after the opening comment tag.

For consistency with the existing code style, add a space after the opening /* in the comment.

/* translators: %s: The detected option value. */

6855-6855: Nitpick: Add a space after the opening comment tag.

For consistency with the existing code style, add a space after the opening /* in the comment.

/* translators: %s: Form Setting section name (ie Form Permissions, Form Scheduling). */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 063d09b and f07b387.

📒 Files selected for processing (1)
  • js/formidable_admin.js (4 hunks)
🧰 Additional context used

Comment thread js/formidable_admin.js
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg left a comment

Choose a reason for hiding this comment

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

Thank you @pedro-mendonca!

This all looks good to me!

I'll go ahead and merge this.

🚀

@Crabcyborg Crabcyborg merged commit b4ecc7f into Strategy11:master Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some JavaScript strings are missing translation

2 participants