What could be causing the issue with Hindi text not displaying properly on Safari?

I'm having trouble with displaying Hindi text on my website. I tried pasting the text in a new file and it worked, so why is it not loading correctly in this case? The Hindi script is within the second span with the class word w2.

"use strict";

let words = document.querySelectorAll(".word");
words.forEach((word) => {
  let letters = word.textContent.split("");
  word.textContent = "";
  letters.forEach((letter) => {
    let span = document.createElement("span")
    span.textContent = letter;
    span.className = "letter";
    word.append(span);
  });
});

let currentWordIndex = 0;
let maxWordIndex = words.length - 1;
words[currentWordIndex].style.opacity = "1";

let rotateText = () => {
  let currentWord = words[currentWordIndex];
  let nextWord =
    currentWordIndex === maxWordIndex ? words[0] : words[currentWordIndex + 1];
  //rotate out letters of current word
  Array.from(currentWord.children).forEach((letter, i) => {
    setTimeout(() => {
      letter.className = "letter out";
    }, i * 80)
  });

  //reveal and rotate in letters of next word
  nextWord.style.opacity = "1";
  Array.from(nextWord.children).forEach((letter, i) => {
    letter.className = "letter behind";
    setTimeout(() => {
      letter.className = "letter in";
    }, 340 + i * 80)
  });
  currentWordIndex =
    currentWordIndex === maxWordIndex ? 0 : currentWordIndex + 1;
};

rotateText();
setInterval(rotateText, 4000);
body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #222;
}

.rotating-text {
  font-family: monospace;
  font-weight: bold;
  font-size: 36px;
  color: #fff;
  transform: translateX(-80px);
}

.rotating-text p {
  display: inline-flex;
  margin: 0;
  vertical-align: top;
}

.rotating-text p .word {
  position: absolute;
  display: flex;
  opacity: 0;
}

.rotating-text p .word .letter {
  transform-origin: center center 25px;
}

.rotating-text p .word .letter.out {
  transform: rotateX(90deg);
  transition: 0.5s cubic-bezier(0.6, 0, 0.7, 0.2);
}

.rotating-text p .word .letter.in {
  transition: 0.8s ease;
}

.rotating-text p .word .letter.behind {
  transform: rotateX(-90deg);
}

.w-1 {
  color: #e74c3c;
}

.w-2 {
  color: #8e44ad;
}

.w-3 {
  color: #3498db;
}

.w-4 {
  color: #28cc71;
}

.w-5 {
  color: #f1c40f;
}
<div class="rotating-text">
  <p>Hello! My Name is </p>
  <p>
    <span class="word w-1">Abhay_Chandra</span>
    <span class="word w-2">अभय चंद्रा </span>
    <span class="word w-3">アバイ・チャンドラ</span>
    <span class="word w-4">Абхай Чандра</span>
    <span class="word w-5">아바이 찬드라</span>
  </p>
</div>

This is the output

However, when I type the Hindi script normally, like in a p tag, it appears correctly. What could be causing this issue?

Answer №1

In order to properly split these characters without breaking them apart, you should perform a grapheme split. This will ensure that the important code points forming the "characters" are not separated incorrectly. Instead of using .split("") which splits based on code units, leading to splitting code units that should stay together, you can use the JavaScript method called Intl.Segmenter for a graphene split. However, make sure to check browser compatibility for this new API.

const graphemeSplit = (str, locale) => {
  const segmenter = new Intl.Segmenter(locale, {granularity: 'grapheme'});
  const segitr = segmenter.segment(str);
  return Array.from(segitr, ({segment}) => segment);
}

If broader browser support is necessary, consider manually splitting the characters and storing them in an object like so:

