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

Implement pluralized add new translation string on web UI #11756

Conversation

harriebird
Copy link
Collaborator

@harriebird harriebird commented May 30, 2024

Proposed changes

This changes the way how the adding of new translation string done in the web UI.
This adds an option to add pluralized translation string.
This solves issue #7663.

Example image:
Screenshot 2024-06-18 at 23-38-58 Test Project_Test Component — English @ Devel Weblate

Checklist

  • Lint and unit tests pass locally with my changes.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added documentation to describe my feature.
  • I have squashed my commits into logic units.
  • I have described the changes in the commit messages.

Other information

Copy link

codecov bot commented May 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.62%. Comparing base (6aee586) to head (8b114d8).
Report is 2251 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #11756      +/-   ##
==========================================
- Coverage   90.82%   90.62%   -0.20%     
==========================================
  Files         554      577      +23     
  Lines       57306    58856    +1550     
  Branches     9122     9395     +273     
==========================================
+ Hits        52046    53340    +1294     
- Misses       3640     3824     +184     
- Partials     1620     1692      +72     
Files Coverage Δ
weblate/trans/forms.py 88.61% <100.00%> (+0.75%) ⬆️
weblate/trans/models/translation.py 86.74% <100.00%> (-0.47%) ⬇️
weblate/trans/tests/test_edit.py 100.00% <100.00%> (ø)
weblate/trans/views/basic.py 82.73% <ø> (-0.20%) ⬇️

... and 330 files with indirect coverage changes

@harriebird harriebird force-pushed the feature/allow-pluralized-translation-added-from-webapp branch 2 times, most recently from 2ae47ab to 9cc3dd4 Compare May 31, 2024 17:42
@harriebird harriebird marked this pull request as ready for review May 31, 2024 18:46
@harriebird harriebird force-pushed the feature/allow-pluralized-translation-added-from-webapp branch from 529dcc8 to f4fd982 Compare May 31, 2024 19:01
@harriebird harriebird force-pushed the feature/allow-pluralized-translation-added-from-webapp branch 2 times, most recently from a1a44b0 to e09a29b Compare June 2, 2024 16:05
Copy link
Member

@nijel nijel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I've made some minor comments.

@@ -663,6 +663,10 @@ def is_base(self, vals: tuple[str, ...]) -> bool:
"""Detect whether language is in given list, ignores variants."""
return self.base_code in vals

