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

Centralize Translation Functions for Common Keys - Part 1 #2002

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6e71f97
folder restructure
pranshugupta54 May 21, 2024
ae93cce
namespace added
pranshugupta54 May 21, 2024
dfa6b2b
fix for 'register' common
pranshugupta54 May 21, 2024
0c3d2f3
feat: Update screens to use tCommon for localization
pranshugupta54 May 21, 2024
762ef11
feat: Update screens to use tCommon for localization
pranshugupta54 May 21, 2024
62397e7
feat: Update screens to use tCommon for localization
pranshugupta54 May 21, 2024
c5b2a9d
feat: Update screens to use tCommon for localization - yes no
pranshugupta54 May 21, 2024
4886d13
feat: Update screens to use tCommon for localization - saveChanges
pranshugupta54 May 21, 2024
1beebb3
feat: Update screens to use tCommon for localization - email, name, pass
pranshugupta54 May 21, 2024
f741a5d
feat: Update screens to use tCommon for localization
pranshugupta54 May 21, 2024
e26e6ae
remove pass
pranshugupta54 May 21, 2024
14ccda1
Update localization for first name and last name fields
pranshugupta54 May 21, 2024
3660816
tCommon fixes
pranshugupta54 May 22, 2024
6abeab2
Update localization
pranshugupta54 May 22, 2024
055495b
Update UsersTableItem component to remove unnecessary code and improv…
pranshugupta54 May 22, 2024
48c5edf
Update common.json with startTime and endTime translations
pranshugupta54 May 22, 2024
9c372e1
Update localization for name field in screens
pranshugupta54 May 22, 2024
da5f423
Update localization for enterLocation field
pranshugupta54 May 22, 2024
0bd9781
translations in test
pranshugupta54 May 22, 2024
960084c
chore: Update OrgPostCard.test.tsx with language detector mock
pranshugupta54 May 22, 2024
74061fb
error translation fix
pranshugupta54 May 23, 2024
f252e5d
Refactor UsersTableItem component to optimize performance and remove …
pranshugupta54 May 23, 2024
19cf745
Update common.json with startTime and endTime translations
pranshugupta54 May 23, 2024
36a97e3
Update error translations in French, Hindi, Spanish, and Chinese locales
pranshugupta54 May 23, 2024
3ff0d34
Merge branch 'develop' into frontend-translations
pranshugupta54 May 23, 2024
2087439
fix variable
pranshugupta54 May 25, 2024
b58811e
fix tests usertableitem
pranshugupta54 May 25, 2024
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
56 changes: 36 additions & 20 deletions .github/workflows/compare_translations.py
Copy link
Member Author

Choose a reason for hiding this comment

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

@coderabbitai review changes in this file

Copy link
Contributor

Choose a reason for hiding this comment

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

@pranshugupta54, I've reviewed the changes in the compare_translations.py file. Here are my observations:

load_translation Function

  • Improvements:

    • The addition of a try-except block enhances error handling by catching JSON decoding errors, which improves the robustness of the function.
    • Checking for empty files before attempting to load them prevents runtime errors and provides a clearer error message, which is a good practice.
  • Suggestions:

    • Currently, the function raises a generic ValueError for different types of errors (empty file, JSON decoding). It might be beneficial to introduce custom exceptions for different error types for more granular error handling.

check_translations Function

  • Improvements:

    • The function now handles multiple default files (common.json, errors.json, translation.json) instead of a single en.json. This is a significant improvement as it allows the script to be more flexible and comprehensive in managing different translation files.
    • The restructuring to iterate over multiple languages and files makes the script more scalable and adaptable to different directory structures.
  • Suggestions:

    • Consider adding logging or a more detailed reporting mechanism to provide insights into the process, especially useful when debugging or when used in larger projects.
    • It might be useful to add functionality to handle cases where additional translation files are present in other languages but not in the default language directory.

