65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Google 翻译为 Microsoft 多语言应用工具包定制 TAUS 数据提供程序

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2016年3月12日

CPOL

2分钟阅读

viewsIcon

15521

downloadIcon

67

自定义 TAUS 数据提供程序,通过 Google 翻译的 Web 界面支持更多语言

引言

Microsoft 提供了一个便捷的工具,即多语言应用工具包。虽然目前在处理数千个 string 时扩展性不太好,这源于 XLIFF 工具的一些问题,但它提供了一个不错的通用平台,用于获取初始的多语言翻译。然而,Google 翻译在某些方面提供了更好的支持,并且在 Microsoft 的提供商仅对一些较为晦涩、不常见或非英语欧洲语言提供部分支持的情况下,提供了完整的语言支持。其 Web 界面是免费的,并且可能受到速率限制,因为 Google 拥有一个专业的 API,更适合于严肃的使用场景。尽管如此,为其构建一个包装器仍然可能有用。

背景

具备 C#、.NET 和 Visual Studio 的知识,以及多语言应用工具包和 Resx 格式的知识将大有裨益。

Microsoft 提供了一个示例 TAUS 数据提供程序

CodeProject 上有一个现有的 Resx 文件翻译器,它为从源语言到目标语言的任意文本提供了一个简单且原始的 Google 翻译接口

多语言应用工具包 v4.0 可以在以下位置找到:

Using the Code

应编译提供的文件,它非常简单且原始地将参考的两个代码库粘合在一起,并将 TAUSDataProvider.dll 部署如下,按照包含的 readme.txt 中的前三个步骤进行操作。

1. Place the TAUSDATAProvider.dll in "%CommonProgramFiles(x86)%\Multilingual App Toolkit"
   (and any satellite DLLS in the related sub folder. E.g.: fr-FR\TAUSDataProvider.resources.dll).

2. Update "%ALLUSERSPROFILE%\Multilingual App Toolkit\TranslationManager.xml to enable the 
   TAUS provider by adding the following XML configuration (Requires admin rights).  
   Note: Providers are used in the order listed in the configuration file.  It is recommended to 
   place this sample provider at the top to ensure it is given priority during translation and 
   suggestions.

   ...
    <provider>
      <id>3AB3DC49-4A77-4371-AFBB-21876E25A40B</id>
      <name>TAUSDataProvider</name>
      <configfile>TAUSDataProvider\TAUSDataSettings.xml</configfile>
      <assemblypath>TAUSDataProvider.dll</assemblypath>
    </provider>
   ...
   
3. Copy the TAUSDataSetting.xml file to the 
   "%ALLUSERSPROFILE%\Multilingual App Toolkit\TAUSDataProvider" 
   folder.  This is the TAUS DATA Provider configuration setting used 
   to access the Windows Credential Manager store.
   
   Take special note as you will need to also copy a DLL before building 
   if a new Multilingual App Toolkit is released and you choose to use it: 
   The provider needs to be compiled against the same build 
   as the Microsoft.Multilingual.Translation.dll installed on your system.  
   If you try to translate a resource, the load error will be displayed
   in the Editor Message tab or in  Visual Studio's output panel. 
   The message should provide the details of the error.

您必须确保手动将使用国家/地区代码的任何源/目标语言添加到 TAUSDataAccess.cs 中的 GetLanguages 列表中,并将 Google 支持但未列出的语言添加到 Language.cs 中的 TranslatableCollection 中,因为自 Resx 翻译器工具创建以来,已经添加了更多语言。

关注点

更好的 HTML 解析可以动态地从 Google 翻译获取语言列表。此外,还提供了建议列表,一个与翻译内联显示,另一个列在下方,包含不同词性的变体,这将使翻译更加完整(HTMLAgilityPack 可能会成为一个不错的解析器)。

这也有助于证明,理论上可以使用不匹配其协议且不需要凭据的服务来模拟 TAUS 提供程序。

频繁访问 Google 的 Web 服务器可能会导致问题,因为他们有一个付费 API 服务,更适合在这里用于进行严肃的 resx 翻译项目,或者肯定可能会出现 403 类型的错误。可以修改代码以考虑到这些情况,并暂停一段时间…

历史

  • 初始版本
© . All rights reserved.