Closed FranklinYu closed 4 years ago
Hi @FranklinYu ,
How about using something this:
import requests
import io
import json
AUTH = ('username', 'password')
URL = 'https://www.transifex.com/api/2/project/project_slug/resource/resource_slug/translation/lang_code/'
content = io.open('translation_filename.extension', 'r', encoding='utf-8').read()
data = {
'content': content
}
headers = {'content-type': 'application/json'}
response = requests.put(URL, data = json.dumps(data), headers=headers, auth=AUTH)
print response;
@nelefth It’s much easier to expose Python API of existing working library than writing a new one from scratch.
@FranklinYu There are 2 ways you can do it with Python.
1. Looking under the hood of the CLI code and leveraging its power
After installing transifex-client and being in the repo directory where you have your .tx
folder with your config
file, you can do:
In [1]: from txclib.project import Project
In [2]: tx = Project()
The push
and pull
methods will be available to you.
In [3]: tx.push?
Signature: tx.push(source=False, translations=False, force=False, resources=None, languages=None, skip=False, no_interactive=False, xliff=False, branch=None, parallel=False)
Docstring: Push all the resources
File: ~/.virtualenvs/cli-13/lib/python3.8/site-packages/txclib/project.py
Type: instancemethod
In [4]: tx.pull?
Signature: tx.pull(languages=None, resources=None, overwrite=True, fetchall=False, fetchsource=False, force=False, skip=False, minimum_perc=0, mode=None, pseudo=False, xliff=False, branch=None, parallel=False, no_interactive=False)
Docstring: Pull all translations file from Transifex server.
File: ~/.virtualenvs/cli-13/lib/python3.8/site-packages/txclib/project.py
Type: instancemethod
As it's underline code, it has the danger of breaking in the future though.
2. Using the Python library we have
This library will require more code than you described, as it doesn't work based on a config file like the CLI.
https://github.com/transifex/transifex-python-library
I hope it helps.
The library is awesome! I should have found it earlier. Or, has it ever been mentioned on https://docs.transifex.com?
In addition, does this depend on the Transifex CLI in any way?
It's totally independent of the CLI.
We don't promote it at the moment in the docs.
Currently we seems to only provide the CLI tool
tx
. Would be useful if Python API is provided. Example: