Try our conversational search powered by Generative AI!

I get "It is not allowed to create a language version of CultureInfo.InvariantCulture" when creating a default product. What does the message refer to

Vote:
 

Hi,

I have an scheduled job that imports products. The Job works fine in dev and test enviroments but in stage I get the error message ""It is not allowed to create a language version of CultureInfo.InvariantCulture". As you can see in the code below I use "LanguageSelector.MasterLanguage().Language" to get the culture that should be swedish. We have sv and en as our cultures and sv is master.

Any Ideas on what the error message refers to?

We use the cms and commerce from januare release.

public BookProduct ImportBooks(BookModel sourceBook)
{
try
{
//Check if product has any catalogs, if not dont insert book.
if (!sourceBook.categories.Any())
{
ErrorMessages.Add(
$"Book Import: Product with ID {sourceBook.articleNumber} doesn´t have any ICS-catalog ralations and will therefore not be imported.");
return null;
}

//Get all related commerce catalogs
var relatedCatalogs = Catalogs.Where(x => sourceBook.categories.Any(y => y.code == x.Code)).ToList();

if (!relatedCatalogs.Any())
{
ErrorMessages.Add(
$"Book Import: Product with ID {sourceBook.articleNumber} doesn´t have any matching catalogs and will therefore not be imported.");
return null;
}

BookProduct bookProduct = null;

var bookProductReference = _referenceConverter.GetContentLink(sourceBook.articleNumber);

if (ContentReference.IsNullOrEmpty(bookProductReference))
{
var firsCatalog = relatedCatalogs.First();
var catalogContentLink = firsCatalog.ContentLink;

bookProduct = _contentRepository.GetDefault(catalogContentLink,
LanguageSelector.MasterLanguage().Language);
}
else
{
var currentBookPruduct = _contentRepository.Get(bookProductReference);
bookProduct = currentBookPruduct.CreateWritableClone();
}

bookProduct.Edition = sourceBook.edition;
bookProduct.ApprovalDate = sourceBook.approvalDate;
bookProduct.StartPublish = sourceBook.publishDate ?? DateTime.Now;
bookProduct.AnnulationDate = sourceBook.annulationDate;
bookProduct.IsActive = sourceBook.webProduct;
bookProduct.Author = sourceBook.author;
bookProduct.Isbn = sourceBook.isbn;
bookProduct.Issn = sourceBook.issn;
bookProduct.Code = sourceBook.articleNumber;
bookProduct.DocType = sourceBook.docType;
bookProduct.CreateDate = sourceBook.createDate;
bookProduct.LastModifiedDate = sourceBook.lastModifiedDate;
bookProduct.IsActive = sourceBook.webProduct;
bookProduct.SubCategory = !string.IsNullOrEmpty(sourceBook.subCategorySe)
? sourceBook.subCategorySe : sourceBook.subCategoryEn;
bookProduct.Name = !string.IsNullOrEmpty(sourceBook.titleSe)
? sourceBook.titleSe
: sourceBook.titleEn;
bookProduct.LangCode = sourceBook.langCode;
bookProduct.Title = !string.IsNullOrEmpty(sourceBook.titleSe)
? sourceBook.titleSe
: sourceBook.titleEn;
bookProduct.Description = sourceBook.description;
bookProduct.Pages = sourceBook.pages;
bookProduct.Publisher = sourceBook.producedByName;
bookProduct.PdfName = sourceBook.pdfName;

_contentRepository.Save(bookProduct, SaveAction.Publish, AccessLevel.NoAccess);

//Update English Product
var bookProducts = _contentRepository.GetLanguageBranches(
bookProduct.ContentLink);

var englishBookProduct =
bookProducts.FirstOrDefault(x => x.IsInCulture(_languageService.EnglishCulture));

if (englishBookProduct != null)
{
var englishBookProductClone = englishBookProduct.CreateWritableClone();

englishBookProductClone.Name = !string.IsNullOrEmpty(sourceBook.titleEn)
? sourceBook.titleEn
: sourceBook.titleSe;
englishBookProductClone.DisplayName = !string.IsNullOrEmpty(sourceBook.titleEn)
? sourceBook.titleEn
: sourceBook.titleSe;
englishBookProductClone.Title = !string.IsNullOrEmpty(sourceBook.titleEn)
? sourceBook.titleEn
: sourceBook.titleSe;
englishBookProductClone.SubCategory = !string.IsNullOrEmpty(sourceBook.subCategoryEn)
? sourceBook.subCategoryEn
: sourceBook.subCategorySe;
}

//Add Category relations
_relationsHelperService.UpdateCatalogRelations(relatedCatalogs.Select(x => x.ContentLink).ToList(),
bookProduct.ContentLink);

//Add Variations
ImportVariations(sourceBook.variants, bookProduct);

ImportedProducts ++;
return bookProduct;
}
catch (Exception e)
{
AddError($"Book Import: Could not import product {sourceBook.articleNumber} becase of following error {e}");

return null;
}
}

#177312
Edited, Apr 07, 2017 8:02
Vote:
 

LanguageSelector is not indended to be used that way, it is intended to be used when loading content for various ways of handling fallbacks and master language without having to know the exact culture those are. But when creating content you have to specify what language you are creating. If you don't know the language you should create from the input data, you could load the targeted parent category or catalog (the code mixes catalogs and categories so it is a bit hard to see what your intention is, ) to check the master language on those. But you seem to know already what language you should create so you could just hard-code it, read from a setting, or include it in the input data.

#177321
Apr 07, 2017 13:16
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.