Squidex.ClientLibrary, Localization and Headers X-Languages|X-Flatten

I am…

  • new to squidex
  • 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.

Expected behavior

According to https://docs.squidex.io/concepts/localization I should get simply
a JSON string for my localized field when combining both headers and could use

public string X { get; set; }

in my BlogPostData.

Minimal reproduction of the problem

Environment

  • [ ] Self hosted with docker
  • [X] Self hosted with IIS
  • [ ] Self hosted with other version
  • [ ] Cloud version

Version: [VERSION]

Browser:

  • [X] Chrome (desktop)
  • [ ] Chrome (Android)
  • [ ] Chrome (iOS)
  • [X] Firefox
  • [ ] Safari (desktop)
  • [ ] Safari (iOS)
  • [ ] IE
  • [X] Edge

Others:

As I stated above I am absolutly new to squidex. So I think I must be something missing.
But what?

Which value do you use for X-Languages and X-Flatten?

The InvariantConverter is only useful for non-localizable fields.

Hi Sebastian,

tried a view things, currently I have

        this.HttpContext.Request.Headers.Add("X-Languages", "de");
        this.HttpContext.Request.Headers.Add("X-Flatten", "true");

right before

var (total, posts) = await apiClient.GetBlogPostsAsync(page, PageSize);

in Post() method of HomeController

You know, that you set the headers of your request coming in and not the headers of the request to Squidex?

If you use Squidex Client library you can pass in a query context: https://github.com/Squidex/squidex-samples/blob/master/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/QueryContext.cs

var ctx = QueryContext.Default.Flatten().WithLanguages("de");

Then pass it to the GetAsync calls: https://github.com/Squidex/squidex-samples/blob/master/csharp/Sample.Blog/Sample.Blog/Models/ApiClient.cs#L40

2 Likes

oh well, that sounds very easy & very nice.

thank you very much for your quick response, seems like i just have to learn a little more about using the client but that’s a good starting point.

1 Like

hi sebastian,

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?

hi

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 :-))

1 Like