mirror of
https://github.com/iv-org/invidious.git
synced 2024-12-16 03:04:36 -05:00
Add locale-key-propagater and auto linter hooks
This commit is contained in:
parent
c85c6d0ac5
commit
2e6adfb44a
23
scripts/git/pre-commit
Normal file
23
scripts/git/pre-commit
Normal file
@ -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
|
77
scripts/propagate-new-locale-keys.cr
Normal file
77
scripts/propagate-new-locale-keys.cr
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user