[damned-lies/gettext-check: 2/3] feat: pre-commit hook to detect string changes
- From: Guillaume Bernard <gbernard src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [damned-lies/gettext-check: 2/3] feat: pre-commit hook to detect string changes
- Date: Fri, 25 Jun 2021 06:49:12 +0000 (UTC)
commit 2b27c631cfce9e75224a803b2309af2186c6af09
Author: Guillaume Bernard <associations guillaume-bernard fr>
Date: Sun May 2 16:12:54 2021 +0200
feat: pre-commit hook to detect string changes
.githooks/check_messages_did_change_or_not.sh | 26 ++++++++++++++++++++++++++
.githooks/pre-commit | 13 +++++++++++++
2 files changed, 39 insertions(+)
---
diff --git a/.githooks/check_messages_did_change_or_not.sh b/.githooks/check_messages_did_change_or_not.sh
new file mode 100644
index 00000000..6bc287a2
--- /dev/null
+++ b/.githooks/check_messages_did_change_or_not.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+
+DIRNAME="$(dirname "${BASH_SOURCE[0]}")"
+
+get_translations_changed_differences_po_file() {
+ lang_code="fr"
+ original_po_file=$(mktemp --suffix original_po.po)
+ original_po_file_cleaned=$(mktemp --suffix original_po_cleaned.po)
+ translation_diff=$(mktemp --suffix translation_diff.po)
+
+ # Clean (remove strings coming from database) the currently committed PO file
+ cp po/${lang_code}.po "${original_po_file}"
+ # This removes messages that only exist in the database-content.py file generated by the update-trans
command
+ perl -0777 -pe "s/#: (?:database-content\.py:[0-9]{1,5}\s*)*(?:\n.+)?\nmsgid ((?:\".*\"\n)*)msgstr
((?:\".*\"\n)*)//g" "${original_po_file}" > "${original_po_file_cleaned}"
+ msgattrib --no-obsolete -o "${original_po_file_cleaned}" "${original_po_file_cleaned}"
+
+ # Call the update-trans command in order to update all the translations
+ "${DIRNAME}/../manage.py" update-trans ${lang_code} > /dev/null
+
+ # Compare code only strings and cleaned PO file contents. If the file is empty, no code string was
updated
+ msgcomm locale/${lang_code}/LC_MESSAGES/code.po "${original_po_file_cleaned}" --sort-output --no-wrap
--less-than=2 | sed '/^#[[:blank:]]\|#$/d' > "${translation_diff}"
+
+ # Replace the modified po file with the original one
+ cp "${original_po_file}" po/${lang_code}.po
+ echo "${translation_diff}"
+}
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
new file mode 100755
index 00000000..732c6144
--- /dev/null
+++ b/.githooks/pre-commit
@@ -0,0 +1,13 @@
+#!/usr/bin/env bash
+
+DIRNAME="$(dirname "${BASH_SOURCE[0]}")"
+
+source "${DIRNAME}/check_messages_did_change_or_not.sh"
+
+echo -e "\e[33mChecking if code strings are updated by this commit…\e[0m"
+translation_diff=$(get_translations_changed_differences_po_file)
+if [[ -s "${translation_diff}" ]]; then
+ echo -e "\e[1m\e[31m\t→ There are some code string changes. Please, open the ${translation_diff} to see
the changes.\e[0m"
+else
+ echo -e "\e[1m\e[32m\t→ No string has been changed in the source code.\e[0m"
+fi
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]