How to avoid google translate to translate :parameters - excel

I'm using a library nikaia/translation-sheet who basically pulls all the translations from the Laravel site into a google spreadsheet making it "easily" translatable with =GOOGLETRANSLATE(A1)
The problem comes with the parameters:
:price
:amount
:etc
So I've got the idea to substitute ":" with #nonmakingsenseworblablaprice so Google couldn't translate example:
=SUBSTITUTE(GOOGLETRANSLATE(SUBSTITUTE(B2;":";"#nonmakingsenseworblabla");"ES";"EU");"#nonmakingsenseworblabla";":")
Well, not sure why Google eats some letters and puts new ones:
:amount de saldo -> #nonmakingseseworblatamount of saldo
So I decided to do something like detect the parameter and change :amount to :a_m_o_u_n_t and that is apparently working and not being weirdly parsed converted or translated.
I was looking for a solution and found a similar idea but having problems migrating it to spreadsheets script plus is not detecting the parameter
Any one knows how to detect all :parameters in a sentence and put a symbol, slash, dash etc between the characters or letters? Example:
The amount :amount for this order number :order_id is :price
I've also tried regex but not been lucky so far
=REGEXREPLACE(GOOGLETRANSLATE(REGEXREPLACE(B22; ":(\w)([\w]+)"; "{%$1_$2%}"); "ES"; $C$1); "{%(\w)_([^_]+)%}"; ":$1$2")
There's a regex to select the spaces between letters, but good luck making that in excel or spreadsheets. Demo

