krul
krul

Reputation: 2269

Where sitecore keeps languages found in SC.Context.Database.Languages?

I have following code:

public void Process(SC.Pipelines.HttpRequest.HttpRequestArgs args)
{
   if (HttpContext.Current == null || SC.Context.Database == null)
   {
      return;
   }
string languageCode = DomainHandler.Instance.GetDomainLanguage(HttpContext.Current.Request.Url.Host);
SC.Globalization.Language language = SC.Context.Database.Languages.First(x => x.Name == languageCode);

that results in error message once run:

Sequence contains no matching element

I'm guessing somebody deleted the languages and now SC.Context.Database.Languages collection is empty hence the error.

I can not log into sitecore cms and I'm curious how I can fix this?

Upvotes: 0

Views: 316

Answers (4)

Night Monger
Night Monger

Reputation: 780

May be little late.. But can you try this

Sitecore.Collections.LanguageCollection languages = LanguageManager.GetLanguages(Sitecore.Context.ContentDatabase);
string languageCode = "xxx"; //get some logic to get your language code. 
Sitecore.Globalization.Language language = languages.FirstOrDefault(x => x.Name == languageCode);

Upvotes: 0

scFootsteps
scFootsteps

Reputation: 133

sitecore languages are stored in /sitecore/system/languages node. Also, it is always good to use FirstOrDefault instead of First, since First will return an exception if matching item was not found.

Upvotes: 0

krul
krul

Reputation: 2269

I haven't solved the problem but curbed it so that user can at least log into cms by assigning default language in case none of them is available by using following code:

string languageCode = DomainHandler.Instance.GetDomainLanguage(HttpContext.Current.Request.Url.Host);
var languages = SC.Context.Database.GetLanguages().Where(l => l.Name == languageCode);
//make dutch default language
SC.Globalization.Language language = Sitecore.Globalization.Language.Parse("nl-NL");
if (languages.Count() > 0)
{
    language = languages.First();
}

I'm still puzzled why languages collections is empty?

Upvotes: 0

alinulms
alinulms

Reputation: 519

You should get the current language just by saying

var languages = Context.Database.GetLanguages();

Then you can apply the Where clause. There may be a difference between Db.Languages and Db.GetLanguages()

Upvotes: 1

Related Questions