How to retrieve culture specific currency and dates

gbzhhu
gbzhhu used Ask the Experts™
on
Hi,

I am doing an ASP.NET app that uses satellite assemblies for resources.  Resources work fine so I can change culture from a dropdown and get the correct resource fot hat culture.  However, I cannot get currency and dates working.  I have attached code below

       btnGenError.Text = String.Format("{0:c}", resourceManager.GetString("errorButton", ci))

Always returns £33.22 or 33.22 whichever i put in the resource, no matter what culture is active.  I expected if I set Italian culture to see a Euro sign before currency.  What am I doing wrong?

Thanks
Private Sub LoadResources(ByVal dbFs As DatabaseFunctions)

        Dim culture As String = Me.cboLanguages.SelectedItem.Value
        Dim ci As New CultureInfo(culture)
        Dim resourceManager As ResourceManager

        resourceManager = New ResourceManager("DBT.Extranet.Resources", Assembly.GetExecutingAssembly())

        Thread.CurrentThread.CurrentCulture = ci
        Thread.CurrentThread.CurrentUICulture = ci

        titleText.Text = resourceManager.GetString("welcomeText", ci)
        litAccess.Text = resourceManager.GetString("accesslevel", ci) & " " & dbFs.AccessLevel(0)
        btnGenError.Text = String.Format("{0:c}", resourceManager.GetString("errorButton", ci))

    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
Try this code--the difference should be using the current culture for the String.Format:

     String.Format(m_currentCulture, "{0:c}", currencyText)
Imports System.Reflection
Imports System.Resources
Imports System.Globalization

Public Class ResourceService

    Private m_resourceManager As ResourceManager
    Private m_currentCulture As CultureInfo

    Public Sub New(ByVal resourceName As String, ByVal cultureName As String)
        m_resourceManager = New ResourceManager(resourceName, Assembly.GetExecutingAssembly())
        m_currentCulture = New CultureInfo(cultureName)
    End Sub

    ''' <summary>
    ''' Get a string from the currently selected resource
    ''' </summary>
    ''' <param name="resourceName">The name of the resource (i.e. FirstName, LastName, etc.)</param>
    ''' <returns>A localized string</returns>
    Public Function GetString(ByVal resourceName As String) As String
        Return m_resourceManager.GetString(resourceName, m_currentCulture)
    End Function

    ''' <summary>
    ''' Get currency value from the selected resource, formatted in the currency style for that culture.
    ''' </summary>
    ''' <param name="resourceName">The name of the resource (i.e. FirstName, LastName, etc.)</param>
    ''' <returns>Currency value taken from resource</returns>
    ''' <remarks></remarks>
    Public Function ConvertLocalizedCurrency(ByVal resourceName As String) As String
        Dim currencyText As String = Me.GetString(resourceName)
        Return String.Format(m_currentCulture, "{0:c}", currencyText)
    End Function

    ''' <summary>
    ''' Provide a property to change the current culture
    ''' </summary>
    ''' <value>A new CultureInfo object</value>
    ''' <returns>The current CultureInfo object</returns>
    Public Property CurrentCulture As CultureInfo
        Get
            Return m_currentCulture
        End Get
        Set(ByVal value As CultureInfo)
            m_currentCulture = value
        End Set
    End Property

End Class

Open in new window

Author

Commented:
Hello Bob,

Your code makes perfect sense and I was planning on making a class like yours after doing a proof of concept that all works as expected.

titleText.Text = String.Format(ci, "{0:c}", resourceManager.GetString("welcomeText", ci))

Using the above line, if welcometext resource has a value of £22.23 titleText.Text displays £22.23.  if I try to 22.23 in the resource titleText.Text displays 22.23.  It doesn't seem to be having any effect.

One thing I didn't mention, I am generating these resources at runtime.  User types a values for a label and decides a language, the I create the .resources file and compile to a sattelite assembly at runtime.  The resource s work just culture specific values like dates, currency don't and I suspect i am just leaving out some minor detail when retreieving resource

Can you help any further

Cheers
H
Most Valuable Expert 2012
Top Expert 2008
Commented:
1) Generating resource assemblies at run-time doesn't make any sense to me.

2) Tried these experiments:

        Dim currentCulture As New CultureInfo("en-GB")
        Dim currency As String = String.Format(currentCulture, "{0:c}", "22.23")       <-- this didn't work

        Dim currentCulture As New CultureInfo("en-GB")
        Dim currency As String = String.Format(currentCulture, "{0:c}", 22.23)          <-- £22.23

        Dim currentCulture As New CultureInfo("en-US")
        Dim currency As String = String.Format(currentCulture, "{0:c}", 22.23)          <-- $22.23

3) Correction:

    Public Function ConvertLocalizedCurrency(ByVal resourceName As String) As String
        Dim currency As Decimal = Val(Me.GetString(resourceName))
        Return String.Format(m_currentCulture, "{0:c}", currency)
    End Function

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
1)  Why?  We want our clients to have access to tool on the site where they give access to a translator.  The tool will display all resources in English (imbed in main assembly) then translator will provide value for each item we then save the resource and compile it.  This is for a basic CMS not a website so we are translating menus, few paragraphs etc

2)  Thanks that clarified it.  It needed a numeric number to format the currency

3)  Val function didn't work well, was trancating number if there was a comma in it so i used Convert.ToDecimal

Thanks Bob.  Igf you could comment on point 1 I would be grateful

H
Most Valuable Expert 2012
Top Expert 2008

Commented:
I don't quite understand your environment.  Is this a shared application, locally-installed application, ...?  Are you doing anything with the generated assemblies once they are generated, or just using them locally?

Author

Commented:
I was wrong when I said CMS.  This is a non CMS web application.  We call it "Extranet"  Everytime we create a website for a client we then end up doing this tool/app that allows site admions to add/remove users, add/remove services, define roles (our own as opposed ASP.NET roles).  It is all about user management really.  We usually have something like www.site.com/userManagement.  Because we keep doing this almost everytime a new client has a website we decided to code a new one in .NET 4 VS2010 .  We want to add multi language feature to it.  For a new client we would just "brand it" for them but all the base functionality will be built in.  I hope you are with me Bob.  I have been tasked to proof that we can allow translators to add resources for a new language.  Each client will obviously have their own copy of the app in their server (we host) so each client can customise how they like, some may have 1 language others may have 20.  Think that makes sense?
Most Valuable Expert 2012
Top Expert 2008

Commented:
This was clearly a case of the misunderstanding that comes from not having all the details.  It sounded like, on the surface, that it was a stand-alone system, and generating satellite assemblies for the same machine that is going to use them doesn't make sense.  If you have one person generating these assemblies for the application, that others share, that makes perfect sense.  Satellite resource assemblies are a good choice for multi-language support.

Author

Commented:
Cheers Bob.  Yes, I think I left out some details and concentrated on my coding issue.  I wanted however to be sure I wasn't going the wrong path and a few words from the LearnedOne wouldn't hurt.  

Point go to you of course, not that you need it :-).  Thanks for helping out yet again.  You are a top man

Hassan

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial