Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Sphinx documentation for the project #18

Merged
merged 5 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions aiogram_i18n/cores/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@


class BaseCore(Generic[Translator], ABC):
"""
Is an abstract base class for implementing core functionality for translation.
"""

default_locale: Optional[str]
locales: Dict[str, Translator]

def __init__(self, default_locale: Optional[str] = None) -> None:
"""

:param default_locale: The default locale to be used for translations.
If not provided, it will default to None.
"""
self.default_locale = default_locale
self.locales = {}

@abstractmethod
def get(self, key: str, /, locale: str, **kwargs: Any) -> str:
...
pass

def get_translator(self, locale: str) -> Translator:
if locale not in self.locales:
Expand Down Expand Up @@ -74,7 +83,7 @@ def _find_locales(

@abstractmethod
def find_locales(self) -> Dict[str, Translator]:
...
pass

@property
def available_locales(self) -> Tuple[str, ...]:
Expand Down
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
12 changes: 12 additions & 0 deletions docs/_static/stylesheets/extra.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@font-face {
font-family: 'JetBrainsMono';
src: url('https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/web/woff2/JetBrainsMono-Regular.woff2') format('woff2'),
url('https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/web/woff/JetBrainsMono-Regular.woff') format('woff'),
url('https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/ttf/JetBrainsMono-Regular.ttf') format('truetype');
font-weight: 400;
font-style: normal;
}

code, kbd, pre {
font-family: "JetBrainsMono", "Roboto Mono", "Courier New", Courier, monospace;
}
8 changes: 8 additions & 0 deletions docs/backends/base.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
====================
Writing own backends
====================

.. automodule:: aiogram_i18n.cores.base.BaseCore
:members:
:undoc-members:
:show-inheritance:
9 changes: 9 additions & 0 deletions docs/backends/fluent_compile.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
==============
Fluent_compile
==============

.. autoclass:: aiogram_i18n.cores.fluent_compile_core.FluentCompileCore
:members:
:undoc-members:
:show-inheritance:
:inherited-members:
9 changes: 9 additions & 0 deletions docs/backends/fluent_runtime.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
==============
Fluent.runtime
==============

.. autoclass:: aiogram_i18n.cores.fluent_runtime_core.FluentRuntimeCore
:members:
:undoc-members:
:show-inheritance:
:inherited-members:
9 changes: 9 additions & 0 deletions docs/backends/gettext.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
===========
GLU Gettext
JrooTJunior marked this conversation as resolved.
Show resolved Hide resolved
===========

.. autoclass:: aiogram_i18n.cores.gnu_text_core.GNUTextCore
:members:
:undoc-members:
:show-inheritance:
:inherited-members:
109 changes: 109 additions & 0 deletions docs/backends/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
.. _Backends:

========
Backends
========

`aiogram-i18n` supports many backends of translation and localization systems,
so you can use any of them to localize your bot.

Fluent runtime
==============

`Fluent.runtime <https://pypi.org/project/fluent.runtime/>`_ is a part of Project Fluent,
which is a localization system developed by Mozilla for natural-sounding translations.
It's designed to unleash the expressive power of the natural language.

If you want to use Fluent it's recommended to use `fluent_compile` instead of runtime
as it's the most efficient way to use Fluent in Python.

Pros of using fluent.runtime
----------------------------

- Fluent leverages Unicode and handles complex grammatical cases and gender.
- It works on a wide range of platforms and operating systems.
- The fluent syntax is very readable which makes it easier for localizers to understand.
- It handles placeholders and markup with ease.

Cons of using fluent.runtime
----------------------------

- Fluent.runtime is still young compared to gettext, therefore the community and
support aren’t as extensive.
- Its complexity and design may be way above what small projects need.

fluent.runtime
======================

.. code-block:: bash

pip install aiogram-i18n[runtime]

Fluent compile
==============

`fluent-compiler <https://pypi.org/project/fluent-compiler/>`_ is a Python implementation
of Project Fluent, a localization framework designed to unleash the entire expressive
power of natural language translations.

it is mainly used to compile FTL files to AST (Abstract Syntax Tree).
It is very useful for run-time parsing.

Pros of using fluent-compiler
-----------------------------

- It's beneficial for handling FTL files on the runtime and compiling them into AST for efficient use.

Cons of using fluent-compiler
-----------------------------

- As with Fluent.runtime, fluent-compiler is relatively new and as such doesn't have
as extensive resources or community.

Install fluent-compiler
-----------------------

.. code-block:: bash

pip install aiogram-i18n[compiler]


GNU gettext
===========

`GNU gettext <https://docs.python.org/3/library/gettext.html>`_ is an
internationalization (i18n) and localization (l10n) framework commonly used in open
source projects. It works by providing string function calls in your code that wraps strings
meant for user interface, and then it generates .po files which are human-readable and editable,
containing the actual translations.

Pros of using GNU gettext
-------------------------

- Gettext is widely used and has a large community. It means there are many tools,
guides and supports available for it.
- Offers good support for plurals.
- It handles the translation outside of your code which makes your code cleaner.
- Gettext is part of the GNU Project, being present in most of the Linux Distros.

Cons of using GNU gettext
-------------------------

- It can be more difficult to provide context for your strings, because the only context
you can give for your source strings are comments in your code.
- Its complexity can be intimidating for non-technical translators.

Install GNU gettext
-------------------

.. code-block:: bash

pip install aiogram-i18n[gettext]


.. toctree::

fluent_compile
fluent_runtime
gettext
base
33 changes: 33 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
import aiogram_i18n

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = "aiogram-i18n"
copyright = "2023, RootShinobi"
author = "RootShinobi"
release = aiogram_i18n.__version__

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
"sphinx.ext.viewcode",
"sphinx.ext.autodoc",
]

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "furo"
html_static_path = ["_static"]
html_css_files = [
"stylesheets/extra.css",
]
95 changes: 95 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
========================================
Welcome to aiogram-i18n's documentation!
========================================


