From 6c9dd8deb682ceccb8c5422fae1c161e6e77ace0 Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 13 Jun 2021 05:28:00 -0700 Subject: [PATCH 1/5] Change internal i18n key of sv-SE from sv to sv-SE --- src/invidious/helpers/i18n.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/invidious/helpers/i18n.cr b/src/invidious/helpers/i18n.cr index 45a3f1ae..dd46feab 100644 --- a/src/invidious/helpers/i18n.cr +++ b/src/invidious/helpers/i18n.cr @@ -21,7 +21,7 @@ LOCALES = { "pt-PT" => load_locale("pt-PT"), "ro" => load_locale("ro"), "ru" => load_locale("ru"), - "sv" => load_locale("sv-SE"), + "sv-SE" => load_locale("sv-SE"), "tr" => load_locale("tr"), "uk" => load_locale("uk"), "zh-CN" => load_locale("zh-CN"), From c85c6d0ac5e3961ee574222489147089b1b0e2ee Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 13 Jun 2021 05:44:06 -0700 Subject: [PATCH 2/5] Add new line at end of locale files --- locales/el.json | 2 +- locales/eu.json | 2 +- locales/fa.json | 2 +- locales/fi.json | 2 +- locales/is.json | 2 +- locales/pt-PT.json | 2 +- locales/si.json | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/locales/el.json b/locales/el.json index 26e7fcaa..c55db8ef 100644 --- a/locales/el.json +++ b/locales/el.json @@ -416,4 +416,4 @@ "hdr": "", "filter": "", "Current version: ": "Τρέχουσα έκδοση: " -} \ No newline at end of file +} diff --git a/locales/eu.json b/locales/eu.json index 8381f496..ff1c67b7 100644 --- a/locales/eu.json +++ b/locales/eu.json @@ -338,4 +338,4 @@ "Playlists": "", "Community": "", "Current version: ": "" -} \ No newline at end of file +} diff --git a/locales/fa.json b/locales/fa.json index 5e540b7d..f8c33b8f 100644 --- a/locales/fa.json +++ b/locales/fa.json @@ -416,4 +416,4 @@ "hdr": "", "filter": "", "Current version: ": "نسخه فعلی: " -} \ No newline at end of file +} diff --git a/locales/fi.json b/locales/fi.json index 62e96639..2092e994 100644 --- a/locales/fi.json +++ b/locales/fi.json @@ -416,4 +416,4 @@ "hdr": "", "filter": "", "Current version: ": "Tämänhetkinen versio: " -} \ No newline at end of file +} diff --git a/locales/is.json b/locales/is.json index 00d50ad1..a847080a 100644 --- a/locales/is.json +++ b/locales/is.json @@ -416,4 +416,4 @@ "hdr": "", "filter": "", "Current version: ": "Núverandi útgáfa: " -} \ No newline at end of file +} diff --git a/locales/pt-PT.json b/locales/pt-PT.json index a8569f18..43ffc7d8 100644 --- a/locales/pt-PT.json +++ b/locales/pt-PT.json @@ -416,4 +416,4 @@ "hdr": "", "filter": "", "Current version: ": "Versão atual: " -} \ No newline at end of file +} diff --git a/locales/si.json b/locales/si.json index a9889672..57ed22a3 100644 --- a/locales/si.json +++ b/locales/si.json @@ -416,4 +416,4 @@ "hdr": "", "filter": "", "Current version: ": "" -} \ No newline at end of file +} From 2e6adfb44a1cb091e10a95f3e4ad2d01a2820b1e Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 13 Jun 2021 06:40:04 -0700 Subject: [PATCH 3/5] Add locale-key-propagater and auto linter hooks --- scripts/git/pre-commit | 23 +++++++++ scripts/propagate-new-locale-keys.cr | 77 ++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 scripts/git/pre-commit create mode 100644 scripts/propagate-new-locale-keys.cr diff --git a/scripts/git/pre-commit b/scripts/git/pre-commit new file mode 100644 index 00000000..e4a27750 --- /dev/null +++ b/scripts/git/pre-commit @@ -0,0 +1,23 @@ +# Useful precomit hooks +# Please see https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks for instructions on installation. + +# Crystal linter +# This is a modified version of the pre-commit hook from the crystal repo. https://github.com/crystal-lang/crystal/blob/master/scripts/git/pre-commit +# Please refer to that if you'd like an version that doesn't automatically format staged files. +changed_cr_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cr$') +if [ ! -z "$changed_cr_files" ]; then + if [ -x bin/crystal ]; then + # use bin/crystal wrapper when available to run local compiler build + bin/crystal tool format $changed_cr_files >&2 + else + crystal tool format $changed_cr_files >&2 + fi + + git add $changed_cr_files +fi + +# Locale equalizer +if [ ! -z $(git diff --name-only --cached -- locales/) ]; then + crystal run scripts/propagate-new-locale-keys.cr + git add locales > /dev/null +fi \ No newline at end of file diff --git a/scripts/propagate-new-locale-keys.cr b/scripts/propagate-new-locale-keys.cr new file mode 100644 index 00000000..8c58d3f9 --- /dev/null +++ b/scripts/propagate-new-locale-keys.cr @@ -0,0 +1,77 @@ +require "json" +require "../src/invidious/helpers/i18n.cr" + +def locale_to_array(locale_name) + arrayifed_locale_data = [] of Tuple(String, JSON::Any | String) + keys_only_array = [] of String + LOCALES[locale_name].each do |k, v| + if v.as_h? + arrayifed_locale_data << {k, JSON.parse(v.as_h.to_json)} + elsif v.as_s? + arrayifed_locale_data << {k, v.as_s} + end + + keys_only_array << k + end + + return arrayifed_locale_data, keys_only_array +end + +# Invidious currently had some unloaded localization files. We shouldn't need to propagate new keys onto those. +# We'll also remove the reference locale (english) from the list to process. +loaded_locales = LOCALES.keys.select! { |key| key != "en-US" } +english_locale = locale_to_array("en-US")[0] + +# In order to automatically propagate locale keys we're going to be needing two arrays. +# One is an array containing each locale data encoded as tuples. The other would contain +# sets of only the keys of each locale files. +# +# The second file is to make sure that an key from the english reference file is present +# in whatever the current locale we're scanning is. +locale_list = [] of Array(Tuple(String, JSON::Any | String)) +locale_list_with_only_keys = [] of Array(String) + +# Populates the created arrays from above +loaded_locales.each do |name| + arrayifed_locale_data, keys_only_locale = locale_to_array(name) + + locale_list << arrayifed_locale_data + locale_list_with_only_keys << keys_only_locale +end + +locale_list_with_only_keys.each_with_index do |keys_of_locale_in_processing, index_of_locale_in_processing| + insert_at = {} of Int32 => Tuple(String, JSON::Any | String) + + LOCALES["en-US"].each_with_index do |ref_locale_data, ref_locale_key_index| + ref_locale_key, ref_locale_value = ref_locale_data + + # Found an new key that isn't present in the current locale being processed. + if !keys_of_locale_in_processing.includes? ref_locale_key + # In terms of structure there's currently only two types; one for plural and the other for singular translations. + if ref_locale_value.as_h? + insert_at[ref_locale_key_index] = {ref_locale_key, JSON.parse({"([^.,0-9]|^)1([^.,0-9]|$)" => "", "" => ""}.to_json)} + else + insert_at[ref_locale_key_index] = {ref_locale_key, ""} + end + end + end + + insert_at.each do |location_to_insert, data| + locale_list[index_of_locale_in_processing].insert(location_to_insert, data) + end +end + +final_locale_list = [] of String +# Now we convert back to how original formats +locale_list.each do |locale| + intermediate_hash = {} of String => (JSON::Any | String) + locale.each { |k, v| intermediate_hash[k] = v } + final_locale_list << intermediate_hash.to_pretty_json(indent = " ") +end + +# Map locale name to locale contents +locale_map = Hash.zip(loaded_locales, final_locale_list) + +locale_map.each do |locale_name, locale_contents| + File.write("locales/#{locale_name}.json", "#{locale_contents}\n") +end From 4eb3de7b4e5f4fa27d4e61da247da258dfc1699e Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 13 Jun 2021 06:56:17 -0700 Subject: [PATCH 4/5] Fix typos --- scripts/propagate-new-locale-keys.cr | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/propagate-new-locale-keys.cr b/scripts/propagate-new-locale-keys.cr index 8c58d3f9..39910c6e 100644 --- a/scripts/propagate-new-locale-keys.cr +++ b/scripts/propagate-new-locale-keys.cr @@ -17,7 +17,7 @@ def locale_to_array(locale_name) return arrayifed_locale_data, keys_only_array end -# Invidious currently had some unloaded localization files. We shouldn't need to propagate new keys onto those. +# Invidious currently has some unloaded localization files. We shouldn't need to propagate new keys onto those. # We'll also remove the reference locale (english) from the list to process. loaded_locales = LOCALES.keys.select! { |key| key != "en-US" } english_locale = locale_to_array("en-US")[0] @@ -26,7 +26,7 @@ english_locale = locale_to_array("en-US")[0] # One is an array containing each locale data encoded as tuples. The other would contain # sets of only the keys of each locale files. # -# The second file is to make sure that an key from the english reference file is present +# The second array is to make sure that an key from the english reference file is present # in whatever the current locale we're scanning is. locale_list = [] of Array(Tuple(String, JSON::Any | String)) locale_list_with_only_keys = [] of Array(String) @@ -45,7 +45,7 @@ locale_list_with_only_keys.each_with_index do |keys_of_locale_in_processing, ind LOCALES["en-US"].each_with_index do |ref_locale_data, ref_locale_key_index| ref_locale_key, ref_locale_value = ref_locale_data - # Found an new key that isn't present in the current locale being processed. + # Found an new key that isn't present in the current locale.. if !keys_of_locale_in_processing.includes? ref_locale_key # In terms of structure there's currently only two types; one for plural and the other for singular translations. if ref_locale_value.as_h? @@ -61,17 +61,17 @@ locale_list_with_only_keys.each_with_index do |keys_of_locale_in_processing, ind end end +# Now we convert back to our original format. final_locale_list = [] of String -# Now we convert back to how original formats locale_list.each do |locale| intermediate_hash = {} of String => (JSON::Any | String) locale.each { |k, v| intermediate_hash[k] = v } final_locale_list << intermediate_hash.to_pretty_json(indent = " ") end -# Map locale name to locale contents locale_map = Hash.zip(loaded_locales, final_locale_list) +# Export locale_map.each do |locale_name, locale_contents| File.write("locales/#{locale_name}.json", "#{locale_contents}\n") end From d4327329596c7d07392896c61fb3aebf4acb7216 Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 13 Jun 2021 07:23:45 -0700 Subject: [PATCH 5/5] Add ability to propagate locale removals --- scripts/propagate-new-locale-keys.cr | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/scripts/propagate-new-locale-keys.cr b/scripts/propagate-new-locale-keys.cr index 39910c6e..570b408a 100644 --- a/scripts/propagate-new-locale-keys.cr +++ b/scripts/propagate-new-locale-keys.cr @@ -20,7 +20,7 @@ end # Invidious currently has some unloaded localization files. We shouldn't need to propagate new keys onto those. # We'll also remove the reference locale (english) from the list to process. loaded_locales = LOCALES.keys.select! { |key| key != "en-US" } -english_locale = locale_to_array("en-US")[0] +english_locale, english_locale_keys = locale_to_array("en-US") # In order to automatically propagate locale keys we're going to be needing two arrays. # One is an array containing each locale data encoded as tuples. The other would contain @@ -39,7 +39,8 @@ loaded_locales.each do |name| locale_list_with_only_keys << keys_only_locale end -locale_list_with_only_keys.each_with_index do |keys_of_locale_in_processing, index_of_locale_in_processing| +# Propagate additions +locale_list_with_only_keys.dup.each_with_index do |keys_of_locale_in_processing, index_of_locale_in_processing| insert_at = {} of Int32 => Tuple(String, JSON::Any | String) LOCALES["en-US"].each_with_index do |ref_locale_data, ref_locale_key_index| @@ -57,10 +58,27 @@ locale_list_with_only_keys.each_with_index do |keys_of_locale_in_processing, ind end insert_at.each do |location_to_insert, data| + locale_list_with_only_keys[index_of_locale_in_processing].insert(location_to_insert, data[0]) locale_list[index_of_locale_in_processing].insert(location_to_insert, data) end end +# Propagate removals +locale_list_with_only_keys.dup.each_with_index do |keys_of_locale_in_processing, index_of_locale_in_processing| + remove_at = [] of Int32 + + keys_of_locale_in_processing.each_with_index do |current_key, current_key_index| + if !english_locale_keys.includes? current_key + remove_at << current_key_index + end + end + + remove_at.each do |index_to_remove_at| + locale_list_with_only_keys[index_of_locale_in_processing].delete_at(index_to_remove_at) + locale_list[index_of_locale_in_processing].delete_at(index_to_remove_at) + end +end + # Now we convert back to our original format. final_locale_list = [] of String locale_list.each do |locale|