1

:)

I'm looking for something that I saw in a project few month ago. I don't know what they use and I'm very interrested to know how it work but i don't know how to search for it... and I'm looking for it during 6 hours now.. can you help me to improve my knowledge ? :D

Database :

|id |key             |en          |fr           |
|1  |TheNameOfTheKey |Hello       |Bonjour      |
|2  |TheNameOfTheKey2|Hello2      |Bonjour2     |

Code :

String s1 = Singleton.Instance.Translation.TheNameOfTheKey
String s2 = Singleton.Instance.Translation.TheNameOfTheKey2

// this key don't exist in Db but return his name instead
String s3 = Singleton.Instance.Translation.AnotherKeyNotInDb 

// Results
// s1 = "Hello"
// s2 = "Hello2"
// s3 = "AnotherKeyNotInDb"

My questions :

  1. What is the type of Translation ? If you have documentation, I will look at !

  2. Something that look like what I'm looking for is : ExpandoObject

Thank you in advance !

  • 1
    This looks like some type of globalization solution. You might want to search on that. – juharr Apr 07 '20 at 14:27
  • 1. There might be a default value specidifed in the code that is returned if no key in the database matches the query. 2. An ExpandoObject is just an object whose members can be added/removed/changed dynamically at runtime. I cannot see a connection of it to your question. – Erik T. Apr 07 '20 at 14:33
  • I think what you are looking for is [DynamicObject](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/walkthrough-creating-and-using-dynamic-objects) – Oguz Ozgul Apr 07 '20 at 14:34
  • I have look for ASP globalization but it's not the same that i've used in the past. I don't know this syntax .. MyObject.NameOfTheKey return the value or his name – Amaury NEYHOUSER Apr 07 '20 at 14:34
  • 1
    `Singleton.Instance.Translation` is probably extends DynamicObject and overrides the `TryGetMember()` and checks if the given name (In the code you see it as a property, but it is actually passed to TryGetMember() in the binder as `binder.Name`) exists as a key in the database. If the key doesn't exist, it returns the key name. – Oguz Ozgul Apr 07 '20 at 14:39
  • I will try DynamicObject as you tell me and come back later. – Amaury NEYHOUSER Apr 07 '20 at 14:41
  • This seems like a custom globalization solution to me. DynamicObject is what you are looking for, that's for sure. – Oguz Ozgul Apr 07 '20 at 14:42

2 Answers2

0

You can use Resource file it works like a dictionary and depends on your localization will load appropriate resource file

Whare strings is a strings.resx file

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");
Console.WriteLine(Properties.strings.Hello);

More details in How to use Localization in C#

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1

Support Ukraine
  • 978
  • 6
  • 20
0

If you want to store on the database, it is a bad idea to keep languages as columns. I would keep them as rows:

|Id|Key             |Language|Translation|
|1 |TheNameOfTheKey |en      |Hello      |
|2 |TheNameOfTheKey |fr      |Bonjour    |

Index Key and Language column, and you will have very fast query results. This is the strength of databases. :)

Roland Buergi
  • 1,157
  • 9
  • 23
  • For sur, but the translation was easier with one line for key and values. We can easely check for the difference between multiple translate. And we need less effort to compare multiple translation. – Amaury NEYHOUSER Apr 08 '20 at 16:05