'How to load external JavaScript in WordPress only for specific folder/subdirectory?

I am trying to load an external JavaScript to execute only in a language version of a website. Example: websitename.com/es/ <-- for Spanish I don't want a custom external (src..) third-party script be loaded onto the whole website. I was trying to use conditional tags in WordPress via functions but there is no option to target all the subdirectory. Using PHP or jQuery. Any solution you can suggest will be appreciated.

EDIT: it's an external script loaded using src, I need it to be applied only to the Spanish version which is in folder '/es/'. I am using polylang.

Thank you in advance

Here is what I was trying to do. I don't know if using taxonomies can help.

function tc_hook_wajs() {   
   if (is_home ('')) { 
        ?>      
      <script type="text/javascript">  
         //js code goes here     
       </script>        <?php      }    } 

   add_action('wp_head', 'tc_hook_wajs');


Solution 1:[1]

You can use the wpml_current_language hook in functions.php to get the current language code and based on languages code you can load each language file separately. here is example :

function my_script() {
 $current_language_code = apply_filters( 'wpml_current_language', null );
 if($current_language_code == 'en') { // English script
    $load_file = 'myscript.js';
 }
 elseif($current_language_code == 'sp') { // Spainsh script
    $load_file = 'myscript-sp.js';
 }
 else { // Italian script
    $load_file = 'myscript-it.js';
 }
 
 wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/'.$load_file, array( 'jquery' ),'',true );
 }
 add_action( 'wp_enqueue_scripts', 'my_script' );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MD Code