Showing posts with label terminal. Show all posts
Showing posts with label terminal. Show all posts

Saturday, June 17, 2023

Microsoft Terminology SOAP Bash Script

#!/bin/bash
# This script translates terminologies used in MS products by sending a SOAP
# request to api.terminology.microsoft.com/terminology and extracting the result
# MS Language Portal: http://www.microsoft.com/Language/en-US/Search.aspx
# WSDL: http://api.terminology.microsoft.com/Terminology.svc?singleWsdl
# More info: http://www.microsoft.com/Language/de-de/Microsoft-Terminology-API.aspx
# Copyright (C) 2014 Michael Clemens
# This program 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.
#
# This program 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 this program. 
# If not, see http://www.gnu.org/licenses/.#
# usage: ./MSTerminology "STRING TO BE TRANSLATED"

text=$1

from="en-us"
to="de-de"
product="Windows"
version="7"

xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ter=\"http://api.terminology.microsoft.com/terminology\"> \
   <soapenv:Header/> \
   <soapenv:Body> \
      <ter:GetTranslations> \
         <ter:text>${text}</ter:text> \
         <ter:from>${from}</ter:from> \
         <ter:to>${to}</ter:to> \
         <ter:searchOperator>Contains</ter:searchOperator> \
         <ter:sources> \
            <ter:TranslationSource>UiStrings</ter:TranslationSource> \
         </ter:sources> \
         <ter:unique>false</ter:unique> \
         <ter:maxTranslations>1</ter:maxTranslations> \
         <ter:includeDefinitions>true</ter:includeDefinitions> \
         <ter:products> \
            <ter:Product> \
               <ter:Name>${product}</ter:Name> \
               <ter:Versions> \
                  <ter:Version> \
                     <ter:Name>${version}</ter:Name> \
                  </ter:Version> \
               </ter:Versions> \
            </ter:Product> \
         </ter:products> \
      </ter:GetTranslations> \
   </soapenv:Body> \
</soapenv:Envelope>"

output=$(curl -s -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction: \"http://api.terminology.microsoft.com/terminology/Terminology/GetTranslations\"" -d "${xml}" -X POST http://api.terminology.microsoft.com/Terminology.svc)

result=$(echo ${output} | sed -e 's,.*<TranslatedText>\([^<]*\)</TranslatedText>.*,\1,g')

if [[ $result != *s:Envelope* ]]; then
	echo $result
else
	echo "'"$text"' could not be translated. The server's response was:"
	echo $output
fi

Friday, November 20, 2020

Create permanent BASH aliases in Linux

When creating an alias, for example
alias la="ls -la"
it exists until the terminal session is killed. When starting a new terminal window, the alias does not exist any more. The question is, how to create a "permanent" alias, one that exists in every terminal session?
Such aliases can be stored in the ~/.bash_aliases file.
 
That file is loaded by ~/.bashrc. The following lines need to be uncommented or added to enable the use of the ~/.bash_aliases file:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

 
The aliased command will be available on any new terminal. To have the aliased command on any existing terminal one need to source ~/.bashrc from that terminal:
source ~/.bashrc

A one liner would be:
echo "alias la='ls-la'" >> ~/.bash_aliases && source ~/.bash_aliases
 
Another option would be to add the alias line into ~/.bashrc or into ~/.profile / ~/.bash_profile for remote logins. If the command should be executed for all users, put it into /etc/bash.bashrc.
 
The function below can be added to the .bashrc file.
function permalias () 
{ 
  alias "$*";
  echo alias "$*" >> ~/.bash_aliases
}
Then open a new terminal or run source ~/.bashrc in your current terminal. You can now create permanent aliases by using the permalias command, for example permalias cls=clear.

~/.bashrc is run every time you open a new terminal, whereas ~/.bash_profile is not.

In order to update the file, run . ~/.bashrc or source ~/.bashrc

Function from askubuntu.com:

# -----------------------------------
#  Create a new permanent bash alias
#
#  @param $1 - NAME
#  @param $2 - DEFINITION
# -----------------------------------
new-alias () { 
  if [ -z "$1" ]; then
    echo "alias name:" && read NAME
  else
    NAME=$1
  fi

  if alias $NAME 2 > /dev/null > /dev/null; then
    echo "alias $NAME already exists - continue [y/n]?" && read YN
    case $YN in
      [Yy]* ) echo "okay, let's proceed.";;
      [Nn]* ) return;;
      * ) echo "invalid response." && return;;
    esac
  fi

  if [ -z "$2" ]; then
    echo "alias definition:" && read DEFINITION
  else
    DEFINITION="$2"
  fi

  if [ -f ~/.bash_aliases ]; then
    echo "alias $NAME=\"$DEFINITION\"" >> ~/.bash_aliases
  else
    echo "alias $NAME=\"$DEFINITION\"" >> ~/.bashrc
  fi

  alias $NAME="$DEFINITION"
}