Importing JSON via Crowdin Github Action to String based project

I’m trying to migrate a project to string based.

My files are in the react-intl JSON format: React Intl file localization

The github action errors if I try to upload this JSON with the following error:

<Code: 422, Message: *.json files are not allowed to upload in strings-based projects>

I presume I need to convert the JSON to a different format first as the docs suggest that JSON is supported by string-based projects:

However I can’t see any references of what this JSON format should look like that includes both the strings to be translated and context.

Where can I find this information or is there a better way to upload this format into string based projects?

Hi @georeith !

Here it is the example of JSON file format which can be uploaded to the string-based project:

Thanks,

1 Like

Hi @Tania,

Thank you but there is no example of how to add context to strings on that page.

Whereas the CSV page for instance shows how to add context, labels, max length, etc

All the best,
George

1 Like

Dear @georeith !

As far as I understand you want to upload the JSON with already added context, right? Currently, for the mentioned JSON format which can be uploaded to the string-based project, context can be managed directly in Crowdin.

Thanks,

1 Like

@Tania yes the context is described in our codebase alongside the source strings, the JSON format page says it can take context but does not show an example. There is surely a way to upload it alongside the source strings.

1 Like

Hey @georeith ,

Sorry for the confusion, in order for the context to be imported into Crowdin you need to use JSON with context app and the file must have “crowdinContext”, there is an example in the documentation

Thanks @Roman thats what I was after, I updated the content to match that structure however I still can’t upload it to my string based project though either over the API or through the Crowdin UI as it tells me JSON files can’t be uploaded, but it states JSON can be uploaded here:

I’ve also tried with the CSV format which it also states as supported.

I’m not sure how I’m supposed to upload my sources in an automated workflow to a string based project.

Hello @georeith

For strings-based projects, you need to use this API call to upload sources:
https://support.crowdin.com/developer/api/v2/string-based/#tag/Source-Strings/operation/api.projects.strings.post

As it is strings-based, it works with strings, so the system will show you an error message regardless of project configuration or file configuration.

As an instant solution, I’d suggest you install this application (Translate JSON files with context - Crowdin Marketplace) and create (Creating a Project | Crowdin Docs) a file-based project.

Once it’s done, you’re welcome to follow this guide and do small adjustments to your Git Actions (GitHub - crowdin/github-action: A GitHub action to manage and synchronize localization resources with your Crowdin project)

I’ve just run a quick test, and with all these setup, JSON like this one works just like a charm:

{
    "campaign": {
        "text": "Email Campaign",
        "crowdinContext": "An email campaign in the context of marketing."
    },
    "lead": {
        "text": "Lead",
        "crowdinContext": "Marketing lead."
    }
}

Thanks @Dima I already have a file based project with a github action workflow set up but I cannot use it as file based does not work for merging branches and I need to be able to merge branches in git that have either approved or unapproved strings or a mixture of both.

In file based when you upload translations you must either upload all approved or all unapproved and you lose the status of those strings as they were on the branches which means it can’t be used in our automated workflow and requires someone to manually adjust the approval status of each uploaded string after.

Just to be clear is the documentation I linked to wrong? It definitely says you can upload JSON to string based projects.

And is the github action entirely uncapable of uploading sources to string based projects as the only option to upload sources there is via files specified in your crowdin.yml?

@georeith

GitHub Actions works well with Strings-based projects. By default, it would work with source and translation upload. Downloading the translations should be done via Bundles, and using the option “downalod_bundle”.

But, as you may see not all file formats are supported. For example, JSON like this one, would work without an issue:

{
    "Key1": "1111",
    "Key2": "2222",
    "Key3": "3333"
}

But JSON with context (like this one) would not work:

{
    "campaign": {
        "text": "Email Campaign",
        "crowdinContext": "An email campaign in the context of marketing."
    },
    "lead": {
        "text": "Lead",
        "crowdinContext": "Marketing lead."
    }
}

Because Strings-based projects were added not a long time ago, as for today, not all applications/features/integrations work with them.

We have already adapted the most popular and commonly used integrations to work well with strings-based projects, but unfortunately, this application (Translate JSON files with context - Crowdin Marketplace) was not in the list, so as for now it works with File-Based projects only.

We have an improvement created (to adapt the application for strings-based projects) but it gathers votes “+” to be taken into work (int. CN-52571).

In other words, as for right now options, you can either do refactoring of code and switch to basic JSON (to make it work with SB projects), or use FB projects (with GitActions or API).

Hope for your understanding.

Thanks @Dima appreciate the information.

I will need to use context because a lot of text is pretty domain specific and we use third party translation services so they are vital in giving them contextual information about what they are translating.

I will take a look at the Custom File Format plugin and see if I can make it export and re-import the translation/approval status of each string alongside each string in my file based project as that would solve my issue I believe.

If on merging to master I uploaded the translations with information about whether its approved or not and on the Crowdin side it sets that correctly, then I can delete the branch and it would be as if it were merged on the Crowdin side. Which looks like it may be possible with that plugin (I can see I can set the status on import but not whether I can access it on export).

1 Like

Great to hear that!

You can find more info about version management here → Version Management | Crowdin Docs

On the project level, it’s recommended to use the Duplicates option (Import Settings | Crowdin Docs)

So, basically, when you upload branch_2, if some strings are presented in branch_1 as well, they would become duplicates of master strings in branch_1, and when you delete branch_1, all translations (with authority) would migrate to new master strings (branch_2)

Thanks @Dima I have installed the CFF module and added my custom import / export code. But I can’t work out how to make Crowdin use this instead of the react-intl format its currently using.

Do you know how I make it use this when importing and exporting my files?

So I got this sort of working in a new project, I used the File type modifier integration and tell it to use the cff type for the files I want.

However thats only on import, and in my existing project even if I do that because the file name matches an existing file I think it ignores the file type modifier integration.

So I guess my only option is to delete all my current sources from the project and reimport them to get them recognised as my custom file format?

Hello @georeith

Yes, all import/export modules work like this, they apply changes for all files that are uploaded into the project after the processor is added

Hi @georeith, I’ve run into exactly the same issue you are dealing with. What is working for me so far is converting json files into csv files, then you can use “schema” property in the yaml file to correctly upload this csv into crowdin.
So steps are:

  1. formatjs extract --crowdin format
    You will get your files in this format:
{
  "someKey": {
    "description": "Some description",
    "message": "Some source string"
  },
  ...
}
  1. Write node script using “csv-stringify” to convert json into csv with this structure
identifier,source_or_translation,context
someKey,Some source string,Some description
  1. Update your crowdin.yml to include something like this:
files: [
  {
    "source": "extracted.csv",
    "first_line_contains_header": true,
    "scheme": "identifier,source_or_translation,context"
  }
]

Finally you may use crowdin-cli to upload these string into crowdin.
For downloading strings in a react-intl format you can then use JSON Exporter with object-shape configuration.

Thanks @Nikita appreciate the help, glad to know I’m not alone for wanting this either.

I got my usecase working now (in my file-based project instead) using the CFF integration I wrote some custom code to the export side to add status and then some code on the import side to re-import it and it is working well and means I don’t need to merge branches anymore I can just delete them after upload as all the approval/unapproval statuses will remain on master as they were on the branch.

I’ll keep your solution in mind though if I end up looking at string based projects again.

1 Like