Repository: translate/pootle

Issue:
Add current character count when entering translations
We're trying to use Pootle to translate metadata strings for an app in the iOS AppStore. The metadata includes individual messages for the app name, subtitle, description, privacy URL and so on and there are different limits on the number of characters allowed in each of them. For instance, an app's name can be no more than 30 characters. 

When entering translations, it would be really helpful to see the current number of characters that the translation uses as you type to ensure that you're not exceeding the limit. This could maybe fit on the lower right corner of the input view. You currently have timeline / comment / raw on the left. Current character count could just be a small label that floats to the right on the same line.

# Environment (i.e. 'pootle --version', DB, OS, Browser):

Pootle 2.8.0


Discussion:
@Johennes does Apple specify these limitation somewhere or are they available in the file that needs translation.  Having a counter still means that a translator needs to know these limitations. If we actually know the limitations we are able to flag somehow that you've gone over.

Possible methods to flag:
1. A custom check
2. Adding the counter when you are close or go over.
@dwaynebailey The limits are scattered over [this](https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/iTunesConnect_Guide/Chapters/Properties.html) document. The source files are plain text TXTs.

```bash
> ls metadata/de-DE/*.txt
../metadata/de-DE/description.txt
../metadata/de-DE/keywords.txt
../metadata/de-DE/marketing_url.txt
../metadata/de-DE/name.txt
../metadata/de-DE/privacy_url.txt
../metadata/de-DE/promotional_text.txt
../metadata/de-DE/release_notes.txt
../metadata/de-DE/subtitle.txt
../metadata/de-DE/support_url.txt
```

We convert the TXTs into POs / POTs with a shell script and had planned to add the limit in an extracted comment (`#.`).

For clarity, my main request is just seeing the current character count as I type in a translation. I don't necessarily need it to be validated against a preconfigured limit (though that would obviously be nice but probably also a lot more complicated).
This may be super over-simplified but I had just imagined this to be a small JavaScript snippet that listens for text changes in an `oninput` handler and then updates the character count as I type.
I'd also be open to contribute here but it would be great to have an agreement on whether or not the minimal scope I described above would be desirable from your point of view. If so, I could just work it into the [edit page](https://github.com/translate/pootle/blob/c98c94c19782e52e8789e496d1f78385d0debc52/pootle/templates/editor/units/edit.html).
if you were able to put it together @Johennes i think it would be pretty welcome - you probably want to look at the react classes for the editor i think https://github.com/translate/pootle/blob/master/pootle/static/js/editor/components/Editor.js
hmm maybe not - perhaps you could do it with jquery - ping on gitter if i can help at all

Resolving patch:
diff --git a/pootle/core/templatetags/core.py b/pootle/core/templatetags/core.py
--- a/pootle/core/templatetags/core.py
+++ b/pootle/core/templatetags/core.py
@@ -25,6 +25,13 @@ def to_js(value):
     return mark_safe('JSON.parse("%s")' % escapejs(jsonify(value)))
 
 
+@register.filter
+def map_to_lengths(value):
+    """Maps a list value by replacing each element with its length.
+    """
+    return [len(e) for e in value]
+
+
 @register.inclusion_tag('includes/formtable.html')
 def formtable(formtable):
     return dict(formtable=formtable)


Test patch:
diff --git a/tests/core/templatetags/core.py b/tests/core/templatetags/core.py
new file mode 100644
--- /dev/null
+++ b/tests/core/templatetags/core.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) Pootle contributors.
+#
+# This file is a part of the Pootle project. It is distributed under the GPL3
+# or later license. See the LICENSE file for a copy of the license and the
+# AUTHORS file for copyright and authorship information.
+
+from pootle.core.templatetags.core import map_to_lengths
+
+
+def test_map_to_length_empty_list():
+    assert map_to_lengths([]) == []
+
+
+def test_map_to_length_one_element():
+    assert map_to_lengths(['foo']) == [3]
+
+
+def test_map_to_length_multiple_elements():
+    assert map_to_lengths(['foobar', 'foo', 'f']) == [6, 3, 1]