0

TranslationService sample Java SDK code

Hello,

I'm interested in understanding how the TranslationService can be used but I need some sample code. I checked github but did not find any new samples.

Will you be able to provide some sample code that uses the TranslationService component?

Thank you,

Johnny

2 comments

  • Avatar
    John Tanner Official comment

    Hi Johnny,

    Since we don't have a sample posted on github, here is a sample Java SDK class showing an example of how to call the translation service.

    Regards

    John

     

    package com.veeva.vault.custom.triggers;

    import com.veeva.vault.sdk.api.core.ServiceLocator;
    import com.veeva.vault.sdk.api.core.ValueType;
    import com.veeva.vault.sdk.api.data.*;
    import com.veeva.vault.sdk.api.i18n.TranslationService;
    import com.veeva.vault.sdk.api.i18n.TranslationsReadRequest;

    import java.util.Map;

    @RecordTriggerInfo(object = "manufacturer__c", events = {RecordEvent.BEFORE_INSERT, RecordEvent.BEFORE_UPDATE})
    public class BikeManufacturerValidator implements RecordTrigger {

    // This sample displays a translated pop up error message if a manufacturer object record
    // is either saved or updated where the metadata field 'name__v' = 'Canyon'.
    // It assumes the following configuration has first been set up:
    //
    // 1) A custom validation message has already been created through the Vault UI Admin Screen at
    // Admin > Configuration > Message Catalog (VAULT JAVA SDK).
    // The Message Group has the details:
    // - Label: Validation Message
    // - Name: validation_messages__c
    // - Source: Custom
    // It will then have a child message:
    // - Name: direct_to_consumer__c
    // - Source: Custom
    // - Default Value: Cannot stock this manufacturer, they sell direct-to-consumer.
    // - Translation: Cannot stock this manufacturer, they sell direct-to-consumer.
    //
    // (This is documented at http://vaulthelp2.vod309.com/wordpress/admin-user-help/viewing-vault-java-sdk-solutions/#message_catalog)
    //
    // 2) The Translation messages have been added in the desired language, by exporting, updating and importing the
    // bulk translation messages.
    //
    // (This is documented at http://vaulthelp2.vod309.com/wordpress/admin-user-help/using-the-message-catalog/)


    public void execute(RecordTriggerContext recordTriggerContext) {

    for (RecordChange inputRecord : recordTriggerContext.getRecordChanges()) {
    String name = inputRecord.getNew().getValue("name__v", ValueType.STRING);
    if (name.equalsIgnoreCase("Canyon")) {

    // With internationalized code

    // Locate TranslationService
    TranslationService translationService = ServiceLocator.locate(TranslationService.class);

    // Build the request
    TranslationsReadRequest readRequest = translationService.newTranslationsReadRequestBuilder()
    .withMessageGroup("validation_messages__c")
    .build();

    // Read all messages in the group
    Map<String, String> allValidationMessages = translationService.readTranslations(readRequest)
    .getTranslations();

    // Get the translated message we want
    String errorTranslation = allValidationMessages.get("direct_to_consumer__c");

    // Display the translated error message to the user
    inputRecord.setError("BIKE_ERROR", errorTranslation);

    }
    }
    }
    }
  • Avatar
    John Choo

    Hi John

    Thank you, this is very helpful.

    Best Regards,

    Johnny

Please sign in to leave a comment.