Search code examples
javascriptencodingmacrosmicrosoft-dynamicsdynamics-gp

Javascript text file encoding with Arabic and English for macro file


I am trying to export a text file using JavaScript to be used as macro for Microsoft Dynamics GP.

The problem is Arabic letters don't render correctly when I try to use the macro in GP.

For example "النسر" would render as "ط§ظ„ظ†ط³ط±"

My JavaScript code uses FileSaver.js

var file = new Blob([text], {type:"text/plain;charset=utf-8;"});
saveAs(file, filename);

One solution I found was that after downloading the file I save it with ANSI encoding. But if I changed my charset in the JavaScript Blob object to "ansi" or "Windows-1252" or "cp-1252" it doesn't work.

Note: I am using Chrome on Windows 7


Solution

  • From my experience I have found that Dynamics GP macros uses ANSI.

    You will first need to encode your text to ANSI that includes arabic (windows-1256/CP1256).

    I used this js library to encode the text: https://github.com/mathiasbynens/windows-1256

    var text= windows1256.encode(text);
    

    Then you will need to convert it to 8 bit unsigned integers and export it using a Blob.

    var uint8 = new Uint8Array(text.length);
    for (var i = 0; i < uint8.length; i++)
    {
      uint8[i] = text.charCodeAt(i);
    }
    var file = new Blob([uint8], {type: 'text/plain;charset=windows-1256'}); 
    

    I know this answer is too late but I hope it helps.