[Pkg-owncloud-commits] [owncloud-doc] 65/270: add l10n chapter

David Prévot taffit at moszumanska.debian.org
Thu Jul 31 03:53:02 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud-doc.

commit 9a5e341e0738cce6fd3024cbb124f1383707812c
Author: Bernhard Posselt <dev at bernhard-posselt.com>
Date:   Sun May 11 23:50:55 2014 +0200

    add l10n chapter
---
 developer_manual/app/l10n.rst | 120 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 119 insertions(+), 1 deletion(-)

diff --git a/developer_manual/app/l10n.rst b/developer_manual/app/l10n.rst
index d0eac3e..94041ff 100644
--- a/developer_manual/app/l10n.rst
+++ b/developer_manual/app/l10n.rst
@@ -1,4 +1,122 @@
+===========
 Translation
 ===========
 
-.. sectionauthor:: Bernhard Posselt <dev at bernhard-posselt.com>
\ No newline at end of file
+.. sectionauthor:: Bernhard Posselt <dev at bernhard-posselt.com>
+
+ownCloud's translation system is powered by `Transifex <https://www.transifex.com/projects/p/owncloud/>`_. To start translating sign up and enter a group. If your community app should be added to Transifex contact someone of the `core developers <http://owncloud.org/about/contact/>`_ to set it up for you.
+
+PHP
+===
+Should it ever be needed to use localized strings on the serverside simply inject the L10N service from the ServerContainer into the needed constructor
+
+
+.. code-block:: php
+
+    <?php
+    namespace OCA\MyApp\AppInfo;
+
+    use \OCP\AppFramework\App;
+
+    use \OCA\MyApp\Service\AuthorService;
+
+
+    class Application extends App {
+
+        public function __construct(array $urlParams=array()){
+            parent::__construct('myapp', $urlParams);
+
+            $container = $this->getContainer();
+
+            /**
+             * Controllers
+             */
+            $container->registerService('AuthorService', function($c) {
+                return new AuthorService(
+                    $c->query('L10N')
+                );
+            });
+
+            $container->registerService('L10N', function($c) {
+                return $c->query('ServerContainer')->getL10N($c->query('AppName'));
+            });
+        }
+    }
+
+Strings can then be translated in the following way:
+
+.. code-block:: php
+
+    <?php
+    namespace OCA\MyApp\Service;
+
+    use \OCP\IL10N;
+
+
+    class AuthorService {
+
+        private $trans;
+
+        public function __construct(IL10N $trans){
+            $this->trans = $trans;
+        }
+
+        public function getLanguageCode() {
+            return $this->trans->getLanguage();
+        }
+
+        public sayHello() {
+            return $this->trans->t('Hello');
+        }
+
+        public function getAuthorName($name) {
+            return $this->trans->t('Getting author %s', array($name));
+        }
+
+        public function getAuthors($count, $city) {
+            return $this->trans->n(
+                '%n author is currently in the city %s',  // singular string
+                '%n authors are currently in the city %s',  // plural string
+                $count,
+                array($city)
+            );
+        }
+    }
+
+
+
+Templates
+=========
+In every template the global variable **$l** can be used to translate the strings using its methods **t()** and **n()**:
+
+.. code-block:: php
+
+    <div><?php p($l->t('Showing %s files', $_['count'])); ?></div>
+
+    <button><?php p($l->t('Hide')); ?></button>
+
+JavaScript
+==========
+There is currently no good way to translate JavaScript strings. One way to still use translated strings in the scripts is to create an invisble HTML element with all the translations in it which can be parsed in the JavaScript code:
+
+.. code-block:: php
+
+    <ul id="translations">
+        <li id="add-new"><?php p($l->t('Add new file')); ?></li>
+    </ul>
+
+.. code-block:: js
+
+    var addNewTranslation = $('#add-new').text();
+
+Creating your own translatable files
+====================================
+If Transifex is not the right choice or the app is not accepted for translation, generate the gettext strings by yourself by creating an **l10n/** directory in the app folder and executing::
+
+
+    cd l10n/
+    perl ../../../l10n/l10n.pl read ..  # assuming your app is in owncloud/apps
+
+The translation script requires Locale::PO, installable via::
+
+        apt-get install liblocale-po-perl
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-doc.git



More information about the Pkg-owncloud-commits mailing list