diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..77b4ed1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.7-alpine + +LABEL MAINTAINER="Lednerb " + +RUN pip install pipenv + +COPY . /app +WORKDIR /app + +RUN pipenv install --system --deploy --ignore-pipfile + +WORKDIR /export +CMD /app/poeditor2hugo.py diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..26c9b4e --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2018 Sascha Brendel | @Lednerb + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..d77d59a --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +requests = "*" +tqdm = "*" + +[dev-packages] + +[requires] +python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..eb38607 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,65 @@ +{ + "_meta": { + "hash": { + "sha256": "df6ebf39d66e672a9a537a104937610238d0d339288d1e44612ef72c6ebf4380" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.6" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "certifi": { + "hashes": [ + "sha256:4c1d68a1408dd090d2f3a869aa94c3947cc1d967821d1ed303208c9f41f0f2f4", + "sha256:b6e8b28b2b7e771a41ecdd12d4d43262ecab52adebbafa42c77d6b57fb6ad3a4" + ], + "version": "==2018.8.13" + }, + "chardet": { + "hashes": [ + "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", + "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" + ], + "version": "==3.0.4" + }, + "idna": { + "hashes": [ + "sha256:156a6814fb5ac1fc6850fb002e0852d56c0c8d2531923a51032d1b70760e186e", + "sha256:684a38a6f903c1d71d6d5fac066b58d7768af4de2b832e426ec79c30daa94a16" + ], + "version": "==2.7" + }, + "requests": { + "hashes": [ + "sha256:63b52e3c866428a224f97cab011de738c36aec0185aa91cfacd418b5d58911d1", + "sha256:ec22d826a36ed72a7358ff3fe56cbd4ba69dd7a6718ffd450ff0e9df7a47ce6a" + ], + "index": "pypi", + "version": "==2.19.1" + }, + "tqdm": { + "hashes": [ + "sha256:536e5a0205b6401d8aaf04b21469949c178dbe3642e3c76d3bb494a83cb22a10", + "sha256:60bbaa6700e87a250f6abcbbd7ddb33243ad592240ba46afce5305b15b406fad" + ], + "index": "pypi", + "version": "==4.24.0" + }, + "urllib3": { + "hashes": [ + "sha256:a68ac5e15e76e7e5dd2b8f94007233e01effe3e50e8daddf69acfd81cb686baf", + "sha256:b5725a0bd4ba422ab0e66e89e030c806576753ea3ee08554382c14e685d117b5" + ], + "version": "==1.23" + } + }, + "develop": {} +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..4284e6d --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# `$ poeditor2hugo` + +[![GitHub version](https://img.shields.io/github/release/Lednerb/poeditor2hugo/all.svg?style=flat-square)](https://github.com/Lednerb/poeditor2hugo/releases) +[![License](https://img.shields.io/github/license/Lednerb/poeditor2hugo.svg?style=flat-square)](https://github.com/Lednerb/poeditor2hugo/blob/master/LICENSE.md) +[![Docker Pulls](https://img.shields.io/docker/pulls/lednerb/poeditor2hugo.svg?style=flat-square)](https://hub.docker.com/r/lednerb/poeditor2hugo/) +[![GitHub stars](https://img.shields.io/github/stars/Lednerb/poeditor2hugo.svg?style=social&label=Stars)](https://github.com/Lednerb/poeditor2hugo) + +This tool is for all creators of multilingual Hugo themes. + +With `poeditor2hugo` you can easily export your [POEditor](https://poeditor.com) translations to your [Hugo](https://gohugo.io) theme. + + +## Quickstart with docker + +If you have Docker installed, you can simply `cd` into your theme's directory and run: +``` +docker run --rm -it -v $PWD:/export lednerb/poeditor2hugo +``` + +If your current user is not in the `docker` group, you maybe have to run this command with `sudo`. + + +__Fixing permissions:__ + +If you are on a Linux system, you might want to change the permissions for the language files. +Within your theme directory run the following command to change the permissions: + +``` +sudo chown -r $USER:$USER i18n/ +``` + +Alternatively, do it in one step or create a bash alias: + +`~/.bashrc` +``` +alias poeditor2hugo='sudo docker run --rm -it -v $PWD:/export lednerb/poeditor2hugo && sudo chown -R $USER:$USER i18n' +``` + +You can then run: +``` +cd ~/your/hugo/theme +poeditor2hugo +``` + +### DEMO +[![DEMO](./demo.gif)](https://github.com/Lednerb/poeditor2hugo/) diff --git a/demo.gif b/demo.gif new file mode 100644 index 0000000..e6053b7 Binary files /dev/null and b/demo.gif differ diff --git a/poeditor2hugo.py b/poeditor2hugo.py new file mode 100755 index 0000000..f95f63a --- /dev/null +++ b/poeditor2hugo.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +import sys, os, os.path, errno +import requests +from tqdm import tqdm + + + +# Greeting +os.system('cls' if os.name == 'nt' else 'clear') +print() +print(" (c) by @Lednerb ") +print(" _ _ _ ____ _ ") +print(" _ __ ___ ___ __| (_) |_ ___ _ _|___ \| |__ _ _ __ _ ___ ") +print("| '_ \ / _ \ / _ \/ _` | | __/ _ \| '__|__) | '_ \| | | |/ _` |/ _ \ ") +print("| |_) | (_) | __/ (_| | | || (_) | | / __/| | | | |_| | (_| | (_) |") +print("| .__/ \___/ \___|\__,_|_|\__\___/|_| |_____|_| |_|\__,_|\__, |\___/ ") +print("|_| |___/ ") +print() + + +# Settings +POEDITOR_API_KEY = input("Enter your API KEY: ") +POEDITOR_ID = input("Enter the project ID: ") +LANGUAGE_FILE_PATH = 'i18n/' + +# Margin after input +print() +print() + + +# Get languages +r = requests.post('https://api.poeditor.com/v2/languages/list', { + 'api_token': POEDITOR_API_KEY, + 'id': POEDITOR_ID +}).json() + +if r.get('response').get('code') != '200': + sys.exit("ERROR: Can't get list of languages") + +# Create i18n directory if it does not exist +if not os.path.exists(LANGUAGE_FILE_PATH): + os.makedirs(LANGUAGE_FILE_PATH, 0o755, True) + +# Export languages +for language in tqdm(r.get('result').get('languages'), "Processing languages from poeditor"): + if language.get('translations') > 0: + + e = requests.post('https://api.poeditor.com/v2/projects/export', { + 'api_token': POEDITOR_API_KEY, + 'id': POEDITOR_ID, + 'language': language.get('code'), + 'type': 'json' + }).json() + + if e.get('response').get('code') != '200': + sys.exit("ERROR: Can't export language:" + language.get('name')) + + # Download the language file + f = requests.get(e.get('result').get('url')) + + if f.status_code != 200: + sys.exit("ERROR: Can't download language file:" + language.get('name')) + + # Convert and write file + with open(LANGUAGE_FILE_PATH + language.get('code') + '.toml', 'w', 1, 'utf-8') as file: + for string in f.json(): + if string.get('definition') is not None: + file.write("[" + string.get('term') + "]\n") + if string.get('term_plural') is not '': + file.write('one = "' + string.get('definition') + '"\n') + file.write('other = "' + string.get('term_plural') + '"\n') + else: + file.write('other = "' + string.get('definition') + '"\n') + + file.write("\n") + +print() +print("All available translations downloaded successfully!") +print("Don't forget to adjust the file permissions.") +print()