Skip to content

Commit 94559d4

Browse files
authored
Reorganize the documentation and add plenty of info (#6)
* Moved platform adapters to the bottom of the list * Moved around order in the docs. This sets a good structure * Added docs about common and extractor * typos * Added docs about translation component * typos * typos * minor * Added info about platform adapter component * Updated toctree * Added docs for Symfony * Added more docs about Symfony * typos * Added better toc * Minor fixes * Remove primary domain * added favicon
1 parent 5c5f116 commit 94559d4

24 files changed

+482
-87
lines changed

CONTRIBUTING

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Please see http://docs.php-http.org/en/latest/development/contributing.html
1+
Please see http://php-translation.readthedocs.io/en/latest/development/contributing.html

_static/custom.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
color: #b3b3b3;
1111
margin-top: 16px;
1212
margin-bottom: 0;
13+
margin-left: 1.4em
1314
}
1415
.wy-menu-vertical p.caption .caption-text {
1516
font-size: 120%;

assets/image/webui-dashboard.png

141 KB
Loading

assets/image/webui-page.png

94.2 KB
Loading

components/common.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Common
2+
======
3+
4+
The Common component is the least exiting component. It contains common interfaces
5+
and classes that are used by many other packages in this organization. The most
6+
important ones are listed on this page.
7+
8+
Message
9+
-------
10+
11+
The ``Message`` class is a representation of a translation in a specific language. This
12+
class is commonly used as a parameter or a return value for functions in the organisation.
13+
14+
The message contains of an ``key``, ``domain``, ``locale`` and ``translation``.
15+
There is also an array where meta data can be stored. Example of usage of meta data
16+
could be when a third party translation service has flagged the translation as "fuzzy".
17+
18+
Storage
19+
-------
20+
21+
The ``Storage`` interface is an abstraction for places where you can store translations.
22+
There are many examples like on file system, in database or in any third party translation
23+
service. A ``Storage`` is very simple. It has methods for getting, updating and
24+
deleting a translation.
25+
26+
Exception
27+
---------
28+
29+
The ``Exception`` interface will decorate all the runtime exceptions in the organisation.

components/extractor.rst

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Extractor
2+
=========
3+
4+
The responsibility of the Extractor component is to get translation keys from the
5+
source code.
6+
7+
Installation and Usage
8+
----------------------
9+
10+
Install the extractor component with Composer
11+
12+
.. code-block:: bash
13+
14+
composer require php-translation/extractor
15+
16+
When the extractor is downloaded you may use it by doing the following:
17+
18+
.. code-block:: php
19+
20+
require "vendor/autoload.php";
21+
22+
use Translation\Extractor\Visitor\Php\Symfony as Visitor;
23+
24+
// Create extractor for PHP files
25+
$fileExtractor = new PHPFileExtractor()
26+
27+
// Add visitors
28+
$fileExtractor->addVisitor(new Visitor\ContainerAwareTrans());
29+
$fileExtractor->addVisitor(new Visitor\ContainerAwareTransChoice());
30+
$fileExtractor->addVisitor(new Visitor\FlashMessage());
31+
$fileExtractor->addVisitor(new Visitor\FormTypeChoices());
32+
33+
// Add the file extractor to Extractor
34+
$extractor = new Extractor();
35+
$extractor->addFileExtractor($this->getPHPFileExtractor());
36+
37+
//Start extracting files
38+
$sourceCollection = $extractor->extractFromDirectory('/foo/bar');
39+
40+
// Print the result
41+
foreach ($sourceCollection as $source) {
42+
echo sprintf('Key "%s" found in %s at line %d', $source-getMessage(), $source->getPath(), $source->getLine());
43+
}
44+
45+
Architecture
46+
------------
47+
48+
There is a lot of things happening the the code example above. Everything is very
49+
SOLID so it is easy to add your own extractors if you have custom features that
50+
you need to translate.
51+
52+
The class that we interact with after when we want to extract translations is the
53+
``Extractor`` class. It supports ``Extractor::extractFromDirectory(string)`` and
54+
the more flexible ``Extractor::extract(Finder)``. The Extractor looks at all files
55+
in the directory and checks the type/extension. The extractor then executes all
56+
``FileExtractor`` for this file type.
57+
58+
There is a few ``FileExtractor`` that comes with this component. They are ``PHPFileExtractor``,
59+
``TwigFileExtractor`` and ``BladeExtractor``. As you may guess they extract translations
60+
from PHP files, Twig files and Blade files respectively. The most interesting ones
61+
are ``PHPFileExtractor`` and ``TwigFileExtractor`` because they are using the `Visitor pattern`_
62+
to parse all the nodes in the document.
63+
64+
Let's focus on the ``PHPFileExtractor`` only for a moment. We are using the Nikic
65+
PHP Parser to split the PHP source into an abstract syntax tree which enables us
66+
to statically analyze the source. Read more about this in the `nikic/php-parser`_
67+
documentation. When you add a visitor to the ``PHPFileExtractor`` it will be called
68+
for each node in the syntax tree.
69+
70+
The visitors is very specific with what they are looking for. The ``FlashMessage``
71+
visitor is searching for a pattern like ``$this->addFlash()``. If that string is
72+
found it will add a new ``SourceLocation`` to the ``SourceCollection`` model.
73+
74+
When all visitors and ``FileExtractor`` has been executed an instance of the ``SourceCollection``
75+
will be returned.
76+
77+
.. note::
78+
79+
If you want to add functionality to the extractor you are most likely to add
80+
a new visitor. See :doc:`../guides/adding-extractor` for more information.
81+
82+
83+
84+
.. _`Visitor pattern`: https://en.wikipedia.org/wiki/Visitor_pattern
85+
.. _`nikic/php-parser`: https://github.com/nikic/PHP-Parser
86+

components/platform-adapters.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Platform Adapters
2+
=================
3+
4+
To be able to integrate with all the third party translation platforms we need adapters
5+
for each of them. The adapter converts the ``ThirdPartyPlatformApiClient`` into
6+
a ``Translation\Common\Storage``.
7+
8+
The `platform adapter repository`_ is a "monolithic" repository. It uses `www.subtreesplit.com`_
9+
to push changes from ``php-translation/platform-adapter`` to ``php-translation/foo-adapter``.
10+
This setup will make it easier to maintain the adapters since it requires only one
11+
pull request to make a change on many adapters. Note that ``php-translation/foo-adapter``
12+
is a **read only** repository and changes should go to ``php-translation/platform-adapter``.
13+
14+
The Adapters
15+
------------
16+
17+
Each adapter has a a dependency on a API client. They also have different Composer
18+
requirements and may support different PHP version. All the adapters has a Symfony
19+
bundle for ease the integration with Symfony.
20+
21+
To install an adapter in Symfony you need to download it with Composer, activate
22+
the bundle in ``AppKernel`` and then add some configuration. This procedure is described
23+
at each adapters repository.
24+
25+
.. note::
26+
27+
See guide :doc:`../guides/using-loco-adapter`
28+
29+
The table below shows the adapters and their platform where you can read more about
30+
the service.
31+
32+
33+
.. csv-table::
34+
:header: "Platform", "Repository", "Badges"
35+
36+
"`Loco/Localise.biz <https://localise.biz/>`_", "`php-translation/loco-adapter <https://github.com/php-translation/loco-adapter/>`_", |vLoco| |dLoco|
37+
38+
.. _`platform adapter repository`: https://github.com/php-translation/platform-adapter
39+
.. _`www.subtreesplit.com`: https://www.subtreesplit.com/
40+
41+
.. Badges:
42+
43+
.. |vLoco| image:: https://poser.pugx.org/php-translation/loco-adapter/v/stable
44+
:target: https://packagist.org/packages/php-translation/loco-adapter
45+
.. |dLoco| image:: https://poser.pugx.org/php-translation/loco-adapter/downloads
46+
:target: https://packagist.org/packages/php-translation/loco-adapter
47+
48+

components/symfony-bundle.rst

Lines changed: 0 additions & 59 deletions
This file was deleted.

components/translator.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Translator
2+
==========
3+
4+
The Translator component provides an interface for translation services like Google
5+
Translate or Bing Translate.
6+
7+
Installation and Usage
8+
----------------------
9+
10+
Install the extractor component with Composer
11+
12+
.. code-block:: bash
13+
14+
composer require php-translation/translator
15+
16+
.. code-block:: php
17+
18+
require "vendor/autoload.php";
19+
20+
$translator = new Translator();
21+
$translator->addTranslatorService(new GoogleTranslator('api_key'));
22+
23+
echo $translator->translate('apple', 'en', 'sv'); // "äpple"
24+
25+
Architecture
26+
------------
27+
28+
The ``Translator`` class could be considered a "chain translator" it asks the first
29+
translation service to translate the string. If the translation fails it asks the
30+
second service until a translation is found. If no translation is found a ``null``
31+
value will be returned.
32+
33+
The ``Translator`` class is SOLID so you can easily add your custom translator into
34+
the chain.
35+
36+
.. note::
37+
38+
Since most translator services are paid services you probably want to add aggressive
39+
caching on the responses. See :doc:`../guides/configure-httplug` for more information.

conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# PHP-HTTP documentation build configuration file, created by
3+
# PHP-Translation documentation build configuration file, created by
44
# sphinx-quickstart on Sat Jan 2 15:26:57 2016.
55
#
66
# This file is execfile()d with the current directory set to its
@@ -21,7 +21,7 @@
2121
lexers['php'] = PhpLexer(startinline=True, linenos=1)
2222
lexers['php-annotations'] = PhpLexer(startinline=True)
2323

24-
primary_domain = 'php'
24+
# primary_domain = 'php'
2525
highlight_language = 'php'
2626

2727
# If extensions (or modules to document with autodoc) are in another directory,

0 commit comments

Comments
 (0)