View Single Post
Old 12th February 2019, 10:39   #1
Pawel
Moderator
 
Pawel's Avatar
 
Join Date: Aug 2004
Location: Poland
Posts: 522
Send a message via ICQ to Pawel
NSIS UTF-8 to ANSI conversion problem

I was so stupid and decided to add support for Chinese language in my project
I thought it will be nice and easy - I was wrong! It seems that there are problems I can not resolve myself.

So, I will try do describe the problem and I hope some of you guys can help me.

My NSIS Installer (Unicode) uses UTF-8 (No BOM) encoded language files. The file contains all strings that I use to
display in installer pages and to generate files on user PC during installation (configuration files).
My NSIS installer with Chinese language with UTF-8 encoding works fine - everything is displayed correctly (my Chinese translator says it is OK).

But, the problem is with files that I generate on runtime during installation process on user PC (the files need to be generated dynamically as its content depends on user selection during installation).
So, I was trying to use what NSIS offer - as far as I know NSIS support "FileWrite" and "FileWriteUTF16LE" commands. Write below strings to file.

LangString UFM_COMPILE_DATE ${LANG_ID} "构建日期: " ; Built Date:
LangString UFM_INSTALL_DATE ${LANG_ID} "安装日期:" ; Installation date:
LangString UFM_INSTALL_PATH ${LANG_ID} "安装目录:" ; Installation Directory:

1. FileWrite creates ANSI file:

PHP Code:
    ClearErrors
    FileOpen $R0 
"$INSTDIR\FILE.MNU" w
    
${If} ${Errors}
        ; 
Error! Do something
    
${Else}
        
FileWrite $R0 '; Example TC Menu File$\r$\n'
        
FileWrite $R0 '; Chinese - Simplified (China) [zh-CN]$\r$\n'
        
FileWrite $R0 '$\r$\n'
        
FileWrite $R0 'POPUP "$(UFM_COMPILE_DATE)"$\r$\n'
        
FileWrite $R0 '  MENUITEM "$(UFM_INSTALL_DATE)", cm_SetAttrib$\r$\n'
        
FileWrite $R0 '  MENUITEM "$(UFM_INSTALL_PATH)", cm_VersionInfo$\r$\n'
        
FileWrite $R0 'END_POPUP$\r$\n'
        
FileClose $R0
    
${EndIf} 
But, above example generates file with wrong encoding - Chinese strings are all wrong. Encoding is bad.

2. FileWriteUTF16LE creates Unicode (UTF-16LE) file:

PHP Code:
    ClearErrors
    FileOpen $R0 
"$INSTDIR\FILE.MNU" w
    
${If} ${Errors}
        ; 
Error! Do something
    
${Else}
        
FileWriteUTF16LE $R0 '; Example TC Menu File$\r$\n'
        
FileWriteUTF16LE $R0 '; Chinese - Simplified (China) [zh-CN]$\r$\n'
        
FileWriteUTF16LE $R0 '$\r$\n'
        
FileWriteUTF16LE $R0 'POPUP "$(UFM_COMPILE_DATE)"$\r$\n'
        
FileWriteUTF16LE $R0 '  MENUITEM "$(UFM_INSTALL_DATE)", cm_SetAttrib$\r$\n'
        
FileWriteUTF16LE $R0 '  MENUITEM "$(UFM_INSTALL_PATH)", cm_VersionInfo$\r$\n'
        
FileWriteUTF16LE $R0 'END_POPUP$\r$\n'
        
FileClose $R0
    
${EndIf} 
Generated file looks nice (Chinese chars looks fine) - but my Chinese translator says it is displayed wrong (this might be problem of the app I use to display the file - Total Commander (it also do not accept BOM)).
But, regardless of whether it's a TC program fault or encoding conversion I can not use this.

So, Chinese translator said only ANSI GB2312 (Simplified) works fine.


Finally, the question is how to write (generate) file with ANSI GB2312 (Simplified) encoding using NSIS with UTF-8 (NO BOM) language file.

- Example NSIS Language file (with input strings): http://www.meggamusic.co.uk/shup/154...IMPCHINESE.nsh
- Example Output Chinese file: http://www.meggamusic.co.uk/shup/154...IMPCHINESE.MNU

Is this possible to create with NSIS a file encoded as ANSI GB2312 (Simplified) using UTF-8 (No BOM) encoded language file?
How can I do this?

Any help is much appreciated!
I am lost with this Chinese magic :P
-Pawel
Pawel is offline   Reply With Quote