Skip to content

Instantly share code, notes, and snippets.

@Caio99BR
Last active March 10, 2018 18:38
Show Gist options
  • Save Caio99BR/8d7017736f807460e6b3f3a230d2e39d to your computer and use it in GitHub Desktop.
Save Caio99BR/8d7017736f807460e6b3f3a230d2e39d to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Copyright 2018 Caio99BR
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Warn about missing editreplacematch function
if [ -z "$(type -t editreplacematch 2> /dev/null)" ]; then
echo '';
echo ' sync_index.sh: The function "editreplacematch" from android_development_shell_tools is not found, downloading it!';
echo '';
source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/sources/host/edit.rc)
fi;
# === Sync Lang File Automatically ===
function sync_langsh()
{
# Usage: sync_langsh (Check in all lang files with en-US base)
# Error about missing editreplacematch function
if [ -z "$(type -t editreplacematch 2> /dev/null)" ]; then
echo '';
echo ' sync_langsh: The function "editreplacematch" from android_development_shell_tools not found it, exiting!';
echo '';
return;
fi;
# Variable
local base_file;
local check_file;
local cwd;
local base_list;
local lang_list=();
local lang_list_size;
local temp_search;
local tmp_file;
export PETT_LAST_LINE;
# Current dir
cwd=$(pwd);
# Temporary file
tmp_file=$(mktemp);
# Check if we're inside of files/ dir
if [ ! -d "${cwd}/index" ]; then
echo '';
echo ' sync_indexsh: "index" folder not found, exiting!';
echo '';
return;
fi;
# Current Language List (Code|Name)
lang_list=('ar' \
'ca' \
'de-DE' \
'es-ES' \
'es-419' \
'fr-FR' \
'hi' \
'it-IT' \
'nl-NL' \
'pl' \
'pt-BR' \
'ru' \
'tr');
# Base Language List (Code|Name)
base_list='en-US';
# Get current size
lang_list_size="${#lang_list[@]}";
# Start working
for ((i=0; i<lang_list_size; i++)); do
# Match List
base_file="js/api/lang/${base_list}/messages.js";
# To Write List
check_file="js/api/lang/${lang_list[$i]}/messages.js";
# Remove comments
grep -v '//' "${base_file}" > "${tmp_file}";
echo -e "\nParsing file ${lang_list[$i]}";
# Check for the strings on a entire file
while read line_search; do
temp_search=$(echo "${line_search}" | cut -d' ' -f2 | cut -d'=' -f1)
echo -ne "\r\033[K ${temp_search}"
# Check if line from en-US is on file to check
grep -q "${temp_search}" "${check_file}";
# Output the message of not found string
if [[ "${?}" != '0' ]]; then
echo " String ${temp_search} not found!\n";
editinsertbelow "${PETT_LAST_LINE}" "${line_search}" "${check_file}";
fi
export PETT_LAST_LINE="${temp_search}";
done < "${tmp_file}";
echo;
done;
}
# Execute it!
sync_langsh;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment