Monday, November 22, 2010

Best Practice Localization/Internationalization Xcode iPhone project - NIB files

This article describes how to setup localization on your NIB files (.xib files).

I wrote another article about how to setup localization for your Strings in the class files (.m files). I recommend reading the other article to get a better understanding of the whole localization process of iPhone apps with Xcode.

Create localized NIB files

First you should bring the according file into a status that can be called complete. You can afterwards still change the file but it makes sense to edit the other language views only once aat the end.

To create the localizations perform the following steps:
1. Right-click on the according .xib file in the Resource folder and select "Get Info".
2. In the window under "General" click "Make file Localizable".
3. Then again under "General" repeat "Add Localization" for all languages you want to support.

The result in the Groups & Files view of Xcode should look like below.


Change the text of labels, buttons etc. for the according language of the .NIB file


Now that you have all the language versions under the NIB file, you can open each separately and change button and label text for the language.
Now Build and Run you App on the iPhone simulator. When you change the language of the iPhone to one of the languages provided in your app the buttons and labels will show the right language.

Troubleshooting: I changed the language in the iPhone but the old NIB file content is displayed


Try to:

1. Clean the Target.
2. Remove the app from the iPhone emulator. (Hold left mouse button down on any app icon until the icons wiggle. Click x for delete. Click iPhone Home button.)
3. Build and Run again.

Sometimes deleting the build folder could also help.

Best Practice Localization/Internationalization Xcode iPhone project - Localizable.strings

This article is about how to create a Localizable.strings file for an Xcode iPhone project, in order to provide translations for Strings that were used within the class files. Also common problems that can accure when dealing with internationalization of an xcode project are discussed.

I also wrote an article about how to add localization for .xib files (NIB-files).

1. Create Localizable.strings files for all supported languages


1. In any xcode iPhone project right-click on the Resources folder and select "Add->New File...". 2. Under the available iPhone templates is no .strings file available so we select "Mac OS X -> Resource -> Strings File" and click Next.
3. Name the file Localizable.strings.
4. Right-click on the file in the Resource folder and select "Get Info".
5. Under "General" click "Make file Localizable".
6. Then again under "General" repeat "Add Localization" for all languages you want to support.

The result for the localized Localizable.strings file in the Groups & Files view of Xcode should look like this:



2. Change plain Strings in class files to the NSLocalizedString macro


Everywhere in the .m files where you are using NSStrings (e.g. @"text";) you need to replace the part with NSLocalizedString(@"textKey", @"comment");.

If you don't want to fill in a comment with your key leave the according filed nil.
The textKey will later be looked up in the according strings file for the language of the iPhone user and replaced with the translated text.

Example entry in a Localizable.strings file:
/* comment */
"exitLabel" = "Exit";

3. Automatically extracting the key's into the Localizable.strings files


Often you can find on the Web that you should create an en.lproj, de.lproj, fr.lproj folder etc. to hold the Localizable.strings files. When we perform our first step Xcode generates English.lproj, German.lproj, French.lproj folders and places the Localizable.strings files in.

That is for newer versions of Xcode, in my case Xcode 3.2.4.

To extract all the keys used in your .m-files run the below command that will write the keys with the according command in the according Localizable.strings files. You have to run the command in the command line of your console and you have to be in the according xcode project.

genstrings -o English.lproj *.m
genstrings -o German.lproj *.m
genstrings -o French.lproj *.m

Important! This command will override the old entries. Be aware and save the old file.

Now you are good to go and when you Build and Run your app the changes should be applied.

Problem: The Localizable.strings file contains inverted question marks


This can happen when the according file is not in UTF-16. You can solve this problem in Xcode though. Right-click on the file and select "Get Info". There select File Encoding UTF-16, when a popup asks you: "Do you want to convert the file to the new encoding or reinterpret it using the new encoding?" Select "Convert". That's it.

Note: Sometimes I have to run the genstrings commands again in order that the encoding works, after I changed it.

Problem: NSLocalizedString only returns the key


Try to:

1. Make sure you got the case right for each character in the key.
2. Clean the Target.
3. Remove the app from the iPhone emulator. (Hold left mouse button down on any app icon until the icons wiggle. Click x for delete. Click iPhone Home button.)
4. Build and Run again.

Sometimes deleting the build folder could also help.

Wednesday, November 17, 2010

Hattrick Online Soccer Manager Match Viewer App for iPhone and Android

Currently I am working on an iPhone App for Hattrick. It will be a match viewer for Hattrick.

The project name is SmartViewer and this is the website for the Hattrick Match Viewer Mobile App. Here you can get the updates about the release dates and available versions.

The first free version will be released early next year (2011).
Shortly after that an Android App will be released.

