Skip to main content

Extracts FTL files from a directory and outputs them to a directory

Project description

FTL-Extract

Description

FTL-Extract is a Python package that extracts Fluent keys from .py files and generates a .ftl file with extracted keys.

Installation

Use the package manager pip to install FTL-Extract.

$ pip install FTL-Extract

Or add it to your pyproject.toml and run poetry update

Usage

First of all, you should to create locales directory in your project.

$ mkdir project_path/locales

Then, you can use the following command to extract keys from your code.

$ ftl_extract project_path/code_path project_path/locales

By default, FTL-Extract will create a directory named en and put all keys into _default.ftl file.

In more cases, you may want to extract keys to specific .ftl files. So, you must add _path argument to i18n.get function in your code.

# Before
i18n.get("key-1", arg1="value1", arg2="value2")

# After
i18n.get("key-1", arg1="value1", arg2="value2", _path="dir/ftl_file.ftl")

FAQ

❓ - What changed 🤔

You just need to add _path argument to i18n.get function and specify the path to the .ftl file where you want to put the key.

It may be just a filename like file.ftl or a path to a file like dir/file.ftl.

❓ - My FluentRuntimeCore throws an error 🤯, when I use _path argument

Now there is a little problem with integration with aiogram-i18n

To fix any possible problems - when you create a i18n middleware in your code:

i18n_middleware = I18nMiddleware(
    core=FluentRuntimeCore(path=Path(__file__).parent / "locales" / "{locale}"),
    manager=FSMManager(),
)

you should replace FluentRuntimeCore with your own patched core.

🤖 Example of your own Core

class CustomFluentRuntimeCore(FluentRuntimeCore):
    def get(self, message_id: str, locale: Optional[str] = None, /, **kwargs: Any) -> str:

    # PATCH START #
    kwargs.pop("_path", None)
    # PATCH END #

    locale = self.get_locale(locale=locale)
    translator: FluentBundle = self.get_translator(locale=locale)
    ...

Then just use this CustomFluentRuntimeCore in your i18n middleware as erlier.

❓ - How to add more languages to the project ?

# Here we add 3 languages: English, Ukrainian and Polish
$ ftl_extract project_path/code_path project_path/locales -l en -l uk -l pl

❓ - How to detect another i18n keys like LazyProxy or L ?

# Here we extract ftl keys from i18n-keys like `i18n`, `LF`, `LazyProxy` and `L`
$ ftl_extract project_path/code_path project_path/locales -k i18n -k LF -k LazyProxy -k L

How I use FTL-Extract in most of my projects

$ ftl_extract \
  '.\app\bot' \
  '.\app\bot\locales' \
  -l 'en' \
  -l 'uk' \
  -l 'pl' \
  -l 'de' \
  -l 'ja' \
  -l 'ru' \
  -k 'i18n' \
  -k 'L' \
  -k 'LF' \
  -k 'LazyProxy' \
  -a 'core' \
  --comment-junks

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ftl_extract-0.4.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

ftl_extract-0.4.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file ftl_extract-0.4.2.tar.gz.

File metadata

  • Download URL: ftl_extract-0.4.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for ftl_extract-0.4.2.tar.gz
Algorithm Hash digest
SHA256 a52fbc62e45aaedeca7d909997e49154d56cf03f037c05a6cbb5cbfdc6fa622d
MD5 9651ee882cf7723a4188f6d08a452fdc
BLAKE2b-256 8c9d03f46bbb39adb01fffe9f03fbaaea061b8a905be01a3c9a268913f0f2b70

See more details on using hashes here.

File details

Details for the file ftl_extract-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: ftl_extract-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for ftl_extract-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cfedc4657e17688be0be4a23abbcab0bee6197e926a0d7e8d3519b6bfaca5262
MD5 2f09633050ea108a002173ed459d4f70
BLAKE2b-256 7df0c6c06eb641820e11ce444ad8ceb65636d7ca71022cb53df2f3e7ba2b324b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page