CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    [RESOLVED] Learn which language CString::LoadString() used

    Hi everyone:


    I was wondering, if I call CString::LoadString() method to load a string from resources how do I know which language did that method use?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Learn which language CString::LoadString() used

    Have you looked through the MFC sources to find out?

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Learn which language CString::LoadString() used

    I don't understand the question.

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Learn which language CString::LoadString() used

    Hi,

    There's no obvious way to do it. You can specify the language you want to be used as the 3rd parameter. However, there's no way to check that MFC was actually able to find the string in that language in your resource DLL.

    Alan

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Learn which language CString::LoadString() used

    Quote Originally Posted by alanjhd08 View Post
    There's no obvious way to do it. You can specify the language you want to be used as the 3rd parameter.
    CString::LoadString does not have any "3rd parameter". It has one and only one:
    BOOL LoadString( UINT nID );
    If the module (exe or dll) contains resources in more than one language then the only way to obtain the ones for the specific language rather than the default one is using resource APIs with Ex suffix:
    FindResourceEx -> LoadResource -> LockResource
    See How To Use LoadResource to Load Strings from a String Table
    Victor Nijegorodov

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Learn which language CString::LoadString() used

    Quote Originally Posted by Arjay
    Have you looked through the MFC sources to find out?
    Yes. What happens on the inside is that the resource string is id'ed with a call to this Win32 API:
    Code:
    ::FindResource( hInstance, MAKEINTRESOURCE( ((id>>4)+1) ), RT_STRING );
    But that doesn't help me much.

    Quote Originally Posted by alanjhd08
    There's no obvious way to do it
    I agree, that seems to be the case.

    Quote Originally Posted by alanjhd08
    You can specify the language you want to be used as the 3rd parameter. However, there's no way to check that MFC was actually able to find the string in that language in your resource DLL.
    That is exactly the reason why I needed to know which version of a string resource was loaded. The reason being, in my application strings were processed a bit differently depending on the language.

    Quote Originally Posted by VictorN
    CString::LoadString does not have any "3rd parameter". It has one and only one:
    That is not true. Or at least in the last versions of MFC. There's also this method:
    Quote Originally Posted by msdn
    BOOL LoadString(
    HINSTANCE hInstance,
    UINT nID,
    WORD wLanguageID
    );
    But I would prefer not to use it and let the language be determined automatically. My reasoning behind it is that this whole language question is quite a complex subject that I may not completely understand at this stage (especially with custom locales in Vista, etc.) thus I'd prefer to let a Windows API handle the language selection.


    But, fellas, I was able to find an easy workaround. By adding a simple language ID string into the resource (that is always in your predefined language, English, in my case) I can easily tell which language was actually loaded.
    Code:
    IDS_STRING_COUNTRY_ID "en_us"

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Learn which language CString::LoadString() used

    Well, I cannot say I understand the solution.
    Best regards,
    Igor

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: [RESOLVED] Learn which language CString::LoadString() used

    Quote Originally Posted by ahmd View Post
    But, fellas, I was able to find an easy workaround. By adding a simple language ID string into the resource (that is always in your predefined language, English, in my case) I can easily tell which language was actually loaded.
    Code:
    IDS_STRING_COUNTRY_ID "en_us"
    Quote Originally Posted by Igor Vartanov View Post
    Well, I cannot say I understand the solution.
    I cannot it either...
    Victor Nijegorodov

  9. #9
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Learn which language CString::LoadString() used

    Quote Originally Posted by Igor Vartanov View Post
    Well, I cannot say I understand the solution.
    OK, say you have three string resources for English, French and German. You add the following string into each:

    //For US resource
    IDS_STRING_COUNTRY_ID "en_us"

    //For French resource
    IDS_STRING_COUNTRY_ID "fr_french"

    //German resource
    IDS_STRING_COUNTRY_ID "ge_german"

    And then when the resource is loaded you check that ID to get the language of the resource loaded.

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Learn which language CString::LoadString() used

    Could you demo the corresponding fragment from .rc file?
    Best regards,
    Igor

  11. #11
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Learn which language CString::LoadString() used

    Quote Originally Posted by Igor Vartanov View Post
    Could you demo the corresponding fragment from .rc file?
    Igor, you're killing me.... Take a look at the resource file snippet:

    Code:
    /////////////////////////////////////////////////////////////////////////////
    //
    // String Table
    //
    
    STRINGTABLE 
    BEGIN
        IDS_STRING_COUNTRY_ID "en_us"
        IDS_STRING_HELLO_WORLD "Hello World!"
        IDS_STRING_GOODBYE_WORLD "Goodbye World!"
    END
    
    
    
    /////////////////////////////////////////////////////////////////////////////
    // French (France) resources
    
    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA)
    #ifdef _WIN32
    LANGUAGE LANG_FRENCH, SUBLANG_FRENCH
    #pragma code_page(1252)
    #endif //_WIN32
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // String Table
    //
    
    STRINGTABLE 
    BEGIN
        IDS_STRING_COUNTRY_ID "fr_france"
        IDS_STRING_HELLO_WORLD "Bonjour tout le monde!"
        IDS_STRING_GOODBYE_WORLD "Adieu le monde!"
    END
    
    #endif    // French (France) resources
    
    
    /////////////////////////////////////////////////////////////////////////////
    // German (Germany) resources
    
    #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_DEU)
    #ifdef _WIN32
    LANGUAGE LANG_GERMAN, SUBLANG_GERMAN
    #pragma code_page(1252)
    #endif //_WIN32
    
    /////////////////////////////////////////////////////////////////////////////
    //
    // String Table
    //
    
    STRINGTABLE 
    BEGIN
        IDS_STRING_COUNTRY_ID "ge_german"
        IDS_STRING_HELLO_WORLD "Hallo Welt!"
        IDS_STRING_GOODBYE_WORLD "Goodbye Welt!"
    END
    
    #endif    // German (Germany) resources

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Learn which language CString::LoadString() used

    Thank you. You might be surprised, but I never did multilanguage support this way. It's always was a per-language resource dll.
    Best regards,
    Igor

  13. #13
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: [RESOLVED] Learn which language CString::LoadString() used

    The same applies to a resource DLL, doesn't it?

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: [RESOLVED] Learn which language CString::LoadString() used

    Quote Originally Posted by ahmd View Post
    The same applies to a resource DLL, doesn't it?
    What is "the same" in this context?
    Victor Nijegorodov

  15. #15
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: [RESOLVED] Learn which language CString::LoadString() used

    Quote Originally Posted by VictorN View Post
    What is "the same" in this context?
    My approach.

    How do you normally load your resource DLL, is it something explicit or you let an API decide which DLL to pick?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured