Skip to content

fix(audio-captions): label generation for audio captions with custom locales - #21279

Merged
antobinary merged 1 commit into
bigbluebutton:v3.0.x-releasefrom
ramonlsouza:fix-custom-locale
Sep 30, 2024
Merged

antobinary merged 1 commit into
bigbluebutton:v3.0.x-releasefrom
ramonlsouza:fix-custom-locale

Conversation

@ramonlsouza

@ramonlsouza ramonlsouza commented Sep 27, 2024 •

Copy link
Copy Markdown
Member

What does this PR do?

Refactors the label generation for audio captions in the audio-captions button and captions components. Instead of directly using the intl formatMessage function, the label is now generated using the intlMessages object and the getLocaleName function from the audio-captions service. This change prevents an issue with custom locales.

How to test

  1. Add a new locale in public.app.audioCaptions.language.available settings
  2. Join a meeting
  3. Try to select a locale for transcription in the audio join modal

…locales

This commit refactors the label generation for audio captions in the audio-captions button and captions components. Instead of directly using the intl formatMessage function, the label is now generated using the intlMessages object and the getLocaleName function from the audio-captions service. This change prevents an issue with custom locales.
@ramonlsouza ramonlsouza added this to the Release 3.0 milestone Sep 27, 2024
@coderabbitai

coderabbitai Bot commented Sep 27, 2024 •

Copy link
Copy Markdown
Contributor

Walkthrough

The changes involve modifications to the AudioCaptionsButton and AudioCaptionsSelect components to enhance the label and message rendering logic. A new function, getLocaleName, has been introduced in the service module to retrieve display names for locales using the Intl.DisplayNames API. The components now include conditional checks to either format messages or fall back to locale names when specific messages are unavailable.

Changes

File Change Summary
bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/button/component.tsx
bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/captions/component.tsx
Updated label/message rendering logic to check for existence in intlMessages and fallback to getLocaleName if not found.
bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/service.ts Added getLocaleName(locale: string) function to retrieve display names for locales and included it in the default export.

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.

@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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)
bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/service.ts (1)

87-92: Approve implementation with a suggestion for error handling

The getLocaleName function is well-implemented, utilizing the Intl.DisplayNames API correctly to retrieve localized language names. This approach aligns with best practices for internationalization.

Consider adding error handling to make the function more robust:

export const getLocaleName = (locale: string): string => {
  try {
    const languageNames = new Intl.DisplayNames([locale], {
      type: 'language',
    });
    return languageNames.of(locale) || locale;
  } catch (error) {
    console.error(`Error getting display name for locale ${locale}:`, error);
    return locale;
  }
};

This modification ensures that:

  1. If the Intl.DisplayNames API is not supported, the function falls back to returning the original locale string.
  2. Any errors during the process are logged, aiding in debugging if issues arise.
  3. The function always returns a string, maintaining consistent behavior.
bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/captions/component.tsx (1)

156-160: Improved handling of custom locales with a minor suggestion

The new conditional rendering logic for select options is a good improvement. It ensures that even when a specific message is not defined for a locale, a readable locale name will be displayed using the getLocaleName function. This change aligns well with the PR objective of improving label generation for audio captions with custom locales.

However, there's a minor suggestion for improving type safety:

Consider using a type guard to ensure type safety when accessing intlMessages. Here's a suggested improvement:

{
  const message = intlMessages[v as keyof typeof intlMessages];
  message
    ? intl.formatMessage(message)
    : getLocaleName(v)
}

This change will ensure that intl.formatMessage is only called with a valid message object, improving type safety.

bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/button/component.tsx (1)

165-169: Approve changes with a minor suggestion for improvement

The changes effectively address the PR objective of improving label generation for custom locales. The new implementation provides a fallback mechanism using AudioCaptionsService.getLocaleName(availableVoice) when a predefined intl message is not available. This enhancement ensures that labels can be generated for a wider range of locales, including custom ones.

To improve readability, consider using object destructuring for intlMessages. This would make the code more concise and easier to understand. Here's a suggested refactor:

const { [availableVoice]: message } = intlMessages;
const label = message
  ? intl.formatMessage(message)
  : AudioCaptionsService.getLocaleName(availableVoice);

This change reduces the need for the type assertion and makes the code more idiomatic TypeScript.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a52a364 and 365a5e2.

📒 Files selected for processing (3)
  • bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/button/component.tsx (1 hunks)
  • bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/captions/component.tsx (2 hunks)
  • bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/service.ts (2 hunks)
🔇 Additional comments (5)
bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/service.ts (2)

104-104: Approve addition of getLocaleName to default export

The inclusion of getLocaleName in the default export is correct and necessary. This change ensures that the new function is accessible to other modules that import this service, allowing them to utilize the localized language name functionality.


Line range hint 87-104: Summary: Changes align well with PR objectives

The addition of the getLocaleName function and its inclusion in the default export effectively address the PR objectives. This implementation provides a robust way to generate localized language names, which is crucial for improving label generation for audio captions with custom locales.

