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

add translation example #112

Merged
merged 11 commits into from
Jun 22, 2024
Merged

add translation example #112

merged 11 commits into from
Jun 22, 2024

Conversation

jamalsoueidan
Copy link
Owner

@jamalsoueidan jamalsoueidan commented Jun 21, 2024

PR Type

Enhancement, Dependencies


Description

  • Integrated i18next for both client-side and server-side internationalization.
  • Created initI18Next function to initialize and configure i18next.
  • Added Danish and English translation JSON files.
  • Updated Vite configuration for SSR optimization.
  • Added necessary dependencies for i18next and react-i18next.

Changes walkthrough 📝

Relevant files
Enhancement
6 files
entry.client.tsx
Add client-side internationalization using i18next.           

app/entry.client.tsx

  • Integrated i18next for client-side internationalization.
  • Wrapped RemixBrowser with I18nextProvider.
  • Added error handling for initI18Next.
  • +18/-8   
    entry.server.tsx
    Add server-side internationalization using i18next.           

    app/entry.server.tsx

  • Integrated i18next for server-side internationalization.
  • Wrapped RemixServer with I18nextProvider.
  • Initialized i18next with server-side language context.
  • +8/-1     
    i18n.ts
    Initialize and configure i18next for internationalization.

    app/i18n.ts

  • Created initI18Next function for initializing i18next.
  • Configured i18next with supported languages and backend options.
  • Added resource bundles for server-side rendering.
  • +49/-0   
    root.tsx
    Integrate useTranslation hook and update HTML lang attribute.

    app/root.tsx

  • Integrated useTranslation hook for translations.
  • Updated HTML lang attribute based on i18n context.
  • Displayed translated text using t function.
  • +10/-5   
    translation.json
    Add Danish translation JSON file.                                               

    app/assets/locales/da/translation.json

    • Added Danish translations for "welcome" and "hello".
    +4/-0     
    translation.json
    Add English translation JSON file.                                             

    app/assets/locales/en/translation.json

    • Added English translations for "welcome" and "hello".
    +4/-0     
    Configuration changes
    1 files
    vite.config.ts
    Update Vite configuration for SSR optimization.                   

    vite.config.ts

  • Adjusted import order for Vite plugins.
  • Added SSR optimization for void-elements.
  • +7/-2     
    Dependencies
    2 files
    package-lock.json
    Update package-lock.json with i18next dependencies.           

    package-lock.json

  • Added dependencies for i18next, react-i18next, and related packages.
  • +139/-0 
    package.json
    Update package.json with i18next dependencies.                     

    package.json

  • Added dependencies for i18next, react-i18next, and related packages.
  • +6/-0     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @github-actions github-actions bot added enhancement New feature or request dependencies labels Jun 21, 2024
    Copy link

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 3
    🧪 Relevant tests No
    🔒 Security concerns No
    ⚡ Key issues to review Error Handling:
    Ensure that the error handling in entry.client.tsx is sufficient and appropriately manages different types of initialization errors.
    Dependency Management:
    Verify that all new dependencies added (i18next, react-i18next, etc.) are compatible with existing packages and are used efficiently throughout the application.
    Configuration:
    Review the configuration settings in app/i18n.ts to ensure they are optimal and secure, especially regarding language detection and fallbacks.

    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add validation for language settings to prevent runtime errors

    Ensure that the language setting is validated or safely handled if
    context.storefront.i18n.language is undefined or not among the supported languages.

    app/entry.server.tsx [18]

    -await initI18Next(i18n, context.storefront.i18n.language);
    +const language = supportedLanguages.includes(context.storefront.i18n.language) ? context.storefront.i18n.language : defaultLanguage;
    +await initI18Next(i18n, language);
     
    Suggestion importance[1-10]: 9

    Why: Adding validation for language settings prevents potential runtime errors, which is crucial for application stability.

    9
    Security
    Update the node-fetch version to improve security and compatibility

    Consider updating the node-fetch version to ^3.0.0 or higher to ensure compatibility with
    the latest security patches and features. The current version ^2.6.12 might not include
    recent security fixes.

    package-lock.json [13851]

    -"node-fetch": "^2.6.12"
    +"node-fetch": "^3.0.0"
     
    Suggestion importance[1-10]: 9

    Why: Updating node-fetch to a newer version addresses potential security vulnerabilities and ensures compatibility with the latest features, which is crucial for maintaining a secure and robust application.

    9
    Enhancement
    Improve error handling in i18n initialization to enhance user experience and reliability

    Consider handling potential errors when initializing i18n in a more robust way than just
    logging to the console. This could involve retrying the initialization or providing a
    fallback UI to inform the user of the issue.

    app/entry.client.tsx [22]

    -.catch((error: any) => console.error(error));
    +.catch((error: any) => {
    +  // Implement a more robust error handling strategy here
    +  console.error("Failed to initialize i18n:", error);
    +  alert("Failed to load internationalization settings, please try refreshing.");
    +});
     
    Suggestion importance[1-10]: 8

    Why: This suggestion improves user experience and reliability by providing a more robust error handling strategy, which is crucial for initialization processes.

    8
    Remove unnecessary cross-fetch dependency to reduce bloat and prevent conflicts

    Remove the redundant cross-fetch dependency from i18next-http-backend if it's not
    explicitly required, as it might lead to unnecessary bloat and potential version
    conflicts.

    package-lock.json [13843]

    -"cross-fetch": "4.0.0"
    +{}
     
    Suggestion importance[1-10]: 6

    Why: Removing unnecessary dependencies helps reduce bloat and potential version conflicts, improving the overall efficiency and maintainability of the project.

    6
    Maintainability
    Refactor initI18Next to separate configuration logic for server and browser to enhance code clarity

    Refactor the initI18Next function to separate concerns for browser and server environments
    into distinct functions. This will improve code readability and maintainability.

    app/i18n.ts [29-37]

     if (!isBrowser) {
    +  configureServerI18nOptions(options, lng);
    +} else {
    +  configureBrowserI18nOptions(options);
    +}
    +
    +function configureServerI18nOptions(options, lng) {
       options.lng = lng;
       options.defaultNS = 'namespace1';
    -} else {
    +}
    +
    +function configureBrowserI18nOptions(options) {
       options.backend = {
         loadPath: resolve('./app/assets/locales/{{lng}}/{{ns}}.json'),
       };
       i18next.use(HttpApi);
     }
     
    Suggestion importance[1-10]: 7

    Why: The refactor enhances code readability and maintainability by separating concerns, which is beneficial for long-term code management.

    7
    Standardize and update the @babel/runtime dependency across all modules

    Update the @babel/runtime dependency to the latest version (^7.24.0) across all modules to
    ensure consistency and access to the latest features and bug fixes.

    package-lock.json [13822]

    -"@babel/runtime": "^7.23.2"
    +"@babel/runtime": "^7.24.0"
     
    Suggestion importance[1-10]: 7

    Why: Updating @babel/runtime to the latest version ensures consistency and access to the latest features and bug fixes, improving maintainability. However, it is not as critical as addressing security vulnerabilities.

    7
    Best practice
    Improve error type specificity for better error handling and debugging

    Use a more specific type for error handling in the .catch block to aid in debugging and
    error tracking.

    app/root.tsx [22]

    -.catch((error: any) => console.error(error));
    +.catch((error: Error) => console.error("Initialization error:", error.message));
     
    Suggestion importance[1-10]: 6

    Why: Using a more specific error type improves debugging and error tracking, but it is a minor improvement compared to other suggestions.

    6
    Possible issue
    Align the i18next version in peer dependencies to prevent compatibility issues

    Ensure that all peer dependencies are correctly specified to prevent runtime issues. The
    react-i18next module specifies i18next >= 23.2.3, but the installed version is 23.11.5,
    which might not be fully compatible.

    package-lock.json [19628]

    -"i18next": ">= 23.2.3",
     "i18next": "^23.11.5"
     
    Suggestion importance[1-10]: 5

    Why: Aligning the i18next version in peer dependencies helps prevent potential compatibility issues, but the current version specified (^23.11.5) is likely to be compatible, making this a minor concern.

    5

    Copy link
    Contributor

    shopify bot commented Jun 22, 2024

    Oxygen deployed a preview of your translate branch. Details:

    Storefront Status Preview link Deployment details Last update (UTC)
    BySisters ✅ Successful (Logs) Preview deployment Inspect deployment June 22, 2024 6:34 PM

    Learn more about Hydrogen's GitHub integration.

     - Move i18n client and server initialization files to i18n folder
     - Implement i18n default configuration file
     - Add getInitialNamespaces and getRouteNamespaces utilities for namespace handling
     - Update import paths to reflect new i18n structure
     - Add new namespace 'component' to root.tsx and corresponding JSON files for 'da' and 'en' locales
     • Replace getRouteNamespaces with extractNamespaces for i18n namespace extraction
     • Remove unused getInitialNamespaces and getRouteNamespaces files
     • Update i18n.client.ts to use extractNamespaces from window object
     • Simplify CustomBackend init method in i18n.server.ts
     • Remove unused i18next related dependencies from package.json
    …ckages
    
     - Add TypeScript definition files for i18n custom types and resources
     - Implement usage of namespace in translation hooks within components
     - Replace various i18n backend packages with `i18next-resources-for-ts`
     - Add script to generate i18n TypeScript types from JSON files
    @jamalsoueidan jamalsoueidan changed the title add package and translation example add translation example Jun 22, 2024
    @jamalsoueidan jamalsoueidan merged commit 847d314 into main Jun 22, 2024
    4 checks passed
    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.

    1 participant