Overall, the changes are quite beneficial, enhancing the functionality, error handling, and robustness of the script. If you have any specific areas you'd like further insight on or additional features you're considering, feel free to let me know!

Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,15 @@ def load_translation(filepath):
Returns:
translation: Loaded translation
"""
with open(filepath, "r", encoding="utf-8") as file:
translation = json.load(file)
return translation
try:
with open(filepath, "r", encoding="utf-8") as file:
content = file.read()
if not content.strip():
raise ValueError(f"File {filepath} is empty.")
translation = json.loads(content)
return translation
except json.JSONDecodeError as e:
raise ValueError(f"Error decoding JSON from file {filepath}: {e}")


def check_translations(directory):
Expand All @@ -112,26 +118,36 @@ def check_translations(directory):
Returns:
None
"""
default_file = "en.json"
default_translation = load_translation(os.path.join(directory, default_file))
translations = os.listdir(directory)
translations.remove(default_file) # Exclude default translation
default_language_dir = os.path.join(directory, "en")
default_files = ["common.json", "errors.json", "translation.json"]
default_translations = {}
for file in default_files:
file_path = os.path.join(default_language_dir, file)
default_translations[file] = load_translation(file_path)

languages = os.listdir(directory)
languages.remove("en") # Exclude default language directory


error_found = False

for translation_file in translations:
other_file = os.path.join(directory, translation_file)
other_translation = load_translation(other_file)
for language in languages:
language_dir = os.path.join(directory, language)
for file in default_files:
default_translation = default_translations[file]
other_file_path = os.path.join(language_dir, file)
other_translation = load_translation(other_file_path)

# Compare translations and get detailed error messages
errors = compare_translations(
default_translation, other_translation, f"en/{file}", f"{language}/{file}"
)
if errors:
error_found = True
print(f"File {language}/{file} has missing translations for:")
for error in errors:
print(f" - {error}")

# Compare translations and get detailed error messages
errors = compare_translations(
default_translation, other_translation, default_file, translation_file
)
if errors:
error_found = True
print(f"File {translation_file} has missing translations for:")
for error in errors:
print(f" - {error}")

if error_found:
sys.exit(1) # Exit with an error status code
Expand All @@ -154,7 +170,7 @@ def main():
"--directory",
type=str,
nargs="?",
default=os.path.join(os.getcwd(), "public/locales"),
default=os.path.join(os.getcwd(), "locales"),
help="Directory containing translation files(relative to the root directory).",
)
args = parser.parse_args()
Expand Down
51 changes: 51 additions & 0 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"firstName": "First Name",
"lastName": "Last Name",
"searchByName": "Search By Name",
"loading": "Loading...",
"endOfResults": "End of results",
"noResultsFoundFor": "No results found for ",
"edit": "Edit",
"admins": "Admins",
"admin": "ADMIN",
"user": "USER",
"superAdmin": "SUPERADMIN",
"members": "Members",
"logout": "Logout",
"login": "Login",
"register": "Register",
"menu": "Menu",
"settings": "Settings",
"users": "Users",
"requests": "Requests",
"OR": "OR",
"cancel": "Cancel",
"close": "Close",
"create": "Create",
"delete": "Delete",
"done": "Done",
"yes": "Yes",
"no": "No",
"filter": "Filter",
"search": "Search",
"description": "Description",
"saveChanges": "Save Changes",
"displayImage": "Display Image",
"enterEmail": "Enter Email",
"emailAddress": "Email Address",
"email": "Email",
"name": "Name",
"enterPassword": "Enter Password",
"password": "Password",
"confirmPassword": "Confirm Password",
"forgotPassword": "Forgot Password ?",
"talawaAdminPortal": "Talawa Admin Portal",
"address": "Address",
"location": "Location",
"enterLocation": "Enter Location",
"joined": "Joined",
"startDate": "Start Date",
"endDate": "End Date",
"startTime": "Start Time",
"endTime": "End Time"
}
9 changes: 9 additions & 0 deletions public/locales/en/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"talawaApiUnavailable": "Talawa-API service is unavailable!. Is it running? Check your network connectivity too.",
"notFound": "Not found",
"unknownError": "An unknown error occurred. Please try again later. {{msg}}",
"notAuthorised": "Sorry! you are not Authorised!",
"errorSendingMail": "Error sending mail",
"emailNotRegistered": "Email not registered",
"notFoundMsg": "Oops! The Page you requested was not found!"
}
Loading
Loading