How to Convert Bangla Unicode to Bijoy Encoding in JavaScript
In a local developer community I participate in, a member recently asked: “I am building a web-based reporting tool that needs to output text for a legacy system that only supports SutonnyMJ (Bijoy). How can I accurately convert Bangla Unicode text to Bijoy encoding using JavaScript?”
This is a notoriously difficult task because it isn’t just a simple character-for-character swap. It involves complex reordering logic and specific font-mapping rules.
The Short Answer (For Experienced Devs)
Section titled “The Short Answer (For Experienced Devs)”To convert Unicode to Bijoy, you must implement a transliteration engine. Since Unicode stores Bangla phonetically (Consonant + Vowel Sign) and Bijoy stores it visually (Vowel Sign + Consonant for certain characters), you must:
- Reorder specific vowel signs (like ‘i-kar’, ‘e-kar’) to appear before the consonant.
- Map the Unicode characters to their corresponding ASCII codes used by Bijoy-compatible fonts (e.g., SutonnyMJ).
- Handle Conjunctions (Juktakkhor) by mapping them to their specific Bijoy keyboard-stroke equivalents.
Deep Dive: The Conversion Logic
Section titled “Deep Dive: The Conversion Logic”Conversion fails most often because developers forget that in Bijoy, the visual order of characters differs from the logical Unicode order.
Solution 1: Manual Mapping with Reordering Logic
Section titled “Solution 1: Manual Mapping with Reordering Logic”This approach is best if you want a lightweight solution without external dependencies. This logic is compatible with Node.js 18+ and modern evergreen browsers.
Illustrative example — verify in your environment:
/** * Simple Unicode to Bijoy Logic * Version: ES6+ (Node 20 / Modern Browsers) */
const unicodeToBijoyMap = { 'অ': 'P', 'আ': 'Gv', 'ই': 'B', 'ঈ': 'C', // ... and so on 'ক': 'K', 'খ': 'L', 'গ': 'M', 'ি': 'f', 'ে': 'e', 'ৈ': 'p'};
function convertToBijoy(text) { let output = text;
// 1. Handle Reordering (e.g., 'ি' comes after consonant in Unicode, // but before in Bijoy/ASCII) // Regex logic to find [Consonant][i-kar] and swap to [i-kar][Consonant] output = output.replace(/([ক-হ])ি/g, 'f$1'); output = output.replace(/([ক-হ])ে/g, 'e$1'); output = output.replace(/([ক-হ])ৈ/g, 'p$1');
// 2. Character Replacement let finalString = ""; for (let char of output) { finalString += unicodeToBijoyMap[char] || char; }
return finalString;}
const input = "আমি"; // Unicode: আ + ম + িconsole.log(convertToBijoy(input)); // Result: Gvfm (Bijoy equivalent)Why this works:
The regex /([ক-হ])ি/g captures a consonant and the ‘i-kar’ sign. By replacing it with f$1, we move the ‘i-kar’ (represented by ‘f’ in Bijoy) to the front. This mimics the visual encoding required by legacy fonts like SutonnyMJ.
Solution 2: Using a Mature Mapping Library
Section titled “Solution 2: Using a Mature Mapping Library”For production apps, manual mapping is error-prone due to the hundreds of “Juktakkhor” (conjunctions). It is better to use a tested mapping table. Since there isn’t one single “standard” npm package for this, many developers port the open-source Bijoy-to-Unicode logic found in projects like Avro or the “Bijoy Converter” web tools.
Illustrative example — using a comprehensive map object:
// Example using a structured mapping approach// Version: Node 18+ / React 18import { bijoyMap } from './constants/bijoyMap';
function heavyConversion(unicodeText) { let result = unicodeText;
// Pre-processing for complex clusters // Example: Replace 'র্ক' (Reph) which has unique Bijoy rules result = result.replace(/([ক-হ])র্/g, '´$1');
// Use a loop or a bulk-replace library like 'string-replace-all' Object.keys(bijoyMap).forEach(key => { const regex = new RegExp(key, 'g'); result = result.replace(regex, bijoyMap[key]); });
return result;}Why this works: Using a pre-defined map ensures that complex characters (like ক্ষ or জ্ঞ) which require specific ASCII codes in SutonnyMJ are handled correctly. This is far more reliable than writing regex for every possible Bengali conjunction.
Critical Follow-up Questions
Section titled “Critical Follow-up Questions”1. Why does my text look like gibberish after conversion? The output of a Unicode-to-Bijoy converter is ASCII text. It will only look like Bangla if you apply a Bijoy-compliant font (like SutonnyMJ) to the container. If you view the output in Arial or Times New Roman, it will look like “Gvfm” instead of “আমি”.
2. Is this conversion lossless? Mostly, but be careful with “Zw-kar” and “Reph” combinations. Unicode handles these as distinct combining marks, while Bijoy often has specific single characters for these combinations. Always perform a round-trip test (Unicode -> Bijoy -> Unicode) to verify accuracy.
3. What about performance for large documents?
JavaScript’s String.replace() is fast, but for 100+ page documents, you should use a Map object or a single-pass character scanner rather than multiple regex passes to avoid O(n * m) complexity where m is the number of replacement rules.
Prevention and Accuracy Checklist
Section titled “Prevention and Accuracy Checklist”- Normalize Unicode: Always run
text.normalize('NFC')on your input to ensure consistent character representation before starting conversion. - Handle ‘Hasanta’: Ensure your logic handles the Hasanta (্) character, as it is used to form conjunctions in Unicode but is often omitted or replaced by a specific glyph in Bijoy.
- Order of Operations: Always process long-string conjunctions (e.g., ‘ক্ষ’) before individual characters (‘ক’ and ‘ষ’) to prevent partial replacements.
- Font Fallback: When displaying Bijoy text on the web, ensure you have
@font-faceset up for SutonnyMJ, otherwise users will see the raw ASCII characters. - Input Validation: Ensure the input is actually Bangla Unicode. Running conversion logic on English text will result in corrupted ASCII.