The changes are focused and well-implemented, enhancing the internationalization capabilities of the audio captions feature. This improvement will contribute to a better user experience across different locale settings.

bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/captions/component.tsx (2)

11-11: LGTM: Import of getLocaleName function

The addition of the getLocaleName import is consistent with the PR's objective to improve label generation for audio captions with custom locales. This function will likely be used to retrieve locale names when specific messages are not available.


Line range hint 1-205: Summary: Effective implementation of custom locale handling

The changes in this file successfully address the PR's objective of improving label generation for audio captions with custom locales. The implementation is clean, maintainable, and provides a good fallback mechanism for handling custom locales.

Key improvements:

  1. Addition of the getLocaleName function import.
  2. Implementation of conditional rendering logic for select options, ensuring proper handling of custom locales.

These changes should enhance the user experience by providing more accurate and consistent labeling for audio captions across different locale settings.

bigbluebutton-html5/imports/ui/components/audio/audio-graphql/audio-captions/button/component.tsx (1)

Line range hint 1-324: Overall assessment: Changes effectively address the PR objectives

The modifications to the AudioCaptionsButton component successfully improve the label generation process for audio captions, particularly for custom locales. The changes are well-integrated into the existing codebase and align with the PR objectives.

Key improvements:

  1. Enhanced flexibility in label generation, supporting both predefined and custom locales.
  2. Fallback mechanism using AudioCaptionsService.getLocaleName for locales without predefined messages.
  3. Maintained consistency with the overall component structure and functionality.

These changes should provide a more robust and inclusive user experience for audio captions across various locale settings.

Comment on lines +165 to +169

const label = intlMessages[availableVoice as keyof typeof intlMessages]
? intl.formatMessage(intlMessages[availableVoice as keyof typeof intlMessages])
: AudioCaptionsService.getLocaleName(availableVoice);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Add Error Handling to AudioCaptionsService.getLocaleName

The getLocaleName function currently utilizes Intl.DisplayNames for generating locale names but lacks proper error handling. To ensure robustness, especially when dealing with unexpected or invalid locale strings, it's crucial to implement error management within this function.

Recommendations:

  • Introduce try-catch blocks to handle potential exceptions from Intl.DisplayNames.
  • Validate input locale strings before processing.
  • Provide fallback mechanisms in case of errors to maintain application stability.

Example Enhancement:

export const getLocaleName = (locale: string) => {
  try {
    const languageNames = new Intl.DisplayNames([locale], { type: 'language' });
    return languageNames.of(locale) || 'Unknown';
  } catch (error) {
    console.error(`Error getting locale name for ${locale}:`, error);
    return 'Unknown';
  }
};
🔗 Analysis chain

Verify the implementation of AudioCaptionsService.getLocaleName

The changes introduce the use of AudioCaptionsService.getLocaleName(availableVoice) as a fallback for generating labels. To ensure robustness, it's crucial to verify that this function is implemented correctly and can handle all possible locale strings, including edge cases.

Please run the following script to check the implementation of getLocaleName:

This script will help us verify:

  1. The existence and basic structure of the getLocaleName function.
  2. Proper error handling within the function.
  3. The use of Intl.DisplayNames for standardized locale name generation.

Please review the results to ensure the function is robust and handles various locale formats correctly.


Retry Verification with Corrected Shell Scripts

The previous script encountered errors due to incorrect file type and regex syntax. Please run the following corrected script to verify the implementation of AudioCaptionsService.getLocaleName:

This revised script addresses the previous issues by:

  1. Using --type ts to match TypeScript files.
  2. Correcting the regex patterns to properly escape characters and match the function definition syntax.

Please execute this script and provide the output to continue the verification process.


Retry Verification with Enhanced Shell Scripts

The previous attempts encountered issues due to incorrect file paths and regex syntax errors. To effectively verify the implementation of AudioCaptionsService.getLocaleName, please run the following improved script:

This script performs the following actions:

  1. Locates the File: Searches for TypeScript files containing AudioCaptionsService to dynamically identify the correct file path.
  2. Finds Function Definition: Searches for the getLocaleName function definition within the identified file.
  3. Checks Error Handling: Looks for try, catch, or throw statements within the getLocaleName function to ensure proper error handling.
  4. Verifies Intl.DisplayNames Usage: Confirms that Intl.DisplayNames is utilized within the getLocaleName function for standardized locale name generation.

Please execute this script and provide the output to proceed with the verification.


Retry Verification by Searching the Entire Codebase for getLocaleName

To accurately locate and verify the implementation of AudioCaptionsService.getLocaleName, please run the following enhanced shell script:

This script performs the following actions:

  1. Locate All References: Searches the entire codebase for occurrences of getLocaleName within TypeScript files.
  2. Identify Function Definitions: Attempts to find the function definition using both standard function declarations and arrow function expressions.
  3. Check Error Handling: Looks for try, catch, or throw statements within the getLocaleName function to ensure proper error handling.
  4. Verify Intl.DisplayNames Usage: Confirms that Intl.DisplayNames is utilized within the getLocaleName function for standardized locale name generation.

