3liz / lizmap-javascript-scripts

Scripts for Lizmap Web Client
Mozilla Public License 2.0
18 stars 40 forks source link

Translation of QGIS project content in Lizmap Web Client #20

Closed RobiFag closed 4 years ago

RobiFag commented 4 years ago

Translation In this folder there are:

  1. a python script to retrieve translatable strings from a QGIS project and its related .cfg file created with the lizmap plugin
  2. a javascript file which translates the project content in Lizmap-Web-Client according to the browser language

The python script (get_translatable_string.py) This python script retrieves all possible translatable strings (layers name, groups name, layouts name, fields or aliases name, etc.) from the QGIS project and the related .cfg file. It saves a json file in which the key is the retrieved string and the value should contain the translation of the string. The json file is automatically saved in the media folder. Following an example of the json structure:

{"Original layer name": "", "Original group name": "", ..}

The translation has to be provided manually by editing the file and putting the translated string in the double-quotes. Following an example of the translated json:

{"Original layer name": "Translated layer name", "Original group name": "Translated group name", ..}

The js script (translation.js) This javascript script translates layers and groups names, the title of the project, the print layouts names and the aliases or fileds names according to the language of the browser and using the json file created with the python script get_translatable_string.py If a translated string is provided in the json file the original string is translated, otherwise the original string is shown in lizmap web client. The json file must be saved in the media folder which needs to be available from the web. Therefore it is necessary to create a symbolic link on the apache directory (e.g. /var/www/html/) to the media folder in the user repository. The javascript retrieves the original string (key) and the translated string (value) from the json file. Then for each key found in the jason check the text of the provided html selector (e.g. $("div#header div#title h1")). If the two strings (the one from the json file e the one found in the html page) match the text of the provided html selector is replaced with the translated string, if provided in the json file.

rldhont commented 4 years ago

@mdouchin @nboisteault can you review the code ?

rldhont commented 4 years ago

@RobiFag can you add some descritpion to your PR ?

RobiFag commented 4 years ago

@rldhont description added!

Gustry commented 4 years ago

Nice idea.

For curiosity, did you have a look to the translation of a QGIS project ? https://changelog.qgis.org/en/qgis/version/3.4-LTR/#translation-of-qgis-projects Would it be hard to export from TS files to JSON?

RobiFag commented 4 years ago

Nice idea.

For curiosity, did you have a look to the translation of a QGIS project ? https://changelog.qgis.org/en/qgis/version/3.4-LTR/#translation-of-qgis-projects Would it be hard to export from TS files to JSON?

Yes sure, I have also work on a script to update the .ts file created by QGIS (see https://github.com/qgis/QGIS-Enhancement-Proposals/issues/90 for more details). To my knowledge, it is surely possible to export an xml (like .ts) file to a json in python or javascript (using standard libraries). However, if I understand correctly, it is necessary to check if all the information needed for translation in lizmap web client are provided and it is also necessary to filter out possible useless information.

RobiFag commented 4 years ago

I have already updated the python script. Now if the .json file already exists it will be updated only with the new strings.

RobiFag commented 4 years ago

Hi @rldhont @nboisteault @mdouchin ! We resolved all your comments and added the update of the json file in order to make the python script more usable.

Is it possible to accept and close the pull request?

RobiFag commented 4 years ago

Thanks for your work. Just some suggestions :

Thanks @Gustry for your suggestions..I hope I have correctly understood and hence resolved them in the proper way.

Gustry commented 4 years ago
RobiFag commented 4 years ago

Thanks, it looks fine for me. I can't comment on the Javascript side, I will let my colleagues on that part.

Side question, which LWC version are you using for this?

We have tested the translation scripts with the versions 3.3.4 and 3.3.5

RobiFag commented 4 years ago
  • [ ] Use the squash button

What do you mean for 'squash button'?

  • [ ] Create a ticket on Lizmap documentation ?

Sure, but maybe it is better to create an ad hoc section..what do you think?

Gustry commented 4 years ago

What do you mean for 'squash button'?

Don't worry, it's a reminder for us. You made a PR with 18 commits, we will squash it on github.

Sure, but maybe it is better to create an ad hoc section..what do you think?

Well, until this is not in core, this help can stay here I think.

I just tried your script. The resulting file contains a single line with all translations on it. This is not recommanded because:

In the python json module, there is a function to format a JSON string. It will add indentation etc. Can you use it?

Sorry, I haven't tried the Javascript part. So only file translated file is possible? (single translation.json with a single language)

I tried on different Lizmap projects and I always have a string with \n and a lot of spaces, the last one. Is-it normal?

{"\n          Cats movements\n        ": ""}
RobiFag commented 4 years ago

What do you mean for 'squash button'?

Don't worry, it's a reminder for us. You made a PR with 18 commits, we will squash it on github.

Sure, but maybe it is better to create an ad hoc section..what do you think?

Well, until this is not in core, this help can stay here I think.

I just tried your script. The resulting file contains a single line with all translations on it. This is not recommanded because:

  • difficult to read
  • If you use a VCS system such as GIT, it makes it impossible to track translations.

In the python json module, there is a function to format a JSON string. It will add indentation etc. Can you use it?

I added an indent=4 in the dump function parameters. now the json file showld be better formatted.

Sorry, I haven't tried the Javascript part. So only file translated file is possible? (single translation.json with a single language)

I'm not a javascript expert and right now the only possible way I can suggest at the moment would be duplicating the code. Obviously I'm sure there are better solutions and we are very glad to discuss them with you whenever possible.

I tried on different Lizmap projects and I always have a string with \n and a lot of spaces, the last one. Is-it normal?

{"\n          Cats movements\n        ": ""}

Yes it is normal! This is the line to translate the title. If you try to inspect the title on your lizmap project, you'll see that the value of the div#title h1 has \n and spaces. In order to translate the title, it is necessary to pass the whole string including \n and spaces.

RobiFag commented 4 years ago

Sorry, I haven't tried the Javascript part. So only file translated file is possible? (single translation.json with a single language)

I all, I have just uploaded a new version of both python and javascript files. Now the python file creates a json for each language specified in a list (e.g translation_en.json, translation_fr.json, etc.). On the javascript side, the file translation.js loads the corresponding json file depending on the browser language.

rmarzocchi84 commented 4 years ago

Let us know if it is ok for you. I think it is a good work which in future can be also integrated in the lizmap-plugin.