Finally I've created a script to avoid parameters translation:
function translate(cell, lang) {
const content = cell.toString();
const keys = [];
const enc = content.replace(/:([\w_]+)/ig, function(m, param) {
const n = `[§${keys.length}]`;
keys.push(param);
return n;
});
return LanguageApp.translate(enc, "es", lang).replace(/\[§(\d+)\]/ig, function(m, param) {
return `:${keys[param]}`;
});

Related

Code about replacing certain words in discord.js

I was trying to make the bot replace multiple words in one sentence with another word.
ex: User will say "Today is a great day"
and the bot shall answer "Today is a bad night"
the words "great" and "day" were replaced by the words "bad" and "night" in this example.
I've been searching in order to find a similar code, but unfortunately all I could find is "word-blacklisting" scripts.
//I tried to do some coding with it but I am not an expert with node.js the code is written really badly. It's not even worth showing really.
The user will say some sentence and the bot will recognize some predetermined words on the sentence and will replace those words with other words I'll decide in the script
We can use String.replace() combined with Regular Expressions to match and replace single words of your choosing.
Consider this example:
function antonyms(string) {
return string
.replace(/(?<![A-Z])terrible(?![A-Z])/gi, 'great')
.replace(/(?<![A-Z])today(?![A-Z])/gi, 'tonight')
.replace(/(?<![A-Z])day(?![A-Z])/gi, 'night');
}
const original = 'Today is a tErRiBlE day.';
console.log(original);
const altered = antonyms(original);
console.log(altered);
const testStr = 'Daylight is approaching.'; // Note that this contains 'day' *within* a word.
const testRes = antonyms(testStr); // The lookarounds in the regex prevent replacement.
console.log(testRes); // If this isn't the desired behavior, you can remove them.

JScript Escape an ampersand in payload data for URL

I am attempting to launch a view using the following JS function:
$('#filterTop').click(function () {
var filterValue = $('#filterValueTop').val();
refreshView(`#Url.Action(Model.Action, Model.Controller)?pageSize=#Model.PageSize&pageNumber=#Model.PageNumber&sortDesc=#Model.SortDescending&filterType=#Model.FilterType&filterValue=${filterValue}&showAll=#Model.ShowAll` + `#Model.Payload`, '#Model.ResultView');
});
It worked great until I needed to append a static payload to the end of the URL. The relevant part is line 3 at the end:
&showAll=#Model.ShowAll` + `#Model.Payload`
I am assigning #Model.Payload a value of:
opts.Payload = "&batchID=" + batchID;
or "&batchID=25". The resulting URL is:
https://localhost:44303/Employee/Repaginate?pageSize=20&pageNumber=1&sortDesc=True&filterType=Name&filterValue=Jes&showAll=False&batchID=25
For some reason, it's translating the "&" to "&a.m.p;" (with no periods) which isn't a valid URL. I've tried various methods of escaping the character like using "%26", "/&", and several other garden varieties but alas, my attempts have been in vain. Any suggestions on what I am doing wrong?

search and replace multiple words in AS3

I've got this code in my AS3 :
var str:String = mySharedObject.data.theDate;
var search:String = "monday";
var replace:String = "";
function strReplace(str:String, search:String, replace:String):String {
return str.split(search).join(replace);
}
Is it possible to tell the code to search for "monday or tuesday or wednsday, or thursday, or friday" and replace them with "_" ?
And, secondly, is it possible to tell the code to search for "January" and replaced it by "01", "February" by "02"...etc ? (in one line if it's possible) ?
Thx
EDIT
I'd like to do something as simple as that :
str.replace( "monday"||"tuesday"||"wednsday" , "" );
Or
var search:String ="tuesday","wednsday","thursday";
But it's not working..
What you want is a regular expression which is implemented in the RegExp class in AS3. The pattern passed into String.replace() can be a RegExp which can be used to do type of matching you want.
A simple regular expression to match English language work days could look like this:
(Monday|Tuesday|Wednesday|Thursday|Friday)
which is Regular Expression for "match Monday or Tuesday or Wednesday or Thursday or Friday". Note that this almost certainly isn't the best regex for this but it's simple and it works. There's a link where you can learn more about regexes at the bottom of this answer.
This regex can be put into a function that takes the string to search, the string to replace matches with and a string called flag. flag is a list of options that modify how the regex works. The docs for the RegExp construction provide complete details. This function might look like:
private function replaceDay(str:String, replace:String, flag:String = ''):String
{
var search:RegExp = new RegExp("(Monday|Tuesday|Wednesday|Thursday|Friday)", flag);
return str.replace(search, replace);
}
You can then test it out (I've run it all in Flash Builder successfully):
// Replace a single, normally capitalized day name
var monday:String = "Monday the 1st";
trace(replaceDay(monday, "REDACTED")); // REDACTED the 1st
// Replace multiple normally capitalized day names
var mondayAndTuesday:String = "Either Monday or Tuesday works for me";
trace(replaceDay(mondayAndTuesday, "", "g")); // Either or works for me
// Replace a single day name regardless of capitalization
var lowerCaseWednesday:String = "wednesday";
trace(replaceDay(lowerCaseWednesday, "yadsendew", "i")); // yadsendew
var weirdCaseThursday:String = "tHurSdaY";
trace(replaceDay(weirdCaseThursday, "It's actually 'Thursday'.", "i")); // It's actually 'Thursday'.
// Replace multiple day names regardless of capitalization
var friday:String = "FriDAY FRIDAY friday";
trace(replaceDay(friday, "Something", "ig")); // Something Something Something
As for your question about replacing month names with numbers that is possible as well. The last example in the docs for String.replace() shows how to use a function as the replace value. Since StackOverflow prefers one post, one question you should remove it from this post and post another question.
For more info on regular expressions check out http://www.regular-expressions.info/

Gradle: How to filter and search through text?

I'm fairly new to gradle. How do I filter text in the following manner?
Pretend that the output/result I want to filter will be the two URLs below.
"http://localhost/artifactory/appNameIwant/moreStuffHereThatsDynamic"
> I want this URL
"http://localhost/artifactory/differentAppName"
> I don't want this URL
I want to put up a "match" variable that would be something like
variable = http://localhost/artifactory/appnameIwant
So essentially, the string will not be a perfect match. I want it to filter and provide back any URLs that start with the variable listed above. It cannot be a perfect match as the characters after the /appnameIwant/ will be changing.
I want to use a for loop to cycle through an array, with an if then statement to return any matches. For instance.
for (i=0; i < results.length; i++){
if (results[i] strings matches (http://localhost/artifactory/appnameIwant) {
return results[i] }
I am just filtering the URL strings themselves, not anything complicated inside the webpages.
Let me know if further explanation would be helpful.
Thanks so much for your time and help!
I figured it out - I just used
if (string.startsWith"texthere")) {println string}
A lot easier than I thought!

Autocomplete function with underscore/JS

what is the proper way to implement an autocomplete search with undescore?
i have a simple array (cities) and a text input field ($.autocomplete). when the user enters the first letters in the auto-complete textfield, it should output an array with all the cities starting with the entered letters (term).
cities:
["Graz","Hamburg","Innsbruck","Linz","München","Other","Salzburg","Wien"]
eventlistener:
$.autocomplete.addEventListener("change", function(e){
var cities = cities_array;
var term = $.autocomplete.value;
var results = _.filter(cities, function (city){
return
});
console.log(results + "this is results");
});
I’ve tried it with _.contains, but it only returns the city when its a complete match (e.g Graz is only output when „Graz“ is entered but not when „Gr“ is entered).
the _.filter/._select collection at http://underscorejs.org/docs/underscore.html are not very clear for me and the closest i found here is filtering JSON using underscore.
but i don’t understand the indexOf part.
thx for any suggestions!
By using #filter and #indexOf, you can get quite close to a pretty decent autocomplete.
What #indexOf does is that it checks the string if it contains the inputVal.
If it does not contain it it'll return -1 therefore our predicate below will work without fail.
Another small trick here is that you (read I) wanted it to be possible to search for s and get a hit for Innsbruck and Salzburg alike therefore I threw in #toLowerCase so that you always search in lower case.
return _.filter(cities, function(city) {
return city.toLowerCase().indexOf(inputVal.toLowerCase()) >= 0;
});

Resources