Please execute this script and provide the output to continue the verification process.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of AudioCaptionsService.getLocaleName

# Test: Search for the getLocaleName function definition
echo "Searching for getLocaleName function definition:"
rg --type typescript "getLocaleName.*=.*" ./imports/ui/components/audio/audio-graphql/audio-captions/service.ts

# Test: Check for error handling in getLocaleName
echo "Checking for error handling in getLocaleName:"
rg --type typescript "getLocaleName.*{" -A 10 ./imports/ui/components/audio/audio-graphql/audio-captions/service.ts | rg "try|catch|throw"

# Test: Look for usage of Intl.DisplayNames in getLocaleName
echo "Checking for usage of Intl.DisplayNames:"
rg --type typescript "getLocaleName.*{" -A 10 ./imports/ui/components/audio/audio-graphql/audio-captions/service.ts | rg "Intl\.DisplayNames"

Length of output: 924


Script:

# Updated Script: Verify the implementation of AudioCaptionsService.getLocaleName

#!/bin/bash

# Test: Search for the getLocaleName function definition
echo "Searching for getLocaleName function definition:"
rg --type ts "getLocaleName\s*=\s*" ./imports/ui/components/audio/audio-graphql/audio-captions/service.ts

# Test: Check for error handling in getLocaleName
echo "Checking for error handling in getLocaleName:"
rg --type ts "getLocaleName\s*\(.*\)\s*{\s*" ./imports/ui/components/audio/audio-graphql/audio-captions/service.ts -A 10 | rg "try|catch|throw"

# Test: Look for usage of Intl.DisplayNames in getLocaleName
echo "Checking for usage of Intl.DisplayNames:"
rg --type ts "getLocaleName\s*\(.*\)\s*{\s*" ./imports/ui/components/audio/audio-graphql/audio-captions/service.ts -A 10 | rg "Intl\.DisplayNames"

Length of output: 1087


Script:

# Enhanced Script: Verify the implementation of AudioCaptionsService.getLocaleName

#!/bin/bash

# Step 1: Locate the file containing AudioCaptionsService
echo "Locating files containing 'AudioCaptionsService':"
files=$(rg --files-with-matches "AudioCaptionsService" --type ts)
if [ -z "$files" ]; then
  echo "No TypeScript files contain 'AudioCaptionsService'. Please verify the correct file path."
  exit 1
fi
echo "Found files:"
echo "$files"

# Assuming there's only one relevant file, adjust if multiple files are found
service_file=$(echo "$files" | head -n 1)

# Step 2: Search for the getLocaleName function definition
echo "Searching for getLocaleName function definition in $service_file:"
rg "function\s+getLocaleName\s*\(" "$service_file"

# Step 3: Check for error handling within getLocaleName
echo "Checking for error handling in getLocaleName:"
rg -A 10 "function\s+getLocaleName\s*\(" "$service_file" | rg "try|catch|throw"

# Step 4: Verify usage of Intl.DisplayNames in getLocaleName
echo "Checking for usage of Intl.DisplayNames in getLocaleName:"
rg -A 10 "function\s+getLocaleName\s*\(" "$service_file" | rg "Intl\.DisplayNames"

Length of output: 1860


Script:

#!/bin/bash

# Enhanced Script: Comprehensive Verification of AudioCaptionsService.getLocaleName

# Step 1: Locate all TypeScript files containing 'getLocaleName'
echo "Locating all TypeScript files containing 'getLocaleName':"
files=$(rg "getLocaleName" --type ts)
if [ -z "$files" ]; then
  echo "No TypeScript files contain 'getLocaleName'. Please verify its implementation exists."
  exit 1
fi
echo "Found files:"
echo "$files"

# Extract unique file paths
unique_files=$(echo "$files" | awk -F ':' '{print $1}' | sort | uniq)

# Iterate over each file to find the function definition
for file in $unique_files; do
  echo "Searching for function definition in $file:"
  rg "function\s+getLocaleName\s*\(" "$file" || rg "const\s+getLocaleName\s*=\s*\(" "$file"
  
  # Check for error handling within the function
  echo "Checking for error handling in getLocaleName within $file:"
  rg -A 10 "function\s+getLocaleName\s*\(" "$file" | rg "try|catch|throw" || rg -A 10 "const\s+getLocaleName\s*=\s*\(" "$file" | rg "try|catch|throw"
  
  # Verify usage of Intl.DisplayNames in the function
  echo "Checking for usage of Intl.DisplayNames in getLocaleName within $file:"
  rg -A 10 "function\s+getLocaleName\s*\(" "$file" | rg "Intl\.DisplayNames" || rg -A 10 "const\s+getLocaleName\s*=\s*\(" "$file" | rg "Intl\.DisplayNames"
done

Length of output: 9536

@github-actions

Copy link
Copy Markdown

Automated tests Summary

✅ All the CI tests have passed!

@antobinary
antobinary merged commit f283f11 into bigbluebutton:v3.0.x-release Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants