Category talk:Multilingual tags: Locations

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Bash script for creating location templates from wikipedia interwikis[edit]

Here's a bash script for creating location templates from wikipedia interwiki links. In short, it converts interwikis like

  • [[en:Agrinio]] to template lines as
  • |en=[[:en:Agrinio|Agrinio]] and adds the rest of the template text.


#!/bin/bash
#By Badseed, distributed under the WTF Public License :P

#prompts user for input
echo "Enter name of file with interwikis:"
read INPUTFILE
echo "Enter name of output file:"
read OUTFILE

#protects against forgetting to include the english interwiki
grep -q en: $INPUTFILE
return_val=$?
if [ $return_val != 0 ] 
	then
	echo "there's no en interwiki... please, type it in:"
	read ENINTERW
	echo $ENINTERW>>$INPUTFILE
fi

#adds the template switch "header" and creates outfile
echo "{{LangSwitch
|lang={{#if:{{{1|}}}|{{{1}}}|{{int:Lang}}}}">>$OUTFILE

# loops reading lines from infile
while read line						
  do
	interwiki="$line"			
	separ=`expr index "$interwiki" :`		#finds position of separator ":" between lang ISO code and area name, not all ISOs are 2-char
	lang=${interwiki:2:separ-3}			#isolates the ISO code, by starting at index 2 and ending at separ minus 3 for [[ and :
	len=${#interwiki}					
	clean=${interwiki:2:len-4}			#strips brackets for central component
	loc=${interwiki:separ}				#rightmost location component
	new="|"$lang"=[[:"$clean"|"$loc			#constructs the final text string using previous components and |, [[, etc.
	echo  "$new">>$OUTFILE				
  done <$INPUTFILE

#prepares location's category name using the english language interwiki
engl=`grep en: $INPUTFILE`
lenengl=${#engl}
catname=${engl:5:lenengl-7}

#closes template at end and adds doc, please check the name of location's category before saving
echo "}}<noinclude>
{{Documentation}}

[[Category:Multilingual tags: Locations|{{PAGENAME}}]]
[[Category:$catname|~{{PAGENAME}}]]
[[Category:Internationalization templates using LangSwitch|{{PAGENAME}}]]
</noinclude>">>$OUTFILE

#adds documentation subpage text in the outfile, remember not to keep this part in the template page; put in the doc. subpage
echo "
{{TemplateBox
|1=1
|1d=<nowiki>{{$catname|xx}}</nowiki> (where xx is ISO 639-2 language code) for output in the specified language
|1stat=optional
|name=$catname
|desc=Multilingual tag for use in the Location field of {{m|Painting}}.
|namespace=all
|usergroup=all
|usage-notes=
|type=
|example=
|i18n-method=switch
|i18n-desc=
|seealso=
|setscats=
|lines=one
|shorthand=
}}

==Examples==
:<nowiki>{{$catname}}</nowiki> - Will display the name in the language specified by user's preferences: {{$catname}}
:<nowiki>{{$catname|ru}}</nowiki> - Will always display the name in Russian: {{$catname|ru}}">>$OUTFILE

#if you're lazy like me uncomment the following to get the text directly in the terminal
#cat $OUTFILE


How to use:

  • Copy the list of interwiki links for the location from a wikipedia (English is your best bet as they will be most complete, do not forget to add the i/w for the originating 'pedia's lang), paste to a text editor and save.
  • Run the script, you will be prompted for input & output file names, as well as for inserting the en interwiki in case you forgot (change iso code in grep ln15 for other languages)
  • After execution, the text in the output file is a complete template by the current standard and ready for pasting in the wiki. The location's category is also inserted, provided that the english article's name is the same with the category's name in Commons (please check before saving). Remember also not to save the documentation subpage text with the template but cut it and paste in the docu subpage.

As I am relatively new to bash, please don't bash my head in if the scipt looks naive to an experienced developer: it "just works" - Badseed talk 22:53, 17 January 2010 (UTC)[reply]