Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ Check that each token has a position specifier with it. e.g. `%s` is not allowed

</details>

## Placeholder token explanation

Identifier: `placeholder_token_explanation`
Opt-In: `true`

Checks that if a placeholder is used in a string, the comment explicitly explains what it is replaced with.
Precondition: Each placeholder in the string and its explanation in comment is expected to follow `token_position_identifiers` rule.

# Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Expand Down
7 changes: 7 additions & 0 deletions localizationkit/localization_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def tokens(self) -> List[str]:
"""
return LocalizedString._TOKEN_PATTERN.findall(self.value)

def comment_tokens(self) -> List[str]:
"""Find and return the tokens in the comment string.

:returns: The list of tokens in the comment string
"""
return LocalizedString._TOKEN_PATTERN.findall(self.comment)

def __str__(self) -> str:
"""Generate and return the string representation of the object.

Expand Down
3 changes: 2 additions & 1 deletion localizationkit/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import localizationkit.tests.invalid_tokens
import localizationkit.tests.key_length
import localizationkit.tests.objectivec_alternative_tokens
import localizationkit.tests.placeholder_token_explanation
import localizationkit.tests.swift_interpolation
import localizationkit.tests.token_matching
import localizationkit.tests.token_position_identifiers
import localizationkit.tests.token_position_identifiers
40 changes: 40 additions & 0 deletions localizationkit/tests/placeholder_token_explanation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Placeholder token explanation."""
Comment thread
PoonamBaxi marked this conversation as resolved.
Resolved

from typing import Any, Dict, List

from localizationkit.tests.test_case import LocalizationTestCase


class PlaceholderTokenExplanation(LocalizationTestCase):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this isn't a common requirement. We should probably make it opt-in.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

PoonamBaxi This issue is still there.

Note: I've had to remove everyone from the Outlook Mobile team for security purposes, so let me know if you can't push any more.

@PoonamBaxi PoonamBaxi Jan 21, 2021 •

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.

Dale (@dalemyers) Sorry not sure how I missed adding the class method for is_opt_in(cls) I fixed it locally but can't push
Pushing to https://github.com/microsoft/localizationkit.git remote: Permission to microsoft/localizationkit.git denied to PoonamBaxi. fatal: unable to access 'https://github.com/microsoft/localizationkit.git/': The requested URL returned error: 403

Can you take a look please? Thanks!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Fixed this now. For future contributions, you'll need to fork and open a PR that way (just as standard).

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.

Dale (@dalemyers) Thanks for pushing the fix, I am not sure I followed your comment above. I had forked the master branch and used that for the change, also I fixed the issue locally but now cannot push to this branch anymore as my permissions are not working.

"""Check the placeholder tokens in strings have explanation in comments."""

@classmethod
def name(cls) -> str:
return "placeholder_token_explanation"

@classmethod
def default_settings(cls) -> Dict[str, Any]:
return {}

@classmethod
def is_opt_in(cls) -> bool:
return True

def run_test(self) -> List[str]:

violations = []

for string in self.collection.localized_strings:
tokens = set(string.tokens())
comment_tokens = set(string.comment_tokens())

extra_in_tokens = tokens - comment_tokens
extra_in_comments = comment_tokens - tokens

if len(extra_in_tokens) != 0:
violations.append(f"Tokens are not described in the comment ({extra_in_tokens}): {string}")

if len(extra_in_comments) != 0:
violations.append(f"Extra tokens appear in the comment ({extra_in_comments}): {string}")

return violations
73 changes: 73 additions & 0 deletions tests/test_placeholder_token_explanation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Placeholder token explanation tests."""

# pylint: disable=line-too-long

import os
import sys
import unittest

sys.path.insert(0, os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..")))
import localizationkit


class PlaceholderTokenExplanationTests(unittest.TestCase):
"""Placeholder token explanation tests."""

def setUp(self):
current_file_path = os.path.abspath(__file__)
current_folder_path = os.path.dirname(current_file_path)
self.config_path = os.path.abspath(os.path.join(current_folder_path, "config.toml"))
self.configuration = localizationkit.Configuration.from_file(self.config_path)

def test_placeholder_token_explanation(self):
"""Test that all placeholder token explanations exists in comments"""
test_cases = [
(
True,
localizationkit.LocalizedString(
"Key", "This is a string with no tokens", "Some comment", "en"
),
),
(
True,
localizationkit.LocalizedString(
"Key", "This is a string with one token: %@", "Some comment %@ token explanation", "en"
),
),
(
True,
localizationkit.LocalizedString(
"Key", "This is a string with two tokens: %1$@ %2$@", "Some comment %1$@ token explanantion %2$@ token explanantion", "en"
),
),
(
False,
localizationkit.LocalizedString(
"Key",
"This is a string with two tokens: %1$@ %2$@",
"Some comment missing all token explanation",
"en",
),
),
(
False,
localizationkit.LocalizedString(
"Key", "This is a string with two tokens: %1$@ %2$@", "Some comment %@ token explanation missing some token explanantion", "en"
),
),
(
False,
localizationkit.LocalizedString(
"Key", "This is a string", "Some comment %@ extra token explanation", "en"
),
),
]

for expected_result, string in test_cases:
collection = localizationkit.LocalizedCollection([string])
test = localizationkit.tests.placeholder_token_explanation.PlaceholderTokenExplanation(
self.configuration,
collection
)
result = test.execute()
Comment thread
PoonamBaxi marked this conversation as resolved.
Resolved
self.assertEqual(expected_result, result.succeeded())