Skip to main content

📦 potojson

PyPI Python versions License

Pofile to JSON conversion without pain.

Status

Tests coverage-image

Installation

pip install potojson

Documentation

API

# pofile_to_json(content, fallback_to_msgid=False, fuzzy=False, pretty=False, indent=2, language=None, plural_forms=None, as_dict=False) ⇒ str

Converts a pofile passed as string or filepath and returns a JSON formatted output. Given this pofile:

#

#, fuzzy
msgid ""
msgstr ""
"Language: es\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"

msgid "msgid"
msgstr "msgstr"

msgctxt "msgctxt"
msgid "msgid"
msgstr "msgstr"

msgid "msgid"
msgid_plural "msgid_plural"
msgstr[0] "msgstr[0]"
msgstr[1] "msgstr[1]"

msgctxt "msgctxt"
msgid "msgid"
msgid_plural "msgid_plural"
msgstr[0] "msgstr[0]"
msgstr[1] "msgstr[1]"

... the output will be:

{
  "": {
    "language": "es",
    "plural-forms": "nplurals=2; plural=n != 1;",
  },
  "msgid": "msgstr",
  "msgctxt": {
    "msgid": "msgstr",
  },
  "msgid": ["msgstr[0]", "msgstr[1]"],
  "msgctxt": {
    "msgid": ["msgstr[0]", "msgstr[1]"],
  }
}

This output can be customized tuning the parameters of the function:

  • content (str) Content or filepath of the pofile to convert.
  • fallback_to_msgid (bool) Use msgid if translation is missing.
  • fuzzy (bool) Include fuzzy messages.
  • pretty (bool) Pretty-print JSON output.
  • sort_keys (bool) Sort dictionary by key. Combined with as_dict parameter, returns an instance of collections.OrderedDict.
  • indent (int) Number of spaces for indentation used pretty-printing JSON output. Only takes effect if pretty is enabled.
  • language (str) Language for the translations. Will be inserted in the empty key of the JSON output. If not provided and the passed pofile includes the "Language" header, will be extracted from it.
  • plural_forms (str) Plural forms for the language of the translations. Will be inserted in the empty key of the JSON output. If not provided and the passed pofile includes the "Plural-Forms" header, will be extracted from it.
  • as_dict (bool) Returns the output as a Python dictionary.
  • encoding (str) The encoding to use reading the pofile. By default will be auto-detected.

CLI

usage: potojson [-h] [-v] [-m] [-f] [-p] [-k] [-i N] [-l LANGUAGE] [-s PLURAL_FORMS] [-e ENCODING] [PO_FILEPATH_OR_CONTENT ...]

Pofile to JSON conversion without pain.

positional arguments:
  PO_FILEPATH_OR_CONTENT
                        Path to pofile or pofile content as string. If the input file stream is interactive, will be read from STDIN.

options:
  -h, --help            show this help message and exit
  -v, --version         Show program version number and exit.
  -m, --fallback-to-msgid
                        Use msgid if translation is missing.
  -f, --fuzzy           Include fuzzy messages.
  -p, --pretty          Pretty-print JSON output.
  -k, --sort-keys       Sort JSON output by keys.
  -i N, --indent N      Number of spaces for indentation used pretty-printing JSON output. Only takes effect passing the "--pretty" option.
  -l LANGUAGE, --language LANGUAGE
                        Language for the translations. Will be inserted in the empty key of the JSON output. If not provided and the passed pofile includes the "Language" header, will be extracted from it.
  -s PLURAL_FORMS, --plural-forms PLURAL_FORMS
                        Plural forms for the language of the translations. Will be inserted in the empty key of the JSON output. If not provided and the passed pofile includes the "Plural-Forms" header, will be extracted from it.
  -e ENCODING, --encoding ENCODING
                        The encoding to use reading the pofile. By default will be auto-detected.

Metadata

Release files for potojson 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for potojson 2.0.0
File Size Uploaded
potojson-2.0.0.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for potojson 2.0.0
File Interpreter ABI Platform
potojson-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / potojson-2.0.0.tar.gz

Download URL potojson-2.0.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2d368c6bfdf12ba31edce88f7c27b183ea1e419abd441605213e431f82d9f251
BLAKE2b-256 checksum
How to use checksums
e3457d9749af31ea974cf0c351c512feb354fe38d99c813b33d0b664f4562cd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / potojson-2.0.0-py3-none-any.whl

Download URL potojson-2.0.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d541f234e782bb9f029ef066149dc0c956da1f8f56b118cc7e91e7c2f9612e89
BLAKE2b-256 checksum
How to use checksums
25eb575729f658fa0bff6d24b9dcc3b60a2cc42bcd949bdd53c2b63901a63be0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.0

2 release files

0.0.18

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page