Ruhrpottpatriot / GW2.NET

A user friendly wrapper around the official GW2 API
GNU General Public License v2.0
17 stars 17 forks source link

Localizable strings #12

Open SamHurne opened 9 years ago

SamHurne commented 9 years ago

[Moved from Codeplex: https://gw2dotnet.codeplex.com/workitem/1239]

Right now, it is still possible to pass in a CultureInfo object per request. I'm thinking about switching to Thread.CurrentUICulture for /v2 services. You'd still be able to pass your own CultureInfo object into the constructor. When you don't, the current UI culture will be used instead.

Info: http://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture(v=vs.100).aspx

Ruhrpottpatriot wrote Sep 22, 2014 at 4:31 AM

Yup, do it. Seems like a good default.

StevenLiekens wrote Oct 19, 2014 at 6:02 AM

I settled on a slightly different approach. I did not like the ability to change the language once a service object has been created. If a user wants to retrieve details in 4 languages, then the user should create 4 different instances of the endpoint class.

In the end, I decided on factory classes that configure endpoints for a given language. The endpoint object itself is immutable, so the language cannot be changed afterwards.

Example: ItemFactory for /v2/items

This factory class has an indexed property that expects a two-letter language code. It uses that index value to configure the item service object.

public IRepository this[string language]
{ 
   get
   {
      return new ItemService(this.ServiceClient) { Culture = new CultureInfo(language) };
   }
} 

The code wouldn't be complete without a default option though.

public IRepository Default
{ 
   get { return new ItemService(this.ServiceClient); }
} 

Usage

// Explicit language
var itemsDE = GW2.V2.Items["de"];
var itemsEN = GW2.V2.Items["en"];
var itemsES = GW2.V2.Items["es"];
var itemsFR = GW2.V2.Items["fr"];

// Default language (API decides what language to use)
var items = GW2.V2.Items.Default; 

System language So what about automatically determining the preferred language? I was thinking about adding helpers to the factory class.

public IRepository ForCurrentCulture
{
   get { return this[CultureInfo.CurrentCulture.TwoLetterISOLanguageName]; }
}

public IRepository ForCurrentUICulture
{
   get { return this[CultureInfo.CurrentUICulture.TwoLetterISOLanguageName]; }
}

Usage

// Current system language
var items = GW2.V2.Items.ForCurrentCulture; 

// Current UI language
var itemsUI = GW2.V2.Items.ForCurrentUICulture;

Any thoughts on this? I really need all the feedback I can get on what goes into the public API.