Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix expressions for tables
  • Loading branch information
CyrilleB79 committed Dec 1, 2023
commit 72f64751d1f4ba28a57d0724b6384c987c549cc2
52 changes: 52 additions & 0 deletions ource
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
diff --git a/source/speech/speech.py b/source/speech/speech.py
Comment thread
CyrilleB79 marked this conversation as resolved.
Outdated
index 07bcc675d..cb86f19f1 100644
--- a/source/speech/speech.py
+++ b/source/speech/speech.py
@@ -1883,10 +1883,18 @@ def getPropertiesSpeech( # noqa: C901
rowCount=propertyValues.get('rowCount',0)
columnCount=propertyValues.get('columnCount',0)
if rowCount and columnCount:
- # Translators: Sub-part of the compound string to speak number of columns and rows in a table
- rowCountTranslation: str = _("{rowCount} rows").format(rowCount=rowCount)
- # Translators: Sub-part of the compound string to speak number of columns and rows in a table
- colCountTranslation: str = _("{columnCount} columns").format(columnCount=columnCount)
+ rowCountTranslation: str = ngettext(
+ # Translators: Sub-part of the compound string to speak number of columns and rows in a table
+ "{rowCount} row",
+ "{rowCount} rows",
+ rowCount,
+ ).format(rowCount=rowCount)
+ colCountTranslation: str = ngettext(
+ # Translators: Sub-part of the compound string to speak number of columns and rows in a table
+ "{columnCount} column",
+ "{columnCount} columns",
+ columnCount,
+ ).format(columnCount=columnCount)
# Translators: Main part of the compound string to speak number of columns and rows in a table
# Example output: "with 3 rows and 2 columns"
# In this example {rowCountTranslation} will be replaced by "3 rows" and {colCountTranslation} by
@@ -2791,12 +2799,21 @@ def getTableInfoSpeech(
newTable=False
textList=[]
if newTable:
- # Translators: Sub-part of the compound string to report a table
columnCount=tableInfo.get("column-count",0)
# Translators: Sub-part of the compound string to report a table
rowCount=tableInfo.get("row-count",0)
- columnCountText = _("{columnCount} columns").format(columnCount=columnCount)
- rowCountText = _("{rowCount} rows").format(rowCount=rowCount)
+ columnCountText = ngettext(
+ # Translators: Sub-part of the compound string to report a table
+ "{columnCount} column",
+ "{columnCount} columns",
+ columnCount,
+ ).format(columnCount=columnCount)
+ rowCountText = ngettext(
+ # Translators: Sub-part of the compound string to report a table
+ "{rowCount} rows",
+ "{rowCount} rows",
+ rowCount,
+ ).format(rowCount=rowCount)
# Translators: Main part of the compound string to report a table
# Example output: table with 3 columns and 5 rows
# {columnCountText} is replaced by "3 columns" and {rowCountText} by "5 rows"
Expand Down
31 changes: 24 additions & 7 deletions source/speech/speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -1883,10 +1883,18 @@ def getPropertiesSpeech( # noqa: C901
rowCount=propertyValues.get('rowCount',0)
columnCount=propertyValues.get('columnCount',0)
if rowCount and columnCount:
# Translators: Sub-part of the compound string to speak number of columns and rows in a table
rowCountTranslation: str = _("{rowCount} rows").format(rowCount=rowCount)
# Translators: Sub-part of the compound string to speak number of columns and rows in a table
colCountTranslation: str = _("{columnCount} columns").format(columnCount=columnCount)
rowCountTranslation: str = ngettext(
# Translators: Sub-part of the compound string to speak number of columns and rows in a table
"{rowCount} row",
"{rowCount} rows",
rowCount,
).format(rowCount=rowCount)
colCountTranslation: str = ngettext(
# Translators: Sub-part of the compound string to speak number of columns and rows in a table
"{columnCount} column",
"{columnCount} columns",
columnCount,
).format(columnCount=columnCount)
# Translators: Main part of the compound string to speak number of columns and rows in a table
# Example output: "with 3 rows and 2 columns"
# In this example {rowCountTranslation} will be replaced by "3 rows" and {colCountTranslation} by
Expand Down Expand Up @@ -2791,12 +2799,21 @@ def getTableInfoSpeech(
newTable=False
textList=[]
if newTable:
# Translators: Sub-part of the compound string to report a table
columnCount=tableInfo.get("column-count",0)
# Translators: Sub-part of the compound string to report a table
rowCount=tableInfo.get("row-count",0)
columnCountText = _("{columnCount} columns").format(columnCount=columnCount)
rowCountText = _("{rowCount} rows").format(rowCount=rowCount)
columnCountText = ngettext(
# Translators: Sub-part of the compound string to report a table
"{columnCount} column",
"{columnCount} columns",
columnCount,
).format(columnCount=columnCount)
rowCountText = ngettext(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps all these repeated components should be factored out into helper functions that just take the count?

e.g. _columnCountStr(count), _rowCountStr(count)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I do not understand which part of the code I can put in a common helper function. Could you clarify please?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The following component is repeated multiple times in this code, same with the row equivalent:

ngettext(
	"{columnCount} column",
	"{columnCount} columns",
	columnCount,
).format(columnCount=columnCount)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@seanbudd, I do not see the point in making a helper function to just call ngettext and `format``; IMO it makes code less easy to read.
Instead, I have made a helper function to factor out the whole computation of table size announcement. See commit 70d32e4.

If you do not agree with this approach, I can revert this change.

@seanbudd seanbudd Dec 7, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it should be reverted.

The reason this was suggested is there is 2 instances of each of these components.
This is like when we assign translatable strings that are used repeatedly to a variable, so that there is less duplication and the strings can be updated across usages easily.

The current approach doesn't remove the duplication - the components are still here, only the other usage of the components were refactored.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK I got it. Done in 6db8c7d.

Note that I have kept the function _rowAndColumnCountText that I had extracted from getPropertiesSpeech since it allows to reduce a bit its complexity. getPropertiesSpeech remains much too complex though.

# Translators: Sub-part of the compound string to report a table
"{rowCount} rows",
"{rowCount} rows",
rowCount,
).format(rowCount=rowCount)
# Translators: Main part of the compound string to report a table
# Example output: table with 3 columns and 5 rows
# {columnCountText} is replaced by "3 columns" and {rowCountText} by "5 rows"
Expand Down