XML-translator
is a tiny Bash
hack to demonstrate how XMLStarlet
can be used with online translator (e.g. Google Translator) to easily translate arbitrary XML files.
- cURL: library and command-line tool for transferring data using various protocols ;
- XMLStarlet: command line XML toolkit ;
- Google Translator: Google's free online language translation service. The service can be disrupted or break compatibility without notice.
If you want to use this script you should be aware that's it's a hack using Google Translate, and shouldn't be considered as a real solution.
You should also be aware that Google provide a Translation API as a paid service.
If you want to get an idea of the cost of the translation, you can use the following command to count characters to translate.
First, define xpath
, i18nTag
and inputFile
to your need :
xpath="/xpath/to/parent"
i18nTag="tag_with_i18n"
inputFile=./input.xml
Then run this command to count:
xmlstarlet sel -t -m "$xpath" -v "$i18nTag" "$inputFile" | wc -c
Simply run (psst, read section [Be responsible](#Be responsible) below):
./xml-translator.bash en zh ./test.en.xml
Overwrite target file: ./test.en.zh.xml ? [Y/n]
Result XML –with translated string– is store in test.en.zh.xml
; target language is appended to input filename.
By default the script doesn't reach for Google Translator. So running it will result in the use of dummy text:
[1] Terms And Conditions -> dummy text [offline mode]
[2] Catalog Menu -> dummy text [offline mode]
[3] Store Catalog -> dummy text [offline mode]
[4] Mini Shopping Cart -> dummy text [offline mode]
You must take responsabilities for the use of this script. In order to do so, you need to set the hackGoogle
variable to true
in xml-translator.bash:
hackGoogle=true
Then running again will use translated text:
[1] Terms And Conditions -> 条款和条件
[2] Catalog Menu -> 目录菜单
[3] Store Catalog -> 商店目录
[4] Mini Shopping Cart -> 迷你购物车
If the script stop immediatly, check for XML namespace as they are problematic with current implementation.
A simple solution is to remove namespace, another one is to submit a pull request.
Enjoy! :)