MOBILE-2639 ci: Generate the first language index

main
Pau Ferrer Ocaña 2018-09-28 14:21:19 +02:00
parent 2faed183e2
commit 30585a61bd
6 changed files with 2091 additions and 0 deletions

1
.gitignore vendored
View File

@ -40,3 +40,4 @@ e2e/build
!/desktop/electron.js
src/configconstants.ts
src/assets/lang
scripts/local_moodlemobileapp.php

View File

@ -0,0 +1,18 @@
#!/bin/bash
source "functions.sh"
forceLang=$1
print_title 'Getting languages'
git clone https://git.in.moodle.com/moodle/moodle-langpacks.git $LANGPACKSFOLDER
pushd $LANGPACKSFOLDER
git checkout MOODLE_36_STABLE
git pull
popd
if [ -z $forceLang ]; then
php -f moodle_to_json.php
else
php -f moodle_to_json.php "$forceLang"
fi
print_ok 'All done!'

View File

@ -0,0 +1,288 @@
#!/bin/bash
source "functions.sh"
#Saves the key and value on a temporary local_moodlemobileapp.php
function save_local {
val=`echo $value | sed "s/\'/\\\\\'/g"`
echo "\$string['$key'] = '$val';" >> local_moodlemobileapp.php
}
#Saves or updates a key on langindex_old.json
function save_key {
key=$1
found=$2
print_ok "$key=$found"
echo "{\"$key\": \"$found\"}" > langindex_old.json
jq -s '.[0] + .[1]' langindex.json langindex_old.json > langindex_new.json
mv langindex_new.json langindex.json
}
#Removes a key on langindex_old.json
function remove_key {
key=$1
found=$2
print_ok "$key=$found"
echo "{\"$key\": \"$found\"}" > langindex_old.json
jq -s '.[0] - .[1]' langindex.json langindex_old.json > langindex_new.json
mv langindex_new.json langindex.json
}
#Check if and i exists in php file
function exists_in_file {
file=$1
id=$2
file=`echo $file | sed s/^mod_workshop_assessment/workshopform/1`
file=`echo $file | sed s/^mod_assign_/assign/1`
file=`echo $file | sed s/^mod_//1`
completeFile="$LANGPACKSFOLDER/en/$file.php"
if [ -f "$completeFile" ]; then
coincidence=`grep "string\[\'$id\'\]" $completeFile`
if [ ! -z "$coincidence" ]; then
found=$file
return
fi
fi
found=0
}
#Checks if a key exists on the original local_moodlemobileapp.php
function exists_in_mobile {
file='local_moodlemobileapp'
exists_in_file $file $key
if [ $found == 0 ]; then
case $type in
'addon')
mobileid="mma.$component.$plainid"
exists_in_file $file $mobileid
;;
'core')
if [ "$component" == 'moodle' ]; then
mobileid="mm.core.$plainid"
elif [ "$component" == 'mainmenu' ]; then
mobileid="mm.sidemenu.$plainid"
else
mobileid="mm.$component.$plainid"
fi
exists_in_file $file $mobileid
;;
*)
return
esac
fi
if [ $found != 0 ]; then
save_local
fi
}
function do_match {
match=$1
filematch=""
coincidence=`grep "$match" $LANGPACKSFOLDER/en/*.php | wc -l`
if [ $coincidence -eq 1 ]; then
filematch=`grep "$match" $LANGPACKSFOLDER/en/*.php | cut -d'/' -f5 | cut -d'.' -f1`
exists_in_file $filematch $plainid
elif [ $coincidence -gt 0 ] && [ "$#" -gt 1 ]; then
print_message $2
tput setaf 6
grep "$match" $LANGPACKSFOLDER/en/*.php
fi
}
#Find if the id or the value can be found on files to help providing a solution.
function find_matches {
do_match "string\[\'$plainid\'\] = \'$value\'" "Found EXACT match for $key in the following paths"
if [ $coincidence -gt 0 ]; then
case=1
return
fi
do_match " = \'$value\'" "Found some string VALUES for $key in the following paths"
if [ $coincidence -gt 0 ]; then
case=2
return
fi
do_match "string\[\'$plainid\'\]" "Found some string KEYS for $key in the following paths, value $value"
if [ $coincidence -gt 0 ]; then
case=3
return
fi
print_error "No match found for $key adding to local_moodlemobileapp"
save_local
}
function find_single_matches {
do_match "string\[\'$plainid\'\] = \'$value\'"
if [ ! -z $filematch ] && [ $found != 0 ]; then
case=1
return
fi
do_match " = \'$value\'"
if [ ! -z $filematch ] && [ $filematch != 'local_moodlemobileapp' ]; then
case=2
print_message "Found some string VALUES for $key in the following paths $filematch"
tput setaf 6
grep "$match" $LANGPACKSFOLDER/en/*.php
return
fi
do_match "string\[\'$plainid\'\]"
if [ ! -z $filematch ] && [ $found != 0 ]; then
case=3
return
fi
}
#Tries to gues the file where the id will be found.
function guess_file {
key=$1
value=$2
type=`echo $key | cut -d'.' -f1`
component=`echo $key | cut -d'.' -f2`
plainid=`echo $key | cut -d'.' -f3-`
if [ -z "$plainid" ]; then
plainid=$component
component='moodle'
fi
exists_in_file $component $plainid
if [ $found == 0 ]; then
tempid=`echo $plainid | sed s/^mod_//1`
if [ $component == 'moodle' ] && [ "$tempid" != "$plainid" ]; then
exists_in_file $plainid pluginname
if [ $found != 0 ]; then
found=$found/pluginname
fi
fi
fi
# Not found in file, try in local_moodlemobileapp
if [ $found == 0 ]; then
exists_in_mobile
fi
# Still not found, if only found in one file, use it.
if [ $found == 0 ]; then
find_single_matches
fi
# Last fallback.
if [ $found == 0 ]; then
exists_in_file 'moodle' $plainid
fi
if [ $found == 0 ]; then
find_matches
else
save_key $key $found
fi
}
#Finds if there's a better file where to get the id from.
function find_better_file {
key=$1
value=$2
current=$3
type=`echo $key | cut -d'.' -f1`
component=`echo $key | cut -d'.' -f2`
plainid=`echo $key | cut -d'.' -f3-`
if [ -z "$plainid" ]; then
plainid=$component
component='moodle'
fi
currentFile=`echo $current | cut -d'/' -f1`
currentStr=`echo $current | cut -d'/' -f2-`
if [ $currentFile == $current ]; then
currentStr=$plainid
fi
exists_in_file $component $plainid
if [ $found != 0 ] && [ $currentStr == $plainid ]; then
if [ $found != $currentFile ]; then
print_ok "Key '$key' found in component, no need to replace old '$current'"
fi
return
fi
# Still not found, if only found in one file, use it.
if [ $found == 0 ]; then
find_single_matches
fi
if [ $found != 0 ] && [ $found != $currentFile ] && [ $case -lt 3 ]; then
print_message "Indexed string '$key' found in '$found' better than '$current'"
return
fi
if [ $currentFile == 'local_moodlemobileapp' ]; then
exists_in_mobile
else
exists_in_file $currentFile $currentStr
fi
if [ $found == 0 ]; then
print_error "Indexed string '$key' not found on current place '$current'"
fi
}
#Parses the file.
function parse_file {
keys=`jq -r 'keys[]' $1`
for key in $keys; do
# Check if already parsed.
exec="jq -r .\"$key\" langindex.json"
found=`$exec`
exec="jq -r .\"$key\" $1"
value=`$exec`
if [ -z "$found" ] || [ "$found" == 'null' ]; then
guess_file $key "$value"
else
find_better_file "$key" "$value" "$found"
fi
done
}
print_title 'Generating language from code...'
gulp lang
print_title 'Getting languages'
git clone https://git.in.moodle.com/moodle/moodle-langpacks.git $LANGPACKSFOLDER
pushd $LANGPACKSFOLDER
git checkout MOODLE_36_STABLE
git pull
popd
print_title 'Processing file'
#Create langindex.json if not exists.
if [ ! -f 'langindex.json' ]; then
echo "{}" > langindex.json
fi
echo "<?php" > local_moodlemobileapp.php
parse_file '../src/assets/lang/en.json'
echo
jq -S --indent 4 -s '.[0]' langindex.json > langindex_new.json
mv langindex_new.json langindex.json
rm langindex_old.json
print_ok 'All done!'

View File

@ -0,0 +1,41 @@
#!/bin/bash
LANGPACKSFOLDER='../../moodle-langpacks'
function check_success {
if [ $? -ne 0 ]; then
print_error "$1"
elif [ "$#" -gt 1 ]; then
print_ok "$2"
fi
}
function print_success {
if [ $? -ne 0 ]; then
print_message "$1"
$3=0
else
print_ok "$2"
fi
}
function print_error {
tput setaf 1; echo " ERROR: $1"
}
function print_ok {
tput setaf 2; echo " OK: $1"
echo
}
function print_message {
tput setaf 3; echo "-------- $1"
echo
}
function print_title {
stepnumber=$(($stepnumber + 1))
echo
tput setaf 5; echo "$stepnumber $1"
tput setaf 5; echo '=================='
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,152 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Script for getting an specific string
*/
// Check we are in CLI.
if (isset($_SERVER['REMOTE_ADDR'])) {
exit(1);
}
define('MOODLE_INTERNAL', 1);
define('LANGPACKSFOLDER', '../../moodle-langpacks');
define('ASSETSPATH', '../src/assets/lang/');
// Set languages to do. If script is called using a language it will be used as unique.
if (isset($argv[1]) && !empty($argv[1])) {
$languages = explode(',', $argv[1]);
} else {
$languages = array();
$files = scandir(ASSETSPATH);
foreach ($files as $f) {
if (strpos($f, ".json")) {
$languages[] = str_replace(".json", "", $f);
}
}
$languages = array_unique($languages);
}
if (empty($languages)) {
$languages = array('ar', 'bg', 'ca', 'cs', 'da', 'de', 'en', 'es-mx', 'es', 'eu', 'fa', 'fr', 'he', 'hu', 'it', 'ja', 'nl', 'pl', 'pt-br', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh-cn', 'zh-tw');
}
// Process the index file, just once.
$keys = file_get_contents('langindex.json');
$keys = (array) json_decode($keys);
foreach ($keys as $key => $value) {
$map = new StdClass();
if ($value == 'local_moodlemobileapp') {
$map->file = $value;
$map->string = $key;
$exp = explode('.', $key, 3);
$type = $exp[0];
if (count($exp) == 3) {
$component = $exp[1];
$plainid = $exp[2];
} else {
$component = 'moodle';
$plainid = $exp[1];
}
switch($type) {
case 'addon':
$map->string_local = "mma.$component.$plainid";
break;
case 'core':
if ($component == 'moodle') {
$map->string_local = "mm.core.$plainid";
} else if ($component == 'mainmenu') {
$map->string_local = "mm.sidemenu.$plainid";
} else {
$map->string_local = "mm.$component.$plainid";
}
break;
}
} else {
$exp = explode('/', $value, 2);
$map->file = $exp[0];
if (count($exp) == 2) {
$map->string = $exp[1];
} else {
$exp = explode('.', $key, 3);
if (count($exp) == 3) {
$map->string = $exp[2];
} else {
$map->string = $exp[1];
}
}
}
$keys[$key] = $map;
}
$total = count ($keys);
// Process the languages.
foreach ($languages as $lang) {
$translations = [];
$success = 0;
$langfoldername = str_replace('-', '_', $lang);
if (!is_dir(LANGPACKSFOLDER.'/'.$langfoldername)) {
echo "Cannot translate $langfoldername, folder not found";
continue;
}
echo "Processing language $lang";
// Add the translation to the array.
foreach ($keys as $key => $value) {
$file = LANGPACKSFOLDER.'/'.$langfoldername.'/'.$value->file.'.php';
// Apply translations.
if (!file_exists($file)) {
continue;
}
$string = [];
include($file);
if (!isset($string[$value->string])) {
if ($value->file != 'local_moodlemobileapp' || !isset($string[$value->string_local])) {
continue;
}
$text = $string[$value->string_local];
} else {
$text = $string[$value->string];
}
if ($value->file != 'local_moodlemobileapp') {
$text = str_replace('$a->', '$a.', $text);
$text = str_replace('{$a', '{{$a', $text);
$text = str_replace('}', '}}', $text);
// Prevent double.
$text = str_replace(array('{{{', '}}}'), array('{{', '}}'), $text);
}
$translations[$key] = $text;
$success++;
}
// Sort and save.
ksort($translations);
file_put_contents(ASSETSPATH.$lang.'.json', str_replace('\/', '/', json_encode($translations, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)));
$percentage = floor($success/$total *100);
echo " -> Processed $success of $total -> $percentage%\n";
}