Nitesh « Intelligrape Groovy & Grails Blogs
Subscribe via E-Mail:

Nitesh

Posts by Nitesh:

  • Bash script to open a terminal with multiple tabs on Linux start-up

    25 May 2012 in Linux

    All of the tasks performed by us after system startup such as opening a browser, MySql etc can be automated by following the steps mentioned below :

    1. Write a script:-

    sleep 1m;gnome-terminal --geometry=150x50 --tab --title="echo" -e "bash -c \"echo "hello";echo "there";exec bash\"" --tab --title="idea" -e "bash -c \"/opt/idea-IU-111.69/bin/idea.sh;exec bash\"" --tab --title="sql" -e "bash -c \"mysql -uroot -pigdefault;\"" --tab --title="firefox" -e "bash -c \"/usr/bin/firefox www.gmail.com;\""
    

    The script above will open a terminal with 4 tabs :

    1. echo “hello there”
    2. Idea
    3. MySql
    4. Firefox

    The script will also set the titles in the terminal for each tab.

    One can personalize the above script accordingly.

    Description of the script :-

    sleep 1m                       : Executes the script after 1 minute so that system finishes its startup process.
    gnome-terminal             : Open a terminal.
    –geometry=150×50       : Set screen size for terminal.
    –tab                               : Open new tab
    –title                              : Set title for the terminal
    -e                                   : Execute the argument inside the terminal.
    exec bash                      : Starts a new bash after executing all the commands. This command is required if you do not want to close the current tab .

    2. Save the script anywhere in the file system and make the file executable using the following command:

    $chmod +x file.sh
    

    3. Add this file to startup applications :-

    Go to  System > Preferences > Startup Applications

    Click on add  and in command write-

    bash path/to/your/file.sh
    

    Close and its done!

    Next time when you start your system, all the tasks mentioned in the script will be automated.

    Hope this helped!

    Nitesh Goel
    [Intelligrape Software Pvt. Ltd.]

  • Change user preferred locale using spring’s SessionLocaleResolver

    11 May 2012 in Grails

    Here we are going to talk about a scenario where there are some records in database which are specific to locale and they need to be displayed as per user’s current locale at number of places.Also user can change its preferred locale.

    For this I preferred to set the user’s locale in the session object. There are two ways to do this :-

    1. Add attribute “lang” to the request and it will be available to session automatically.

    For example: whenever user clicks on the link, the language will change to English and the user will remain on the same page.

    
    <g:link controller="${params.controller}" action="${params.action}" params="${params+[lang:'en']}">English</g:link>
    /*Add different languages here*/
    

    2.Also set default locale in the session in case, it is not set.

    For that add following to any Filters :-

    
    if (!session.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE') {
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    localeResolver.setLocale(request, response, new Locale('en'));
    
    }
    
    

    This sets the locale in spring’s SessionLocaleResolver class. Session always refer for the locale from here only.

    Now one can check the locale wherever session is available. For example in GSPs:

    
    <g:if test="${session.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'.toString()=='en'}">
    
    

    Note: This implementation is useful to set locale in session (for accessing many times). Otherwise locale is available directly from current request as given below:-

    import org.springframework.web.servlet.support.RequestContextUtils as RCU
    Locale locale=RCU.getLocale(request)
    

    Hope this helped!
    Nitesh Goel
    [Intelligrape Software Pvt. Ltd.]

    RequestContextUtils