9

Python学习,VNR调用Jbeijing翻译的代码

 3 years ago
source link: https://xiaix.me/pythonxue-xi-vnrdiao-yong-jbeijingfan-yi-de-dai-ma/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python学习,VNR调用Jbeijing翻译的代码

发表于 2018-12-24   |   标签 Python   |  

由于excite关闭了网页翻译功能,google的网页翻译有时候也不太灵光,就去看了下jbeijing的翻译API调用,正好VNR中已有相关代码,就学习一下。

def transferUnicode(self, text, to, fr=CODEPAGE_JA):  
    """
    @param  text  unicode  not str
    @param  to  uint  code page
    @param  fr  uint  code page
    @raise  WindowsError, AttributeError
    From IDA:
    int __cdecl JC_Transfer_Unicode(int, UINT CodePage, int, int, int, LPCWSTR lpWideCharStr, int, int, int, int)
    Values are get by runtime debugging.

    Guessed
    int __cdecl JC_Transfer_Unicode(
      HWND hwnd,
      UINT fromCodePage,
      UINT toCodePage,
      int unknown,
      int unknown,
      LPCWSTR from,
      LPWSTR to,
      int &toCapacity,
      LPWSTR buffer,
      int &bufferCapacity)

    Note: This function is not thread-safe!
    Using persistent buffers is faster, but not thread-safe!
    """
    size = BUFFER_SIZE
    if self.BUFFER_THREAD_SAFE:
      # Limit buffer size would result in crash ... no idea why
      #size = min(size, len(text) * 10) # the translation should be no more larger than 10 times of Japanese
      out = ctypes.create_unicode_buffer(size)
      buf = ctypes.create_unicode_buffer(size)
    else:
      out = self.buffer1
      buf = self.buffer2
    outsz = ctypes.c_int(size)
    bufsz = ctypes.c_int(size)
    #self.dll.JC_Transfer_Unicode(
    self.JC_Transfer_Unicode(
      0,    # int, unknown
      fr,   # uint from, supposed to be 0x3a4 (cp932)
      to,   # uint to, eighter cp950 or cp932
      1,    # int, unknown
      1,    # int, unknown
      text,
      out,  # wchar_t*
      ctypes.byref(outsz), # &int
      buf,  # wchar_t*
      ctypes.byref(bufsz)) # &int
    return out.value

这段代码是调用JBJCT.dll中的方法(看注释大神是用IDA反编译dll在看函数的参数),功能是将unicode文本从日文翻译成中文,下面是运行效果

实际运用中发现一个问题,翻译的文本是有上限的,如果原文是太长的文章,返回的译文可能会缺失(大概5k以上不行)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK