From af08e6ad5e258284341894b66b4f8329cc4f06f0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Thu, 29 Jan 2015 14:09:39 +0100
Subject: [PATCH] =?UTF-8?q?D=C3=A9placement=20des=20traductions=20dans=20l?=
 =?UTF-8?q?e=20domaine=20"file=5Fupload"=20et=20utilisation=20dans=20les?=
 =?UTF-8?q?=20templates=20et=20le=20JS.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../IrsteaFileUploadExtension.php             |  1 +
 Resources/js/widget/file_upload.js            | 27 +++++++++++--------
 Resources/less/file_upload.less               | 14 +++++++---
 Resources/translations/file_upload.fr.yml     | 18 +++++++++++++
 Resources/translations/messages.fr.yml        | 10 -------
 Resources/views/Form/file_upload.html.twig    |  8 +++---
 Twig/FileUploadExtension.php                  |  4 +--
 7 files changed, 52 insertions(+), 30 deletions(-)
 create mode 100644 Resources/translations/file_upload.fr.yml

diff --git a/DependencyInjection/IrsteaFileUploadExtension.php b/DependencyInjection/IrsteaFileUploadExtension.php
index f8ff6eba..96359cfb 100644
--- a/DependencyInjection/IrsteaFileUploadExtension.php
+++ b/DependencyInjection/IrsteaFileUploadExtension.php
@@ -48,6 +48,7 @@ class IrsteaFileUploadExtension extends Extension implements PrependExtensionInt
                             "$pluginDir/js/jquery.fileupload-validate.js",
                             "$pluginDir/js/jquery.fileupload-ui.js",
                             "@BazingaJsTranslationBundle/Resources/js/translator.js",
+                            'js/translations/file_upload/*.js',
                             '@IrsteaFileUploadBundle/Resources/js/widget/file_upload.js'
                         ],
                     ],
diff --git a/Resources/js/widget/file_upload.js b/Resources/js/widget/file_upload.js
index b416a306..2fd78ee2 100644
--- a/Resources/js/widget/file_upload.js
+++ b/Resources/js/widget/file_upload.js
@@ -2,19 +2,23 @@
  * Copyright (C) 2015 IRSTEA
  * All rights reserved.
  */
-(function($) {
+(function($, Translator) {
 
     var formatFileSize = function(size) {
+            var unit;
             if(size > 1000000000) {
-                return (size/1000000000).toFixed(2) + ' Gio';
+                size = (size/1000000000).toFixed(2);
+                unit = 'Gi';
+            } else if(size > 1000000) {
+                size = (size/1000000).toFixed(2);
+                unit = 'Mi';
+            } else if(size > 1000) {
+                size = (size/1000).toFixed(2);
+                unit ='Ki';
+            } else {
+                unit = '';
             }
-            if(size > 1000000) {
-                return (size/1000000).toFixed(2) + ' Mio';
-            }
-            if(size > 1000) {
-                return (size/1000).toFixed(2)+ ' Kio';
-            }
-            return size + ' o';
+            return Translator.trans('file_size(%size%,%unit%)', {size:size, unit:unit}, 'file_upload');
         },
         formatBitrate = function(rate) {
             return formatFileSize(rate) + '/s';
@@ -48,7 +52,7 @@
         var showError = function(entry, message) {
                 var $this = $(entry);
                 if(message) {
-                    $this.find('.error').text(message);
+                    $this.find('.error').text(Translator.trans(message));
                 }
                 $this.addClass('alert alert-danger');
                 $this.find('.error').show();
@@ -84,6 +88,7 @@
                 downloadTemplateId: null,
                 filesContainer: $this.find('.fileinput-entries'),
                 dropZone: $this,
+                i18n: Translator.trans,
                 uploadTemplate: function(data) {
                     var rows = $();
                     $.each(data.files, function (index, file) {
@@ -186,4 +191,4 @@
 
     };
 
-})(jQuery);
+})(jQuery, Translator);
diff --git a/Resources/less/file_upload.less b/Resources/less/file_upload.less
index 8a942873..2c76b954 100644
--- a/Resources/less/file_upload.less
+++ b/Resources/less/file_upload.less
@@ -6,15 +6,15 @@ All rights reserved.
 @import "variables.less";
 
 .fileinput-entry {
-    & > * {
-        margin: 0 0.2em;
+    .cancel, .delete {
+        margin-left: 0.2em;
     }
     .description {
         display: inline;
         width: auto;
         height: auto;
         padding: @padding-xs-vertical @padding-xs-horizontal;
-        //margin: 0;
+        margin: 0 0.2em;
     }
     .progress {
         height: (@line-height-computed / 2);
@@ -34,6 +34,9 @@ All rights reserved.
         }
         .error {
             display: inline;
+            &:after {
+                content: ".";
+            }
         }
         .size, .description {
             display: none;
@@ -49,3 +52,8 @@ All rights reserved.
     height: auto !important;
     min-height: @input-height-base;
 }
+
+.template-upload .size {
+    &:before { content: "("; }
+    &:after  { content: ")"; }
+}
diff --git a/Resources/translations/file_upload.fr.yml b/Resources/translations/file_upload.fr.yml
new file mode 100644
index 00000000..c60741ff
--- /dev/null
+++ b/Resources/translations/file_upload.fr.yml
@@ -0,0 +1,18 @@
+form:
+    description.placeholder: Description facultative
+    max_number_of_files(%num%): %num% fichiers maximum
+    min_file_size: Taille minimale
+    max_file_size: Taille maximale
+
+file_size(%size%,%unit%): %size% %unit%o
+
+# Message d'erreurs du plugin
+unknownError: Erreur inconnue
+maxNumberOfFiles: Nombre maximum de fichiers dépassé
+acceptFileTypes: Type de fichier non autorisé
+maxFileSize: Le fichier est trop grand
+minFileSize: Le fichier est trop petit
+
+# Messages d'erreur du serveur
+"Invalid CSRF Token": Token CSRF invalide, veuillez recharger la page.
+"Internal server error": Erreur interne du serveur
diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml
index 4a06d254..bdbe1a71 100644
--- a/Resources/translations/messages.fr.yml
+++ b/Resources/translations/messages.fr.yml
@@ -1,12 +1,2 @@
 
 button.upload: Ajouter un fichier
-
-form:
-    file_upload:
-        description.placeholder: Description facultative
-        max_number_of_files(%num%): %num% fichiers maximum
-        min_file_size: Taille minimale
-        max_file_size: Taille maximale
-
-file_upload:
-    file_size(%size%,%unit%): %size% %unit%o
diff --git a/Resources/views/Form/file_upload.html.twig b/Resources/views/Form/file_upload.html.twig
index 05427573..5d32376d 100644
--- a/Resources/views/Form/file_upload.html.twig
+++ b/Resources/views/Form/file_upload.html.twig
@@ -19,7 +19,7 @@
     <div class="template-download fileinput-entry">
         <input class="id" type="hidden" name="{{ full_name }}{% if multiple %}[{{index|default('__index__')}}]{% endif %}[id]" value="{{ file.id|default }}"/>
         {{ irstea_uploaded_file(file) }}
-        <input class="form-control description" type="text" placeholder="{% trans %}form.file_upload.description.placeholder{% endtrans %}" name="{{ full_name }}{% if multiple %}[{{index|default('__index__')}}]{% endif %}[description]" value="{{ file.description|default }}"/>
+        <input class="form-control description" type="text" placeholder="{% trans from 'file_upload' %}form.description.placeholder{% endtrans %}" name="{{ full_name }}{% if multiple %}[{{index|default('__index__')}}]{% endif %}[description]" value="{{ file.description|default }}"/>
         <span class="error"></span>
         <a href="#" class="danger delete {%- if disabled or read_only %} disabled{% endif -%}" title="{% trans %}button.delete{% endtrans %}">
             {{- irstea_icon('remove') -}}
@@ -55,13 +55,13 @@
             />
         </div>
         {% if multiple and widget_attr["data-max-number-of-files"] is defined %}
-            &nbsp;<small>{% trans with {"%num%": widget_attr["data-max-number-of-files"]}%}form.file_upload.max_number_of_files(%num%){% endtrans %}.</small>
+            &nbsp;<small>{% trans with {"%num%": widget_attr["data-max-number-of-files"]}  from 'file_upload' %}form.max_number_of_files(%num%){% endtrans %}.</small>
         {% endif %}
         {% if widget_attr["data-min-file-size"] is defined %}
-            &nbsp;<small>{% trans %}form.file_upload.min_file_size{% endtrans %}&nbsp;: {{ widget_attr["data-min-file-size"]|irstea_file_size(0) }}</small>
+            &nbsp;<small>{% trans from 'file_upload' %}form.min_file_size{% endtrans %}&nbsp;: {{ widget_attr["data-min-file-size"]|irstea_file_size(0) }}</small>
         {% endif %}
         {% if widget_attr["data-max-file-size"] is defined %}
-            &nbsp;<small>{% trans %}form.file_upload.max_file_size{% endtrans %}&nbsp;: {{ widget_attr["data-max-file-size"]|irstea_file_size(0) }}</small>
+            &nbsp;<small>{% trans from 'file_upload' %}form.max_file_size{% endtrans %}&nbsp;: {{ widget_attr["data-max-file-size"]|irstea_file_size(0) }}</small>
         {% endif %}
     </div>
 {% endblock file_upload_widget %}
diff --git a/Twig/FileUploadExtension.php b/Twig/FileUploadExtension.php
index bd680f86..e7ffb8af 100644
--- a/Twig/FileUploadExtension.php
+++ b/Twig/FileUploadExtension.php
@@ -124,12 +124,12 @@ class FileUploadExtension extends Twig_Extension
         $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, null !== $precision ? $precision : $defaultPrecision);
 
         return $this->translator->trans(
-            'file_upload.file_size(%size%,%unit%)',
+            'file_size(%size%,%unit%)',
             [
                 '%size%' => $formatter->format($size),
                 '%unit%' => $unit,
             ],
-            null,
+            'file_upload',
             $locale
         );
     }
-- 
GitLab