const localeGraphemes = {
  "en":["A","b","h","a","y","_","C","h","a","n","d","r","a"],
  "hi-IN":["अ","भ","य"," ","चं","द्रा"," "],
  "ja-JP":["ア","バ","イ","・","チ","ャ","ン","ド","ラ"],
  "ru-RU":["А","б","х","а","й"," ","Ч","а","н","д","р","а"],
  "ko-KR":["아","바","이"," ","찬","드","라"]
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to effectively compare time duration with a timer in the Laravel framework

I am managing a table that stores various job entries for employees. Each job entry includes a column for duration. I would like to trigger an alert or other event when the duration of a job has ended. My project is built using Laravel and VueJS. Below i ...

Could someone please explain how to obtain a compiled string within an AngularJS directive?

Check out the following code snippet for my custom directive: mymodule.directive("test", function($compile) { return { restrict: 'E', replace: true, template: '<div data-date="{{avail}}"></div>', ...

Instructions on keeping a numerical counter at its current value for all site visitors

Recently, I integrated a number counter into my website. However, I am facing an issue where the count resets to zero whenever a new visitor accesses the site. I'd like the count to remain persistent and update based on the previous count. For instanc ...

<a href> click here to append a new query parameter to the existing ones

Is there a way to create a link that will add a query parameter to the current URL instead of replacing all existing ones? Here's what I'm looking for: If the current URL in the browser is example.com/index.html, clicking on it should lead to ...

What could be causing issues with angularjs ng-include not functioning properly in Chrome at times?

<html ng-app="myApp1"> <head> <title>NG-Include</title> <script src="angular.js"></script> <script src="filter.js"></script> </head> <body> <div ng-controller="myController"> ...

Unbinding or undoing an 'onclick' event when another 'onclick' event is triggered

I am facing an issue where clicking on elements with 'onclick' functions works as expected, but when I click on a different element with another 'onclick' function, the first one remains active. What I actually want is for the previous ...

Determining the character encoding of a JavaScript source code file

For my German users, I want to display a status message with umlauts (ä/ü/ö) directly in the source file instead of requiring an extra download for messages. However, I'm having trouble defining the encoding for a JS source file. Is there a way si ...

deleting a aircraft upon the addition of another in three.js

I am striving to implement a button that toggles between different terrain-generating functions and removes the previous terrain. The current issue I am facing is that the planes stack on top of each other. You can see an image of the problem here. There ...

The innerHTML feature in Javascript seems to be malfunctioning

Having a simple javascript issue here. I am attempting to retrieve a date from a textbox and display it in a label for another purpose. However, I encountered some problems along the way. I can successfully alert the date retrieved from the textbox, but wh ...

The passage of time becomes distorted after a few hours of using setInterval

I created a simple digital clock using JavaScript to show the time on a TV screen. However, after several hours of running, I noticed that the displayed time gets off by a few seconds (around 30 or more). Below is the code snippet I used: getTime() { ...

Unable to locate module '.next/server/font-manifest.json'

I'm encountering a frustrating issue while attempting to deploy my nextjs app with server rendering. The app was created using Azure Pipelines and then uploaded to a production server that runs on a Linux operating system. Below is the configuration ...

Having trouble extracting data from a particular cell within an HTML table using jQuery

I am struggling to extract row data from a table for utilization in a modal. Provided below is my code for Views. <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css"> <link href="@Url.Content(" ...

The specified property 'length' is not found on type OkPacket within the mysql2 module

In my code example below, I am simply showcasing a specific scenario: this.getCode = (code: string): Promise<codeObject | false> => { return new Promise((resolve, reject) => { pool.query('SELECT * FROM ?? WHERE code = ...

The challenge of overlapping elements in jQuery UI resizable

Encountering an issue with the resizable class in jQuery UI (http://jqueryui.com/resizable/). When resizing begins, all divs below move to overlap with the resizing div. Upon inspecting the js console in chrome, it appears that resizable applies inline sty ...

Sending an incorrect value to the data variable

Apologies for my limited proficiency in English, Hello, I am new to Vue and struggling with an issue that I can't seem to resolve. I am fetching art data from an API (a simple list of dictionaries), and then creating a multi-array structure (list of l ...

What is the method for aligning inline-block text to the right after a line break?

I am currently encountering a challenge in developing a directory tree using <ul> element, particularly when the text of an item exceeds the width of the container. As I am utilizing the antd React library for this project, my options are somewhat l ...

The descendant selector in CSS fails to apply changes to the element

I am struggling to modify the border of a specific descendant element and so far I have not been successful. Despite using the correct descendant selector in the HTML below, the style change is not taking effect. If anyone has any insights on what could be ...

Handling dynamic routes with React Router 4 and the 404 path

I have been working with the latest version of React Router (4) and have implemented a dynamic route configuration as described in the tutorial. The routes are functioning correctly, except for when I tried to add a 404 path following the tutorial's i ...

Issue: Bidirectional binding functionality is not functioning properly within the user interface modal

I'm currently working on displaying data in a modal, but I'm facing issues with getting two-way binding to work properly. In my HTML code: <div class="cta-actions"> <div class="action"><a class="btn btn-lg btn-max">Send a pa ...

Retrieving data from a specific field within a table cell

Why does the value of the select box not get passed in my form scope when it is within its own table cell, but works when the whole form is in a single table cell? Below are examples of working and non-working code: WORKING <tr> <td> ...