Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ncornette/python-mobile-text-tool

Tools to manage translations on mobile applications
https://github.com/ncornette/python-mobile-text-tool

android python translation

Last synced: 2 months ago
JSON representation

Tools to manage translations on mobile applications

Awesome Lists containing this project

README

        

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Mobile Text Tools\n",
"\n",
"Mobile Text Tools is a set of tools to edit translations for `Android` and `IOS` mobile applications. It provides a data model represented in `json` and can read and write different formats."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Features\n",
" - import .csv, .xls, .xlsx, .json\n",
" - export .csv, .json, android resources, ios resoures\n",
" - custom rows format import and export\n",
" - string escape\n",
" - token replacement: you must use the `{}` or `{1}..{n}` notation in source files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CLI\n",
"\n",
"To generate translations for android and ios from `.xlsx` file : "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"# python update_wordings.py my_wordings.xlsx -i out/ios -a out/android\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To generate translations from json file for android with custom resource filename : "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"# python update_wordings.py my_wordings.json -a out/android --android-resname my_strings.xml\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python interface"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import mobileStrings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Read :\n",
"\n",
"Function `read_file(f)` from module `input` supports `.xls`, `.xlsx`, `.csv`, `.json` file formats."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"en, fr, de, pt, it, es, nl, zh, ja, pl, pt_BR, ru, id, ko, ar, tr, th, sv\n"
]
}
],
"source": [
"languages, wordings = mobileStrings.input.read_file('./test_translations.json')\n",
"print ', '.join(languages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Read custom formats :\n",
"\n",
"For formats represented as rows like `.csv` and `.xls`, you can specify columns numbers with `FormatSpec`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"en, fr, de, pt, it, es, nl, zh, ja, pl, pt_BR, ru, id, ko, ar, tr, th, sv\n"
]
}
],
"source": [
"specs = mobileStrings.input.FormatSpec(0, 1, 2, 3, 4, bool, bool, {}) # Default format_spec (all params are optional)\n",
"\n",
"languages, wordings = mobileStrings.input.read_file('./test_translations.csv', specs)\n",
"print ', '.join(languages)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Query :\n",
"\n",
"To search a wording by its key, you can convert the `list` of `Wording` to a `dict`"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# keys: \n",
"menu.home\n",
"menu.contact\n",
"menu.share\n",
"menu.welcome\n",
"menu.news\n",
"menu.share.not.exported\n",
"menu.infos\n",
"comment.generated\n",
"comment.section\n",
"\n",
"# Title on menu header:\n",
"Bienvenue !\n",
"Willkommen!\n"
]
}
],
"source": [
"# wordings is a list, create a dict to query\n",
"d = dict((w.key,w) for w in wordings)\n",
"print('# keys: ')\n",
"print '\\n'.join(d.keys())\n",
"\n",
"welcome_wording = d.get('menu.welcome')\n",
"\n",
"print('\\n# ' +welcome_wording.comment+':')\n",
"print welcome_wording.translations.get('fr')\n",
"print welcome_wording.translations.get('de')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Write : \n",
"\n",
"Writing is very simple : use a `write_` function from the `output` module. It supports `Android`, `IOS`, `json`, `csv` output formats.\n",
"\n",
"For mobile applications : \n",
"``` python\n",
"mobileStrings.output.write_android_strings(languages, wordings, '~/dev/myAndProject/res')\n",
"mobileStrings.output.write_ios_strings(languages, wordings, '~/dev/myIOSProject/res')\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Csv example :"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"key,exportable,is_comment,comment,en,fr,de\r\n",
"comment.generated,Yes,Yes,Generated by mobile dev tools - Do not modify,,,\r\n",
"menu.welcome,Yes,,Title on menu header,Welcome!,Bienvenue !,Willkommen!\r\n",
"menu.home,Yes,,Home item,Home,Accueil,Start\r\n",
"menu.news,Yes,,News item,News,Actualités,News\r\n",
"comment.section,Yes,Yes,This is a section,,,\r\n",
"menu.contact,Yes,,Contact item,Contact,Contact,Kontakt\r\n",
"menu.infos,Yes,,Information pages item,Info,Infos,Informationen\r\n",
"menu.share.not.exported,,,Share application item - not exported,Share,Partager,Teilen\r\n",
"menu.share,Yes,,Share application item,Share,Partager,Teilen\r\n",
"menu.share,Yes,,Share application item,,Partager,Teilen\r\n",
"\n"
]
}
],
"source": [
"import cStringIO\n",
"sf = cStringIO.StringIO()\n",
"\n",
"# Write csv in a file-like object, for the first 3 languages only\n",
"mobileStrings.output.write_csv(languages[:3], wordings, sf)\n",
"print sf.getvalue()\n",
"sf.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}