Bulk import for language translation- python script share

  • 31 August 2021
  • 1 reply
  • 4 views

Userlevel 2

Hello community,

I want to share this python script that will work to take all language translation files in a directory and bulk upload for one to many engagements for a given api key.  currently, the api key must be hard coded.  However, you can expand to include spinning through an api list if needed.  I currently do not need this functionality… yet… :)

# import the python libraries for use. 

import requests 

import os 

import sys 

  

#---------------------------------------------------------------------------------------------------------------------------------------- 

#  Program Operational Guidelines:  load all engagement translated files in bulk to many engagements for an api key. 

#    This program will look at the current directory where the python script is stored and upload all translated files for an engagement. 

#    It doesn't matter what status the engagement is in albeit completed or paused.  And it doesn't matter which translation state the document is in. 

#    This has been a change from earlier behavior where the status would indicate Completion when all localization languages were uploaded via the Bulk API. 

#    All xliffs for all engagements that need upload for translation purposes will be uploaded en masse. 

#    Check with Gainsight if any limitations of number of files. 

#    When all translated files are uploaded for a particular engagement, the status is automatically set to Translation Complete. 

#    If there is a file or two that did not get uploaded or not yet translated, then the Translation Status will remain Ready for Translation until all files are translated for the particular engagement. 

#    This is important to know since the Engagement is paused until ALL localization files are uploaded 

#    If the translation status is set to Translation Complete, and a file needs to be updated, the file can be updated even though the Status is set to Translation Complete. 

#---------------------------------------------------------------------------------------------------------------------------------------- 

  

# Hard coded constants.   Swap out the api key for use with another Gainsight product api key. 

apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 

 

# Housekeeping 

os.getcwd() 

print(os.getcwd()) 

  

# Endpoints 

uploadXliffEndpoint='https://api.aptrinsic.com/v1/localization/import' 

  

# Working directory path 

path='C:/Users/xxxxx/OneDrive - xxxxx/Documents/Tools/Python Scripts/UploadTranslatedFiles' 

  

#==================================================================================== 

# Function to post (upload) list of files. 

def createPosts(apiKey, inputFilename): 

    #print("hello function") 

    headers = {'X-APTRINSIC-API-KEY': apiKey}  

    files = {'file': open(inputFilename, 'rb')} 

    response = requests.post(uploadXliffEndpoint, headers=headers, files=files) 

    print(response)   

    print(inputFilename)  

    if (response.status_code != 201): 

        print(("Error %d : '%s' on %s" % (response.status_code, response.text, inputFilename)), file=sys.stderr) 

    sys.stderr.flush() 

  

#==================================================================================== 

# Upload (POST) the translated engagement files from vendor (this is a local file directory where these are saved) to the Gainsight engagement. 

    #print("hello post")     

for inputFilename in os.listdir(path): 

    #print(inputFilename)      

    if inputFilename.endswith('.xliff'): 

        with open(inputFilename,'rb') as f: 

            createPosts(apiKey,inputFilename) 

  

# End of program 

  


This topic has been closed for comments

1 reply

Userlevel 5

Hey @Janine Marlatt, thank you for your posting and sharing this with the Community. I am sure that someone out there will find this useful. Appreciate your contribution here :)