# All files / config rtl.js

0% Statements 0/28
0% Branches 0/23
0% Functions 0/5
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120

     /**
     * RTL (Right-to-Left) Configuration Module
     *
     * Centralizes RTL/LTR direction management for the system.
     * This module provides configuration and utilities for RTL support
     * without affecting existing LTR functionality.
     *
     * @module config/rtl
     */

    const logger = require('./logger');

    /**
     * RTL-enabled languages
     * Add language codes that require RTL layout
     */
    const RTL_LANGUAGES = [
      'ar', // Arabic
      'he', // Hebrew
      'fa', // Persian/Farsi
      'ur', // Urdu
      'yi', // Yiddish
    ];

    /**
     * Default configuration
     */
    const DEFAULT_CONFIG = {
      enabled: process.env.RTL_ENABLED === 'true' || false,
      defaultLanguage: process.env.DEFAULT_LANGUAGE || 'en',
      autoDetect: process.env.RTL_AUTO_DETECT === 'true' || true,
    };

    /**
     * Check if a language requires RTL layout
     * @param {string} languageCode - ISO 639-1 language code (e.g., 'ar', 'en')
     * @returns {boolean} True if language requires RTL
     */
    function isRTLLanguage(languageCode) {
      if (!languageCode || typeof languageCode !== 'string') {
        return false;
      }

      const code = languageCode.toLowerCase().split('-')[0]; // Handle 'ar-SA' -> 'ar'
      return RTL_LANGUAGES.includes(code);
    }

    /**
     * Get text direction for a language
     * @param {string} languageCode - ISO 639-1 language code
     * @returns {string} 'rtl' or 'ltr'
     */
    function getDirection(languageCode) {
      return isRTLLanguage(languageCode) ? 'rtl' : 'ltr';
    }

    /**
     * Get RTL configuration
     * @returns {object} RTL configuration object
     */
    function getConfig() {
      return { ...DEFAULT_CONFIG };
    }

    /**
     * Check if RTL is enabled globally
     * @returns {boolean}
     */
    function isRTLEnabled() {
      return DEFAULT_CONFIG.enabled;
    }

    /**
     * Middleware to detect and set text direction based on language
     * Adds `req.textDirection` and `req.isRTL` properties
     */
    function rtlMiddleware(req, res, next) {
      try {
        // Validate req and res objects
        if (!req || !res || typeof next !== 'function') {
          logger.error('Invalid middleware parameters');
          if (typeof next === 'function') next();
          return;
        }

        // Get language from various sources (priority order)
        const language =
          req.query?.lang ||
          req.body?.language ||
          req.headers?.['accept-language']?.split(',')[0]?.split('-')[0] ||
          DEFAULT_CONFIG.defaultLanguage;

        // Determine direction
        const direction = getDirection(language);
        const isRTL = direction === 'rtl';

        // Attach to request object
        req.language = language;
        req.textDirection = direction;
        req.isRTL = isRTL;

        next();
      } catch (error) {
        logger.error('RTL middleware error:', error);
        // Continue without RTL detection
        req.textDirection = 'ltr';
        req.isRTL = false;
        next();
      }
    }

    module.exports = {
      RTL_LANGUAGES,
      isRTLLanguage,
      getDirection,
      getConfig,
      isRTLEnabled,
      rtlMiddleware,
    };