Skip to content

Conversation

@rupato-deriv
Copy link
Collaborator

🤖 Auto-generated PR

This PR was automatically created by ShiftAI CLI.


fix: localization

@rupato-deriv
Copy link
Collaborator Author

🤖 AI Code Analysis Results

📝 Manual Coding

⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ 0%

0 of 1,038 characters (0%) in newly added lines are AI-generated


📊 Analysis Summary

Metric Value
📁 Files Changed 1
New Lines Added 1,038 chars
🤖 AI Characters 0
📈 AI Percentage 0%

📋 Per-File Breakdown (New Lines Only)

File Lines Added AI Content Percentage
src/utils/market-category-translator.ts 1038 chars 0 chars ⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ 0%

📝 This PR contains traditional hand-written code.

📊 Raw Data (for dashboard)
{
  "timestamp": "2025-11-03T08:06:33.691Z",
  "repository": "deriv-com/derivatives-bot",
  "branch": "unknown",
  "author": "unknown",
  "summary": {
    "totalCharacters": 1038,
    "aiCharacters": 0,
    "percentage": 0
  },
  "files": [
    {
      "name": "src/utils/market-category-translator.ts",
      "totalCharacters": 1038,
      "aiCharacters": 0,
      "percentage": 0,
      "status": "modified",
      "hasAddedContent": true,
      "addedContent": "/**\n * Market categories that should NOT be translated and remain in English\n */\nconst KEEP_IN_ENGLISH_CATEGORIES = [\n    'Derived',\n    'Baskets',\n    'Synthetics',\n    'Forex',\n    // Derived subcategories\n    'Continuous Indices',\n    'Crash/Boom',\n    'Jump Indices',\n    'Daily Reset Indices',\n    'Step Indices',\n    // Forex subcategories\n    'Major Pairs',\n    'Minor Pairs',\n    'Exotic Pairs',\n    'Forex Basket',\n    // Additional derived categories\n    'Volatility Indices',\n    'Basket I..."
    }
  ]
}

🚀 Analysis powered by ShiftAI • Analyzes only PR changes, not entire files

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

@claude
Copy link

claude bot commented Nov 3, 2025

Claude finished @rupato-deriv's task —— View job


Code Review Complete ✅

Tasks

  • Read the modified file to understand the code
  • View git diff to see exact changes
  • Analyze changes for correctness and edge cases
  • Review code quality and TypeScript best practices
  • Check for performance implications
  • Review security considerations
  • Verify test coverage
  • Provide comprehensive review feedback

Summary

This PR refactors the localization logic for market categories by moving items that should not be translated (like "Derived", "Forex", trade types, etc.) from the translation dictionary into a separate KEEP_IN_ENGLISH_CATEGORIES constant. This is a good architectural improvement that makes the localization logic clearer and more maintainable.

Priority Items

🔴 HIGH PRIORITY

  1. Missing Test Coverage (src/utils/market-category-translator.ts:97-137)
    • This utility has no automated tests
    • The logic is critical for i18n and has multiple code paths
    • Recommendation: Add unit tests covering:
      • Trade type names that should remain in English
      • Financial instrument patterns (Volatility 100 Index, Crash 500 Index, etc.)
      • Categories that should remain in English
      • Categories that should be translated
      • Fallback behavior with capitalizeWords

