Programming Languages Hacks

Importanti regole per linguaggi di programmazione rilevanti come Java, C, C++, C#…

  • Subscribe

  • Lettori

    I miei lettori abituali

  • Twitter

Split using delimiter except when delimiter is escaped

Posted by Ricibald on 11th September 2014

Split using delimiter except when delimiter is escaped

A common problem is to split a string handling escape characters.

This extension method handle the case of split considering the escape.
It uses the negative lookbehind feature of regular expressions.

Posted in .net | 73 Comments »

Ninject: Replace without unbind

Posted by Ricibald on 10th August 2012

In Ninject the method Rebind<T> does the following:

  • Unbind<T>: removes all binding for that types, also named binding!
  • Bind<T>: add the specified binding

This means that using Rebind<T>().To<U>().Named(“X”) you are not just replacing the binding named “X” but you are clearing all existing binding registered with T!

If you want to just replace it just use this:

Bind<T>().To<U>.Named("X").ReplaceExisting();

 

Here the source code to achieve this (works in Ninject 2+):
 

using System.Linq;
using Ninject.Planning.Bindings;
using Ninject.Syntax;

// ReSharper disable CheckNamespace
namespace System
// ReSharper restore CheckNamespace
{
    public static class ExtensionsNinject
    {
        /// <summary>
        /// Replace existing binding.
        /// To replace a binding you should never use rebind becauses unbind all existing
        /// binds and then bind yours.
        /// You should first configure the entire binding using Bind and only as last 
        /// operation you should declare to replace it using this method.
        /// </summary>
        public static void ReplaceExisting<T>(this IBindingInSyntax<T> bindingInSyntax)
        {
            ReplaceExistingImpl<T>(bindingInSyntax);
        }

        /// <summary>
        /// Replace existing binding.
        /// To replace a binding you should never use rebind becauses unbind all existing
        /// binds and then bind yours.
        /// You should first configure the entire binding using Bind and only as last 
        /// operation you should declare to replace it using this method.
        /// </summary>
        public static void ReplaceExisting<T>(this IBindingOnSyntax<T> bindingInSyntax)
        {
            ReplaceExistingImpl<T>(bindingInSyntax);
        }

        /// <summary>
        /// Replace existing binding.
        /// To replace a binding you should never use rebind becauses unbind all existing
        /// binds and then bind yours.
        /// You should first configure the entire binding using Bind and only as last 
        /// operation you should declare to replace it using this method.
        /// </summary>
        public static void ReplaceExisting<T>(this IBindingWhenSyntax<T> bindingInSyntax)
        {
            ReplaceExistingImpl<T>(bindingInSyntax);
        }

        private static void ReplaceExistingImpl<T>(IBindingSyntax bindingInSyntax)
        {
            var kernel = bindingInSyntax.Kernel;
            var bindingsToRemove = kernel.GetBindings(typeof(T)).Where(b => string.Equals(b.Metadata.Name, bindingInSyntax.BindingConfiguration.Metadata.Name, StringComparison.Ordinal));
            foreach (var bindingToRemove in bindingsToRemove)
            {
                kernel.RemoveBinding(bindingToRemove);
            }
            
            var binding = new Binding(typeof(T), bindingInSyntax.BindingConfiguration);
            kernel.AddBinding(binding);
        }
    }
}

Posted in ninject | No Comments »

iOS NIB Localization in Localizable.strings

Posted by Ricibald on 3rd June 2011

Scenario:

I want to localize my app AllSync but I was taken by a little frustration.

Following this good guide you can see that there are two main way to localize your iOS (iPhone/iPad) app:

  • localize strings using Localizable.strings
  • localize single nib file creating one version for each
The second point is very frustrating for three reasons:
  • you have to mantain updated all version of your nib localized files
  • there are specialized sites like icanlocalize that translates only your Localizable.string, not your nib files
  • you have to take care about the resulting layout of single translation
The problem:
I want to localize my nib files in the same way of using Localizable.strings

A possible solution can be to externalize in code every text setting but is a tedious process and you can’t use the flexibility of xcode 4. The real solution I hope will be used by many of you is this:

The solution:

Create an extended version of iOS user control that automatically translates every single text

Simple to obtain: just see this extended UIButton

@interface UIButtonThemed : UIButton
@end

@implementation UIButtonThemed
- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder]))
	{
        self.titleLabel.adjustsFontSizeToFitWidth = YES;
        NSString* text = NSLocalizedString(self.titleLabel.text, nil);
        [self setTitle:text forState:UIControlStateNormal];
        [self setTitle:text forState:UIControlStateHighlighted];
        [self setTitle:text forState:UIControlStateDisabled];
        [self setTitle:text forState:UIControlStateSelected];
	}
	return self;
}
@end

You can use it inside xcode 4 configuring the base class for your control:

Then you can see my italian Localizable.strings:
"Sync" = "Sincronizza";
"Settings" = "Impostazioni";
"Help" = "Guida";
"Share" = "Condividi";
"Rate us" = "Votaci";
"Follow us" = "Seguici";
And the corresponding result:

Posted in iphone, projects | 12 Comments »