|
||
To develop a custom machine translation service, you need to define a class that inherits from AbstractMachineTranslationService (found in the CMS.TranslationServices namespace).
The service class must override and implement the following abstract methods:
Method |
Description |
Translate |
The main translation method which calls the appropriate web service (Google, Bing etc.) or otherwise translates the specified text from the source language into a target language.
When a user submits a translation to the service, the system calls the Translate method for every translation unit (<trans-unit> element) in the XLIFF source data. The method's text parameter contains the source text of the unit. |
Detect |
You can use this method to call the logic of the translation service to automatically determine the language of the source text.
Kentico CMS does not use the Detect method by default, but you can implement it if you need language detection functionality in your custom API. |
Speak |
Converts text to a stream of sound using the given service's text-to-speech engine.
Kentico CMS does not use the Speak method by default, but you can implement it if you need text-to-speech functionality in your custom API. If your service does not support such an option, throw a not implemented exception. |
IsAvailable |
Checks whether the service is appropriately configured and ready to be used. For example, you can confirm that the target service is currently online, or load any credentials and API keys required by the service from the website settings and confirm their validity.
The system only offers the service when translating documents and resource strings if the IsAvailable method returns a true value. |
This example demonstrates how to write a class providing functionality for a machine translation service. The sample class does not use a real translation service, it only converts the source text to upper case. When creating your own classes, replace the code of the methods with your own translation logic or call the API of the appropriate web service.
1. Open your web project in Visual Studio and add a new class into the App_Code folder (or Old_App_Code if the project is installed as a web application). For example, name the class SampleMachineTS.cs.
2. Edit the class and change its code to the following:
[C#]
using System; |
The system calls the methods of the class as needed when the given translation service is used.
3. Follow the instructions in the Loading service classes from App_Code topic to ensure that the application can access the custom class.
Once you have implemented the class with the required functionality, you need to register the translation service as an object in Kentico CMS:
1. Go to Site Manager -> Development -> Translation services and click New translation service.
2. Enter the following values into the service's properties:
•Display name: Sample machine service
•Code name: Leave the (automatic) option. The system will generate the code name as Sample_machine_service (based on the display name).
•Service provider assembly name: App_Code
•Service provider class name: SampleMachineTS
•Is machine translation service: Yes (checked)
•Service is enabled: yes (checked)
Click Save.
The service is now ready to be used.
When editing resource strings for specific user interface cultures, users can translate the text from the default language by clicking on icons representing the available machine translation services. To set an icon for your custom service:
1. Open the \App_Themes\Default\Images\ folder in your web project.
2. Extract the CMSModules folder from the Images.zip archive and place it into the \App_Themes\Default\Images\ directory.
3. Add the icon image file into the CMSModules\CMS_TranslationServices folder. The name of the file must match the code name of the given translation service, so use Sample_machine_service.png for this example. The recommended size for the icon is 16 x 16 px.
The system automatically loads the service icon images from this folder.
When submitting documents for translation, the dialog offers the Sample machine service as one of the translation service options. Using this option creates the new language version of the document as a copy of the original content, with all characters converted to upper case.
Submitting a document for translation using the sample machine service
Users can also call the custom translation service when localizing resource strings.
"Translating" a resource string using the sample service