🟡 MEDIUM PRIORITY

  1. Potential Regression: Changed Fallback Behavior (src/utils/market-category-translator.ts:136)

    // Before:
    return categoryTranslations[categoryName] || categoryName;
    
    // After:
    return categoryTranslations[categoryName] || capitalizeWords(categoryName);
    • Previously, unknown categories were returned as-is
    • Now they are capitalized via capitalizeWords
    • Risk: If the API returns categories with specific casing (e.g., "forex basket"), they will be transformed to "Forex Basket"
    • Recommendation: Verify this behavior change is intentional and won't break existing UI expectations
  2. Type Safety: any Type in translateTradingTimesData (src/utils/market-category-translator.ts:142)

    export const translateTradingTimesData = (tradingTimesData: any): any => {
    • Using any bypasses TypeScript's type checking
    • Recommendation: Define proper interfaces:
    interface Submarket {
        name: string;
        // other properties
    }
    
    interface Market {
        name: string;
        submarkets?: Submarket[];
        // other properties
    }
    
    interface TradingTimesData {
        markets?: Market[];
    }
    
    export const translateTradingTimesData = (
        tradingTimesData: TradingTimesData | undefined
    ): TradingTimesData | undefined => {
        // ...
    }

🟢 LOW PRIORITY

  1. Potential Performance Optimization (src/utils/market-category-translator.ts:100, 110)

    • The TRADE_TYPE_NAMES.includes() and KEEP_IN_ENGLISH_CATEGORIES.includes() perform linear searches
    • For better performance, consider using Set for O(1) lookups:
    const TRADE_TYPE_NAMES_SET = new Set(TRADE_TYPE_NAMES);
    const KEEP_IN_ENGLISH_CATEGORIES_SET = new Set(KEEP_IN_ENGLISH_CATEGORIES);
    • Note: This is minor unless translateMarketCategory is called in a tight loop
  2. Missing JSDoc for Return Type (src/utils/market-category-translator.ts:97)

    • The function has a description but could benefit from @param and @returns tags:
    /**
     * Maps API-returned market category names to their translated versions
     * Excludes trade type names which should remain in English
     * @param categoryName - The market category name to translate
     * @returns The translated category name or the original name if it should remain in English
     */

Detailed Analysis

What's Good

  1. Clear Separation of Concerns: Moving non-translatable items to a constant improves code clarity
  2. Well-Documented: Good use of comments explaining which categories belong to which group
  3. Consistent Capitalization: The capitalizeWords helper ensures consistent formatting
  4. Security: No security concerns (no user input is executed, no XSS vectors)

⚠️ What Needs Attention

  1. Test Coverage: Critical for i18n logic
  2. Type Safety: Replace any types with proper interfaces
  3. Verify Behavioral Change: Ensure the new fallback with capitalizeWords is intentional

🎯 Suggested Quick Fixes

1. Replace `any` with proper types
interface Submarket {
    name: string;
    [key: string]: any; // Allow additional properties
}

interface Market {
    name: string;
    submarkets?: Submarket[];
    [key: string]: any; // Allow additional properties
}

interface TradingTimesData {
    markets?: Market[];
    [key: string]: any; // Allow additional properties
}

export const translateTradingTimesData = (
    tradingTimesData: TradingTimesData | undefined
): TradingTimesData | undefined => {
    if (!tradingTimesData?.markets) {
        return tradingTimesData;
    }

    const translatedData = { ...tradingTimesData };

    translatedData.markets = tradingTimesData.markets.map((market: Market) => ({
        ...market,
        name: translateMarketCategory(market.name),
        submarkets:
            market.submarkets?.map((submarket: Submarket) => ({
                ...submarket,
                name: translateMarketCategory(submarket.name),
            })) || [],
    }));

    return translatedData;
};
2. Optimize with Sets (optional)
const TRADE_TYPE_NAMES_SET = new Set(TRADE_TYPE_NAMES);
const KEEP_IN_ENGLISH_CATEGORIES_SET = new Set(KEEP_IN_ENGLISH_CATEGORIES);

export const translateMarketCategory = (categoryName: string): string => {
    if (TRADE_TYPE_NAMES_SET.has(categoryName)) {
        return categoryName;
    }

    if (isFinancialInstrumentName(categoryName)) {
        return categoryName;
    }

    if (KEEP_IN_ENGLISH_CATEGORIES_SET.has(categoryName)) {
        return capitalizeWords(categoryName);
    }
    // ... rest of the function
};

Verdict

Overall: Good refactoring with minor issues 👍

The changes improve code organization and make the i18n logic more maintainable. The main concerns are:

  • Missing test coverage (high priority)
  • Changed fallback behavior (verify intentional)
  • Type safety (medium priority)

Recommended Next Steps:

  1. Add unit tests for this utility
  2. Verify the capitalizeWords fallback behavior is correct
  3. Consider adding proper TypeScript types

@cloudflare-workers-and-pages
Copy link

Deploying derivatives-bot with  Cloudflare Pages  Cloudflare Pages

Latest commit: d23b479
Status: ✅  Deploy successful!
Preview URL: https://aefaf622.derivatives-bot.pages.dev
Branch Preview URL: https://rupato-fix--localization-mar.derivatives-bot.pages.dev

View logs

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