.. image:: https://img.shields.io/pypi/v/aiogram-i18n.svg
:target: https://pypi.python.org/pypi/aiogram-i18n


.. image:: https://img.shields.io/pypi/pyversions/aiogram-i18n.svg


.. image:: https://img.shields.io/pypi/l/aiogram-i18n.svg


.. image:: https://img.shields.io/pypi/dm/aiogram-i18n.svg


.. image::
https://readthedocs.org/projects/aiogram-i18n/badge/?version=latest
:target: https://aiogram-i18n.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status


**aiogram_i18n**, an unrivalled middleware for Telegram bot internationalization,
breathes life into bots, enabling diverse interactions in multiple languages,
while adeptly managing user context, paving the way for a truly engaging and
immersive user experience irrespective of language preference, and providing robust
support for both Fluent and GNU gettext localization systems,
thereby offering flexibility in translation file format selection,
streamlining the translation process, and making the creation of multilingual bots
an achievable goal for developers.

Installation
============

To install aiogram-i18n without any backends:

.. code-block:: bash

pip install aiogram-i18n

If you need help with what backend to choose, read :ref:`Backends`.


To use FluentCompileCore
------------------------

To use `fluent-compiler <https://pypi.org/project/fluent-compiler/>`_ backend
(:class:`aiogram_i18n.cores.fluent_compile_core.FluentCompileCore`) install it:

.. code-block:: bash

pip install aiogram-i18n[compiler]

To use FluentRuntimeCore
------------------------

To use `Fluent.runtime <https://pypi.org/project/fluent.runtime/>`_ backend
(:class:`aiogram_i18n.cores.fluent_runtime_core.FluentRuntimeCore`) install it:

.. code-block:: bash

pip install aiogram-i18n[runtime]

To use GNU gettext
------------------

To use `gettext <https://pypi.org/project/gettext/>`_ backend
(:class:`aiogram_i18n.cores.gnu_text_core.py.GettextCore`) install it:

.. code-block:: bash

pip install aiogram-i18n[gettext]

Usage example
=============

.. literalinclude:: ../examples/mre.py
:language: python
:linenos:


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

.. toctree::
:caption: Contents:

backends/index
poem
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
Loading