Daniel Hoelbling-Inzko talks about programming

Formatting Date and Times in Rails

When looking at formatting Times and Dates in a certain format one quickly looks at strftime for help. Only problem there is that strftime will not take into account the translations configured through Rails I18n gem. So a german representation of 10. Oct 2015 will not render correctly since October is abbreviated in german as Okt.

A solution would be to just define the format pattern through the I18n translations inside the yaml files in config/locales/ - but often you have formats that are set just this once, so externalizing them is not really a good solution.

Looking at the I18n gem the solution is quite easy: I18n.localize(<date>, format: '%d. %b %Y') yields the appropriate result while passing in the localization format. So just replacing the date.strftime(<pattern>) call with I18n.localize(date, format: <pattern>) is the way to go.

You can also pass in the locale you want it formatted in using the locale: :de parameter to localize.

Hope this helps.

Filed under rails3, rails, i18n

Ruby Time.strftime pads %e with whitespace

I just ran into this quick issue so I thought i'd share it.

When trying to create a Date format for Rails i18n.l you are at the mercy of Time.strftime and my format was supposed to look like this: "%e. %b %Y" - so the quite common 1. Jul 2015. According to the excellent http://www.foragoodstrftime.com/ %e should give me the non-zero-padded day of the month - but my tests still failed because of a leading whitespace on days < 10.

Looking at the reference I noticed that strftime allows flags for each replacement to modify how padding and other things work. The solution to getting rid of the whitespace was to change the format to this: %-e. %b %Y.

Here is the list of flags from the documentation:

-  don't pad a numerical output
_  use spaces for padding
0  use zeros for padding
^  upcase the result string
#  change case
:  use colons for %z

The flags just go after the % and before the format directive. Hope this helps.

Filed under ruby, i18n, rails

You have to know English to be a Programmer!

Scott Hanselman just blogged this interesting letter from Fabrice Fonck on Microsoft’s effort to localize Visual Studio and .NET entitled Do you have to know English to be a Programmer? and I wanted to share my thoughts on this as a non-native speaker:

As most of you may know, I am from Austria – so my native tongue is German. There should be about 100 million people speaking that language and so it’s rather natural that there are many sites discussing programming topics in German and also there are many books that get translated. So, while I know that you could be a rather successful programmer in Germany/Austria without English, I really believe it’s utterly impossible to be a good programmer without being fluent in English!

Why? Simply, because being a good programmer is not about knowledge, ability or technology but about your ability to acquire even more knowledge and constantly improve and refine your skills.

And you really can’t do that if deny yourself access to 90% of the internet’s resources. You’ll miss out on great articles from people like Scott Hanselman, Ayende Rahien, Miško Hevery, Rob Conery, Carl Senguin and many more who have contributed great articles that have influenced my skill directly.

Not accepting that English is the predominant language in software development only fragments the community and hinders sharing of knowledge.

And to be honest, all our programming languages are based on the English syntax, so I can’t think about a way for somebody with no English skills to write code against the .NET framework anyway. The code we produce is in English and defines it’s context/meaning/purpose through naming. Not being able to understand the meaning of System.Net.MailMessage.To is a serious problem when trying to write software (when not copying snippets and samples).

One thing remains to be said: When writing code I constantly encounter one problem: German speaking customers vs English speaking code. Naming my objects in German doesn't work for me, it starts to feel wrong if you mix two languages (English syntax and German variables), so I translate the German requirements and object names to English ones.

This makes it sometimes difficult to talk to the the customer during development, because I use words he doesn’t know and I constantly have to map his words to mine. This isn’t so much of a problem as long as you as a developer don’t mix things up and choose your translations right. Still it annoys a bit and I would be thankful for a English customer once in a while :).

Finally: I switched my blog to English almost a year ago because I felt that blogging in German created a barrier for me. I couldn’t point anyone outside Austria/Germany to my writings. And while many people since have asked my why I neglect German (mostly out of curiosity), I can’t say I have ever regretted this move (and my English did benefit from this).

My Photography business


dynamic css for .NET