Skip to content

Commit

Permalink
Finished first version and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lednerb committed Aug 26, 2018
0 parents commit 80362c7
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
@@ -0,0 +1,13 @@
FROM python:3.7-alpine

LABEL MAINTAINER="Lednerb <code@lednerb.de>"

RUN pip install pipenv

COPY . /app
WORKDIR /app

RUN pipenv install --system --deploy --ignore-pipfile

WORKDIR /export
CMD /app/poeditor2hugo.py
9 changes: 9 additions & 0 deletions LICENSE.md
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2018 Sascha Brendel | @Lednerb <code@lednerb.de>

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.
13 changes: 13 additions & 0 deletions 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"
65 changes: 65 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions 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)

This comment has been minimized.

Copy link
@Realislam

Realislam Nov 22, 2018

202795

[![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/)
Binary file added demo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions 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()

0 comments on commit 80362c7

Please sign in to comment.