def get_source_plurals(self):
"""Return blank source fields for pluralized new string."""
return ["" for _ in range(self.plural.number)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return ["" for _ in range(self.plural.number)]
return [""] * self.plural.number

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the recommended change in the code.

</a>
<ul class="dropdown-menu">
<li><a href="#new-singular" data-toggle="tab">{% trans "Singular" %}</a></li>
<li><a href="#new-plural" data-toggle="tab">{% trans "Plural" %}</a></li>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the plural choice should be available only for languages having plural (plural.number > 1). Otherwise, there would be two same forms.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, updated the code the new plural translation string. The form for new plural string and those buttons that leads it will be only shown if plural.number > 1.

@nijel nijel added this to the 5.6 milestone Jun 13, 2024
@nijel nijel linked an issue Jun 13, 2024 that may be closed by this pull request
2 tasks
@harriebird harriebird force-pushed the feature/allow-pluralized-translation-added-from-webapp branch from cc0c79d to 30577f8 Compare June 14, 2024 12:50
@nijel nijel modified the milestones: 5.6, 5.7 Jun 17, 2024
@@ -74,9 +74,6 @@
{% endif %}
<li><a href="{% url 'data_project' project=object.component.project.slug %}">{% trans "Data exports" %}</a></li>
<li><a href="{% url 'checks' path=object.get_url_path %}">{% trans "Failing checks" %}</a></li>
{% if user_can_add_unit %}
<li><a href="#new" data-toggle="tab">{{ object.component.get_add_label }}</a></li>
{% endif %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really prefer to keep it this way so that link to #new still works.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted to the previous #new hyperlink to a tab containing the new Add new translation string form.

{% endif %}
</ul>
</li>
{% endif %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The top-level navigation has already enough items, so better to stick with placement in the Tools menu.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted this change.

</div>
</form>
</div>
{% endif %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have it on a single tab and toggle the forms there (maybe radio)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, It is now only a single tab. The form can be switched by selecting Singular or Plural radio buttons.

@@ -663,6 +663,10 @@ def is_base(self, vals: tuple[str, ...]) -> bool:
"""Detect whether language is in given list, ignores variants."""
return self.base_code in vals

def get_source_plurals(self):
"""Return blank source fields for pluralized new string."""
return [""] * self.plural.number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default language plural doesn't have to be the one which is actually being used, this should belong to the Translation class to get correct Plural instance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transferred the function get_source_plurals to Translation model.

def get_new_unit_form(translation, user, data=None, initial=None):
def get_new_unit_form(
translation, user, data=None, initial=None, is_source_plural=None
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that is_source_plural should be passed in all branches here, not just the one. Currently, the Singular and Plural forms are the same for me on monolingual translation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added is_source_plural to NewMonolingualUnitForm to address this.

@harriebird harriebird force-pushed the feature/allow-pluralized-translation-added-from-webapp branch from 30577f8 to 2c0e098 Compare June 18, 2024 14:58
data=data,
initial=initial,
is_source_plural=is_source_plural,
)
return NewBilingualUnitForm(translation, user, data=data, initial=initial)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least this also needs the logic (when adding string to bilingual translation), it still lacks the plural fields:

image

I'm not sure about the glossary forms above. Technically, there is no reason not to use plurals, but the default glossary format doesn't support it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured this out, both source and target in the NewBilingualUnitForm must receive the value of is_source_plural.

$("#new-singular").addClass("hidden");
$("#new-plural").removeClass("hidden");
}
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this also sync forms content? So that the text doesn't disappear when you toggle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this can now transfer the inputs when toggling between the singular and plural forms.

@nijel
Copy link
Member

nijel commented Jun 20, 2024

Tests need some adjustment:

======================================================================
FAIL: test_bilingual_new_plural_unit (weblate.trans.tests.test_edit.EditDTDTest.test_bilingual_new_plural_unit)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/weblate/weblate/weblate/trans/tests/test_edit.py", line 242, in test_bilingual_new_plural_unit
    self.test_new_plural_unit(args, "cs")
  File "/home/runner/work/weblate/weblate/weblate/trans/tests/test_edit.py", line 220, in test_new_plural_unit
    self.assertContains(
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/django/test/testcases.py", line 524, in assertContains
    text_repr, real_count, msg_prefix = self._assert_contains(
                                        ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/django/test/testcases.py", line 487, in _assert_contains
    self.assertEqual(
AssertionError: 403 != 200 : Couldn't retrieve content: Response code was 403 (expected 200)

Most likely, the failures are for formats not supporting plurals so the tests should assert that plurals do not work in that case?

See https://github.com/WeblateOrg/weblate/actions/runs/9583865006/job/26426111254?pr=11756

@harriebird harriebird force-pushed the feature/allow-pluralized-translation-added-from-webapp branch from 1b8c139 to 8b114d8 Compare June 20, 2024 08:27
@harriebird
Copy link
Collaborator Author

Hi @nijel, I can't directly reply on your comment about the test failure. I adjusted the test to skip those formats that are not supported.

@nijel nijel merged commit dd540b1 into WeblateOrg:main Jun 25, 2024
29 of 31 checks passed
@nijel
Copy link
Member

nijel commented Jun 25, 2024

Merged, thanks for your contribution!

@nijel nijel self-assigned this Jun 25, 2024
Comment on lines +309 to +315
<label class="radio-inline">
<input type="radio" name="new-unit-form-type" id="show-singular" value="singular" checked> {% trans "Singular" %}
</label>
<label class="radio-inline">
<input type="radio" name="new-unit-form-type" id="show-plural" value="plural"
{% if object.plural.number < 2 %} disabled{% endif %}> {% trans "Plural" %}
</label>
Copy link
Member

@nijel nijel Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These radios should be hidden if there is just a single form available (show only if {% if object.plural.number > 1 %}).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is there is a way to create pluralized translations from the UI?
2 participants