Some of the features are:
  • direct link to the users match
  • via favorites also a direct link to matches of other users
  • the user is able to create conferences to follow several games in parallel
  • during the match information about crowd, weather, goals, cards, substitutions, injuries, special events and ball possession will be provided
  • after the game in addition the ratings and maybe some special stats, like hatstats, will be provided

Monday, November 15, 2010

Install and run SVN server on Linux with browser based administration GUI

This article should describe how to install and setup a SVN server on Linux Debian Lenny. The instructions may also work for other Linux distributions like Ubuntu, Fedora, RedHat and Open Suse.

1. Install the packages necessary for the SVN server

apt-get install php-pear libapache2-svn subversion
pear install -f -o VersionControl_SVN
Setup the repository root directory.
mkdir -p /var/lib/svn/repos
touch /var/lib/svn/htpasswd
touch /var/lib/svn/accessfile
chown -R www-data:www-data /var/lib/svn

2. Configurate the dav_svn module in Apache2

nano /etc/apache2/mods-available/dav_svn.conf
Add the following lines or make sure they are not commented (no leading #). There might be more content between the location tags but below are the values that have to be there with those values.
<Location /svn>
  DAV svn
  SVNParentPath /var/lib/svn/repos
  AuthType Basic
  AuthName “Subversion Repository”
  AuthUserFile /var/lib/svn/htpasswd
  # SSLRequireSSL
  AuthzSVNAccessFile /var/lib/svn/accessfile

  Require valid-user

</Location>

3. Download SVNManager and setup


Change into the folder for the apache server pages. By default that is /var/www.
cd /var/www
Create the folder for the svnmanager and download the svnmanager file there. Check on svnmanager.org for the current version and right click on the download link and copy the link address.
mkdir svnmanger
wget http://prdownloads.sourceforge.net/svnmanager/svnmanager-1.08.tar.gz
After the download is done extract the archive and copy the files into the "nice folder".
tar xvfz svnmanager-1.xx.tar.gz
cp -R svnmanager-1.xx/* /var/www/svnmanager
Enable the default linux config file.
cd /var/www/svnmanager
cp config.php.linux config.php
Enable the dav_svn module for apache2.
a2enmod dav_svn
invoke-rc.d apache2 restart

4. Edit the config.php

nano config.php
Change the lines to following values.
<?php
//Shell command’s
$htpassword_cmd = “/usr/bin/htpasswd”;
$svn_cmd = “/usr/bin/svn”;
$svnadmin_cmd = “/usr/bin/svnadmin”;

//Subversion
$svn_repos_loc = “/var/lib/svn/repos”;
$svn_passwd_file= “/var/lib/svn/htpasswd”;
$svn_access_file= “/var/lib/svn/accessfile”;

//SMTP Server
$smtp_server = “localhost”;

//Database
$dsn =”mysqli://svnmanager:password@localhost/svnmanager”;

//Administrator account
$admin_name=”admin”;
$admin_temp_password=”admin”;

5. Create Databases and User Account


If you have phpmyadmin installed (e.g. via ttp://your-ip/phpmyadmin) you can create a new database there called svnmanager. Also create a user (e.g. svnuser) with password and grant select, insert, update, delete, create, drop, alter rights.

If you don't have phpmyadmin run following commands:
mysql -u root -p -e”CREATE DATABASE svnmanager; \
GRANT select, insert, update, delete, create, drop, alter on svnmanager.* to ’svnmanager’@'localhost’ identified by ‘password’; \
FLUSH PRIVILEGES;

6. Create admin user in SVN Manager and create repository and grant access


With the Administrator account configured in the config.php login to your svnmanager at http://your-IP/svnmanager. Create a new user with administrator rights there. Because for security reasons you can not use the initial administrator account to create repositories and users.

So log out and login with the new admin user and setup repositories users and privileges.
Repositories created via SVNManager look like:
http://your-ip/svn/repository-name

7. Troubleshooting for Linux Debian Lenny


For Linux Debian Lenny following bug is known. A locale setting problem can occur if en_US.UTF-8 is not used. This can be fixed with the following steps.
apt-get install locales
export LC_ALL=en_US.UTF-8
dpkg-reconfigure locales (run the command and select en_US.UTF-8 as default)
reboot

Wednesday, November 10, 2010

Problem opening .xib file(NIB file) with xCode 3.x.x

The .xib or NIB files in a iOS project are supposed to open with the Interface Builder.
-> Although the Interface Builder files end with the .xib extension, often Cocoa programmers still refer to them by their old name, NIBs.

After installing Xcode 3.2.4 and the iOS SDK 4.1 the .xib files in my projects didn't open after double click.

When right clicking on the files in Xcode it only showed under "Open As" the options Plain Text File or Source Code File. I searched the Web and people often recommend to reinstall Xcode. That totally did not work for me.

The solution


First I checked if the Interface Builder was installed on my Mac OS. I used the Spotlight (Command+Space opens the search field) and searched for Interface Builder. Surprisingly the Builder was installed on the System.

If the Interface Builder is installed you have to connect the NIB files with it. Use Finder to navigate to one of your NIB files in any Xcode project. Then right-click and use File > Open With and select Interface Builder. Since I did that one time I could always open any .xib file directly in XCode project view with double click.

Tuesday, November 9, 2010

Error in Hibernate: unexpected token: LIMIT near line ...

Why do I get the "unexpected token: LIMIT..." error?


Hibernate doesn't support the LIMIT token in a SELECT statement. Whereas in a Java SQL PreparedStatement to define a limit on the result set you can use the LIMIT command in the SELECT query.
SELECT * FROM person_table as person WHERE last_name = "Miller" LIMIT 0,20;
If you use Hibernate instead this command wouldn't work. You would get an error looking like:
"unexpected token: LIMIT near line 1 column..."

How to limit results for paging in a Hibernate query?


The proper way in Hibernate select queries would be to use:
query.setFirstResult(0);
query.setMaxResults(20);

Example on how to use paging in Hibernate HQL select query?


Below an example on how to execute a SELECT query with a limit on the returned result set in Hibernate. Especially if you want to use pagination through the results this example how to pass in a page size and page number and get the according results from the table.
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.List;
.
.
.

public List<Person> getPersonsByLastName(String lastName, int pageSize, int pageNum) throws Exception
{
 //unless you need only certain columns you don't need to add the SELECT part in the select query (HQL query) in Hibernate 
 String sqlQuery = "FROM Person as person where person.lastName=:lastName";
  
 Query query = workspace.getReadOnlyDbQuery(sqlQuery);

 // Pass all the parameters
 query.setParameter("lastName", lastName);
 query.setFirstResult(getStartIndex(pageSize, pageNum));
 query.setMaxResults(pageSize);
  
 List<Person> result = query.list();
 if (result != null && result.size() > 0) {
  return result;
 } else {
  return null;
 }
}

public Query getReadOnlyDbQuery(String queryString) throws Exception
{
 Session session = getReadOnlyDbSession();
 Query query = session.createQuery(queryString);
 query.setTimeout(5);
 return query;
}

//calculates the start index for pagination
private int getStartIndex(int pageSize, int pageNum) {
 if (pageNum < 1)
  return 0;

 return (pageNum - 1) * pageSize;
}

Thursday, November 4, 2010

How to enabled mod_rewrite on Apache2.2 and Linux Debian 5 Lenny

Below a little tutorial with an example on how to enable apache mod_rewrite to be able to use htaccess, that is for example necessary to use pretty permalinks in WordPress and Joomla, with URL rewriting. The is no extra download necessary if you have Apache 2.2 already installed and running.


There are two easy steps to activate, install or enable mod_rewrite for Apache 2.2 Web Server. Go in the Command line of your linux. It doesn't matter where you are in the folder structure, but you should either be the root or put sudo infront of the commands, like below to switch to the root user.

1. Type below command and hit enter

sudo a2enmod rewrite
A successful response should be:
Enabling module rewrite.
Run '/etc/init.d/apache2 restart' to activate new configuration!


2. Restart Apache2.2

/etc/init.d/apache2 restart
Again, use sudo in front of the command, if you not logged in as root user.
You could also use:
/etc/init.d/apache2 stop
/etc/init.d/apache2 start
If you have problems in Linux RedHat you might want to try:
/etc/init.d/httpd restart

Problem: /etc/init.d/apache2 command not found


Another command that could work instead is:
sudo service apache2 restart
Another reason for this error message is that you might not have installed apache2 properly. In this case it helps to delete the old installation and run a clean install via.
sudo apt-get install apache2


Problem: /etc/init.d/apache2 permission denied



If you are not the root user run:
sudo /etc/init.d/apache2 restart
The instructions might also work for other Debian versions and other Linux destributions like Open Suse Linux, Ubuntu, RedHat etc. Let me know if you have problems with the instructions.

Monday, November 1, 2010

JMeter - How to reset the listeners

Especially when writing a new load test you work a lot with the jmeter listeners. After a while you don't want old requests to show up in the listener log anymore. Otherwise you have to scroll forever.

My first attempt to empty the listener was to right click on one. But there is no option to reset the listeners (e.g. Summary Report or View Result Tree).

Then for a while I always was just saving my JMeter load test and reopening it to get rid of the old logs in the listener and I was ok with that. Since a co worker asked me about it, I thought there must be a better way and after some investigation I found the answer to how to empty the listener.

Either in the "Run menu", select "Clear" when the according listener is selected. Or you select Clear all if you want to reset or listener logs.

The according shortcuts are Control+Shift+E and Control+E.