Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter Kindberg 17 posts 107 karma points
    Aug 14, 2022 @ 15:57
    Peter Kindberg
    0

    Publishing language variant via API

    Hello everyone,

    I'm having an issue figuring out two things in a piece of code of mine.

    In essence, I work on a website that has multiple languages, and multiple countries. Depending on your location, you may see different content, prices etc.

    The nodes for the countries are found at:

    Home -> Localization -> [countries (like Sweden, Denmark etc)]

    All countries are setup properly in English language, however we now need to fill in the German names for them. All German nodes are unpublished, and will need the "Name" value as well as a property called "countryName" set.

    An extract of my code is below:

    for (int x = 0;  x < match.Count; x++) //match is an array of each country in the world, containing name and iso2 code
            {
                if (match[x].Success)
                {
                    string countryname = Common.Helper.RegularExpressions.GetFirstValue(match[x].Value, @"<span style=""display:none"">(.*)</span>");
                    string countrycode = Common.Helper.RegularExpressions.GetFirstValue(match[x].Value, @"<td>(..)</td>");
                    if (string.IsNullOrEmpty(countryname) || string.IsNullOrEmpty(countrycode))
                    {
                        continue;
                    }
                    countrycode = countrycode.ToLower();
                    var countries = local.ChildrenForAllCultures;
                    var country = countries.FirstOrDefault(a => a.Value<string>("countryCode").Equals(countrycode));
                    if (country == null)
                    {
                        continue;
                    }
                    //Converting IPublishedContent to IContent, as required by content service
                    IContent c = _contentService.GetById(country.Id);
                    c.Name = countryname;
                    c.SetValue("countryName", countryname, "de");
                    _contentService.SaveAndPublish(c, "de", -1, false);
    
                }
            }
    

    There are two things that I cannot get my head around:

    1. The c.Name does not appear to work
    2. The language variant is not saved and published.

    I've checked for validation errors that could have prevented this, but no. If I publish the language variant beforehand, lets say for Sweden, so it has a published German node with name "S", the:

     c.SetValue("countryName", countryname, "de"); 
    _contentService.SaveAndPublish(c, "de", -1, false);
    

    works, however c.Name is not set.

    It's driving me a bit crazy, so any help would be greatly appreciated!

    Best regards

    Peter

  • Peter Kindberg 17 posts 107 karma points
    Aug 14, 2022 @ 16:39
    Peter Kindberg
    100

    Hi again,

    Sometimes I guess it helps simply asking the problem out loud to yourself... I completely missed the "iContent.CultureInfos".

    If anyone else runs into this, hopefully it will save someone some time :)

    for (int x = 0;  x < match.Count; x++)
            {
                if (match[x].Success)
                {
                    string countryname = Common.Helper.RegularExpressions.GetFirstValue(match[x].Value, @"<span style=""display:none"">(.*)</span>");
                    string countrycode = Common.Helper.RegularExpressions.GetFirstValue(match[x].Value, @"<td>(..)</td>");
                    if (string.IsNullOrEmpty(countryname) || string.IsNullOrEmpty(countrycode))
                    {
                        continue;
                    }
                    countrycode = countrycode.ToLower();
                    var countries = local.ChildrenForAllCultures;
                    var country = countries.FirstOrDefault(a => a.Value<string>("countryCode").Equals(countrycode));
                    if (country == null)
                    {
                        continue;
                    }
                    if (countryname.Contains('<'))
                    {
                        countryname = countryname.Substring(0, countryname.IndexOf('<'));
                    }
                    IContent c = _contentService.GetById(country.Id);
                    c.CultureInfos.AddOrUpdate("de", countryname, DateTime.Now);
                    c.SetValue("countryName", countryname, "de");
                    _contentService.SaveAndPublish(c, "de", -1, false);
    
                }
            }
    

    Also, sorry for the ugly code, but it's intended to run once, then hit the trash bin.

    Best regards

    Peter

Please Sign in or register to post replies

Write your reply to:

Draft