Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not use the translation/file REST API #8417

Closed
2 tasks done
Saturnyn opened this issue Dec 2, 2022 · 8 comments
Closed
2 tasks done

Can not use the translation/file REST API #8417

Saturnyn opened this issue Dec 2, 2022 · 8 comments
Assignees
Labels
bug Something is broken.
Milestone

Comments

@Saturnyn
Copy link

Saturnyn commented Dec 2, 2022

Describe the issue

Hello,
I can not get this API to work: https://docs.weblate.org/en/latest/api.html#post--api-translations-(string-project)-(string-component)-(string-language)-file-

I get this message when I try to use the "conflicts" parameter:
{"conflicts":[""ignore" is not a valid choice."]}

I get this message when I try to use "method":"add":
{"method":"This method is not available here."}

It does work with "method":"replace" but I need the whole API to work.

Here is the node script I use to make the call:

		const form = new FormData();
			const fileStream = fse.createReadStream(tempFile);
			form.append('file', fileStream, fileName);
			form.append('method', 'add');
			form.append('conflicts', 'ignore');

			let url = WEBLATE_API_URL + 'translations/singularity/catalog/' + language + '/file/';
			return axios.post(url, form, {
				headers: Object.assign({}, form.getHeaders(), WEBLATE_AUTH_HEADER)
			})
			.then((res) => {
				console.log('=> SUCCESS', res.data);
			})
			.catch((e) => {
				return Promise.reject(formatError('$updloadStringsFile failed', e));
			});

The file I'm sending is a basic JSON such as {"my-key":"my-value"}

Did I miss something ?

I already tried

  • I've read and searched the documentation.
  • I've searched for similar issues in this repository.

Steps to reproduce the behavior

  1. Send POST /api/translations/(string:project)/(string:component)/(string:language)/file/
  2. See error:
    400 Bad Request
    {"method":"This method is not available here."}

Expected behavior

The request should not fail as I'm using the parameters described in the doc ?

Screenshots

No response

Exception traceback

No response

How do you run Weblate?

Docker container

Weblate versions

I don't have a direct access to the server, but this is what I can see in the web page:
Powered by Weblate 4.14.2

Weblate deploy checks

No response

Additional context

No response

@nijel
Copy link
Member

nijel commented Dec 2, 2022

The available choices depend on the scope and permissions. Are you uploading to the source or translation? Do you have full permissions or translate only?

@Saturnyn
Copy link
Author

Saturnyn commented Dec 5, 2022

Thanks for your comment.
I am trying to upload both to the source and translations.
I think I have full permissions since I was able to send values using POST and PATCH on /units.

I think the errors I had with the "add" method was because I was sending it to a translation language.
I modified the script to use "add" on the source language and "translate" on the others.
I still don't understand how the conflicts param should be used, what are the allowed values in each case ?


To give a little more context, my use case is the following:

  • I have an external source (google sheet document) that contains a catalog of items
  • each item has a bunch of text properties (title, description, etc..) that may have translations in different languages
  • translations are based on keys built like this {itemId}/{property} (ex: item1.title)

I need to migrate the text properties to weblate while keeping the rest of the catalog as it is.
So I want to make a script that I can run whenever changes are made to the catalog that keeps weblate in sync:

  • register the keys of the new items in weblate
  • send the translations if they exist in the catalog and not in weblate (=> never overwrite the translations that were set in weblate)

I have a version of the script that works like this:

  • downloads the catalog and makes a list of key/value for each language
  • downloads the weblate strings using GET /file/?format=json
  • builds a JSON for each language that contains all the keys that are present in the catalog and not in weblate
  • uploads the JSON files to weblate using POST /file ('add' for the source language, 'translate' for the other)

I'd like to make sure this script will never override strings that were translated in weblate and I thought the "conflicts" param would let me do that.
I also would like to be able to add new keys into weblate even if I do not have the string for the source language yet. It does not seem to work if I put an empty string in the JSON.

@nijel
Copy link
Member

nijel commented Dec 5, 2022

{"conflicts":[""ignore" is not a valid choice."]}

Okay, this is a bug – the documentation states ignore is a valid choice, but the code uses a blank string in this case. I will fix this so that both choices actually work.

I also would like to be able to add new keys into weblate even if I do not have the string for the source language yet. It does not seem to work if I put an empty string in the JSON.

It probably won't work via the upload because it skips blank strings. I'm uncertain if it will work via https://docs.weblate.org/en/latest/api.html#post--api-translations-(string-project)-(string-component)-(string-language)-units-

@nijel nijel self-assigned this Dec 5, 2022
@nijel nijel added the bug Something is broken. label Dec 5, 2022
@nijel nijel added this to the 4.15 milestone Dec 5, 2022
@nijel nijel closed this as completed in e3fa15a Dec 5, 2022
nijel added a commit that referenced this issue Dec 5, 2022
This should give users better clue what might be wrong.

See #8417
@github-actions
Copy link

github-actions bot commented Dec 5, 2022

Thank you for your report; the issue you have reported has just been fixed.

  • In case you see a problem with the fix, please comment on this issue.
  • In case you see a similar problem, please open a separate issue.
  • If you are happy with the outcome, don’t hesitate to support Weblate by making a donation.

@Saturnyn
Copy link
Author

Saturnyn commented Dec 5, 2022

Thanks for investigating and fixing the issue so fast !

@Sil3ntStorm

This comment was marked as off-topic.

@nijel

This comment was marked as off-topic.

@Sil3ntStorm

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken.
Projects
None yet
Development

No branches or pull requests

3 participants