0

I'm new to WPF and trying to mash together some concepts I'm reading about.

What I'm trying to do is a build a localizable UI. For simplicity, let's say I am building a UI with string: "The file takes up 2 GB in disk space."

The "2 GB" portion is dynamic. The value could change depending upon the file the user selects. Secondly, a conversion should take from ulong (file size bytes) to string (friendly size, using appropriate units e.g. KB, MB, GB, TB, etc.).

I was thinking an IValueConverter would be most appropriate for the byte count to friendly-file-size conversion. I was also thinking that I'd store "The file takes up {0} in disk space." as a string resource.

I'm not sure the IValueConverter will be of use here. Can it be used with String.Format()? I don't see how it could be used in a binding directly, because we're inserting the conversion result into the localizable text template.

Is there a better way to approach this?

jglouie
  • 12,523
  • 6
  • 48
  • 65
  • Localization more refers to date and currency formats used by various cultures. To my knowledge KB, MB, GB, and TB are the same in all cultures. So I would just use a converter. – paparazzo Feb 21 '12 at 16:19
  • It's obvious that KB, MB, GB, and TB are what he refers to as "dynamic text" and that the "localizable" part is `The file takes up {0} in disk space.` - for i18n/translation. – ANeves Jan 29 '14 at 12:08

2 Answers2

1

Bindings have a StringFormat property, you should be able to use that if you can somehow reference your localized string (possibly using a custom markup extension).

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Surely this is a standard problem, localizable text templates, isn't it? – jglouie Feb 21 '12 at 19:51
  • @LemonBeagle: If you look around you might find something here on SO, i for one hate localization and don't want anything to do with it if i can avoid it. If you store the localized strings in your application resources you can reference them using `{x:Static prop:Resources.SomeString}` where prop is mapped to the `Properties` clr-namespace which usually contains those resources. If you use a different mechanism you need to come up with a different method. – H.B. Feb 21 '12 at 20:02
  • I used this link here: http://www.hardcodet.net/2009/03/lookup-resource-value-using-markup-extension and it worked beautifully – jglouie Feb 21 '12 at 22:04
  • @LemonBeagle: As i said a custom markup extension is handy for those things, glad it worked out for you. – H.B. Feb 21 '12 at 22:08
1

Use this handy bytes name to text converter and an IValueConverter

Converting bytes to GB in C#?

    private string formatBytes(float bytes)
        {
            string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
            int i;
            double dblSByte=0;
            for (i = 0; (int)(bytes / 1024) > 0; i++, bytes /= 1024)
                dblSByte = bytes / 1024.0;
            return String.Format("{0:0.00} {1}", dblSByte, Suffix[i]);
        }

public class BytesSuffixConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        float bytes = (float)value;
        return formatBytes(bytes);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("Not Supported.");
    }
}
Community
  • 1
  • 1
tcables
  • 1,231
  • 5
  • 16
  • 36
  • Thanks, but there is more to the problem. I also want to translate the text to other languages, and dynamically insert this converter's output into that text – jglouie Feb 21 '12 at 16:56