2

I have an localizeable c# application. There is a file Localization.resx and a file Localization.de.resx. The Localization.resx contains the invariant language - it is english.

The user should be able to select his preferred language. I found the first answer of this question Programmatic way to get all the available languages (in satellite assemblies) to determine which cultures are available. I see that there is de and InvariantCulture available.

But I can't offer InvariantCulture. An user wont know what this is. Instead I should offer en.

I can't rename Localization.resx to Localization.en.resx, since this file is needed to create the Class holding the resources.

I could copy Localization.resx to Localization.en.resx, but then I would always have to keep those files consistent and also need to remove InvariantCulture from the available cultures.

I could write a converter that visualizes the the InvariantCulture as en. But this, I think, is an ugly hack.

Any help please?

Community
  • 1
  • 1
Steffen
  • 2,381
  • 4
  • 20
  • 33
  • I had a similar situation and ended up hard-coding "en". I'm curious if anyone has a better solution. – Jack A. Mar 09 '16 at 16:23
  • 1
    We call this language "default" (it's somewhat English, but more *"nerdish"*). For true `en` rather make a dedicated satellite. – Sinatr Mar 09 '16 at 16:28
  • @Sinatr. So, you mean copy to `Localization.en.resx` and remove `InvariantCulture` from the "selectable resources"? – Steffen Mar 09 '16 at 16:36
  • Hard to see how this could be a hangup. You already know what your "invariant" language looks like, it is never going to change. Pragmatism is usually absent when implementing features like this. The user already knows what his preferred language looks like, it is the same one he used for at least the past 5 years, no point in him having to tell you. – Hans Passant Mar 09 '16 at 16:36
  • @Hans: I don't see your point. The Application will be delivered in several countries. So there is a default language, but several preferred languages. – Steffen Mar 09 '16 at 16:38
  • 2
    Steffen - since 'de' and 'invariant' works for you, how about hiding invariant from the selection UI and creating another resource that will be 'en' and that will be empty? If I recall well, keys not found will fallback to invariant, hence en=invariant and only 1 resx to mantain. – quetzalcoatl Mar 11 '16 at 22:10
  • You could use my answer here to determine all available non-invariant languages http://stackoverflow.com/questions/5112828/loop-through-embedded-resources-of-different-languages-cultures-in-c-sharp and simply forget about the invariant language? – Simon Mourier Mar 12 '16 at 17:01
  • @quetzalcoatl. Thanks I think I will implement something like that. – Steffen Mar 14 '16 at 09:33

2 Answers2

3

In the UI we let user know what "By Default" application has English localization. And offer to change it by showing all available options except Invariant. Also it won't be an ugly hack to write a converter that visualizes the the InvariantCulture as en.

skalinkin
  • 1,024
  • 9
  • 19
3

You can use the NeutralResourcesLanguageAttribute assembly attribute to set the neutral language.

[assembly: NeutralResourcesLanguageAttribute("en-US", UltimateResourceFallbackLocation.Satellite)]

This also makes it "fallback" to other satellites if a key is not found in one resource (if you ask for a key which is not defined in resource.en-US.resx, it'll search in resource.en.resx and finally in resource.resx)

I'm not sure how you are listing the language names, but in case this still gives InvariantCulture, you could have an empty Localization.en.resx, and with this method it'll find all missing resources in Localization.resx, so no need to have them in sync.

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • Thank you sir. The InvariantCulture ist still shown. But I think I will implement it this way. Remove the invariant language from the selectable Languages and maintain an empty "Localization.en.resx". – Steffen Mar 14 '16 at 09:36
  • If you are 100% sure (and you are) that `InvariantCulture` is `English`, why not just replace the string in the selectable languages UI? – Jcl Mar 14 '16 at 09:37
  • I am sure - right now. But I think this is an ugly hack. I can't tell what will happen when another person gets the source code. – Steffen Mar 14 '16 at 10:24
  • @Steffen I understand... I don't think it's an ugly hack as long as it's in the specifications (software without any specifications is just a whole ugly hack ;-) ). If the specs say that the "neutral language is english", then it's just a way of complying with the spec... as hacky as it could be having an empty resource or any other means (without any specification, having an empty resource is just as hacky). The difference is wether that's *specified* or not. Specification could just be a simple comment over the line that replaces the string (`// non-localized resource is considered English`) – Jcl Mar 14 '16 at 11:03
  • 1
    I see your point. However, I think the blank resource file will cause less trouble. If I replace the string and someone decides to add an english resource file, this may cause trouble. I would have english twice for selection. Also, with the blank resource, the default language can be easily changed (don't think thats happening). You are right, those are both hacks. I take the one, my guts tell me its the better one. – Steffen Mar 14 '16 at 12:36
  • @Steffen whatever works, as long as it's specified (at least if someone else has to take on that code at some point in time :-) ) – Jcl Mar 14 '16 at 12:37