experimenting with your blogging sample in aspnet core and localization
I’m submitting a…
[ ] Regression (a behavior that stopped working in a new release)
[ ] Bug report
[ ] Performance issue
[X] Documentation issue or request
Current behavior
I have added a new field with the simple name X to the “Posts” schema of the blog C# sample
and set this field to localizeable. In the settings I have languages “en” and “de”.
I do set HttpContext.Request.Headers “X-Languages” and “X-Flatten” and, if I am not
totaly mistaken, should get a JSON STRING for my field “X” but I still get a JSON ARRAY
(for “en” and “de” language) in public async Task Posts(int page = 0)
method of the Home controller.
When I add this field in BlogPostData
[JsonConverter(typeof(InvariantConverter))]
public string X { get; set; }
I get the exception “Property must have a invariant language property.”
When I remove the [JsonConverter(typeof(InvariantConverter))] attribute according to ClientLibrary Localization I get the exception
“JsonReaderException: Unexpected character encountered while parsing value: {. Path ‘items[0].data.X’, line 1, position 339.”
But when I change the X field to
public Dictionary<string, string> X { get; set; }
everything works - except that I get both languages in return.
tried it out - basicaly it works but i experiences troubles [or! i am assuming something wrong] with .Flattern() - let me explain:
when i use
var ctx = QueryContext.Default.Flatten().WithLanguages("de");
var ctoken = default(CancellationToken);
var posts = await postsClient.GetAsync(query, ctx, ctoken);
in ApiClient.cs and set the X field in BlogPostData.cs again back from
public Dictionary<string, string> X { get; set; }
to a simple string
public string X { get; set; }
i always get this exception:
JsonSerializationException: Property must have a invariant language property.
when i keep the dictionary this works
@Html.Raw(post.Data.X["de"])
and “de” is also the only language (no english has been loaded into the JSON array)
but this means that i do not experience any difference between
var ctx = QueryContext.Default.Flatten().WithLanguages("de");
and
var ctx = QueryContext.Default.WithLanguages("de");
for both seem to return for the localized field X the very same content (some kind of JSON array and not simply a JSON string).
sounds a little bit like in ClientLibrary Localization but i do not use the [JsonConverter(typeof(InvariantConverter))] attribute for my string field X.
It looks okay, but the message JsonSerializationException: Property must have a invariant language property. only comes from the InvariantConverter. Are you sure you have recompiled it and that you use the context for all calls to the postsClient?
yes and yes - i’ve found the failure on my side. just removing the attribute from the field in question was not sufficient i rather hat to remove it from all other properties (which are not localized) in the class as well:
public sealed class BlogPostData
{
//[JsonConverter(typeof(InvariantConverter))]
public string Title { get; set; }
//[JsonConverter(typeof(InvariantConverter))]
public string Slug { get; set; }
//[JsonConverter(typeof(InvariantConverter))]
public string Text { get; set; }
//[JsonConverter(typeof(InvariantConverter))]
public string X { get; set; }
//public Dictionary<string, string> X { get; set; }
}
but that’s more than ok; not having to type in a line for each prop is way much better than the opposite :-))