When calling @umbraco.GetDictionaryValue, is it possible to pass the culture or do I have to create different domains. I currently have sctflash.com, but I don't want to have to createsctflash.com/en, sctflash.com/es, etc. What are my options?
I tried changing my browser language, but that had no effect.
var service = ApplicationContext.Current.Services.LocalizationService;
var dictionaryItem = service.GetDictionaryItemByKey(dictionaryKey);
var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.Id.Equals(languageId));
return translation.Value;
You can probably make it prettier, this code snippet is somewhat old!
Extra (useless info):
The reason I personally used the "language.Id" to match, was because I was using a language selector based on the avaliable languages in the language settings - something like:
@foreach (var language in UmbracoContext.Application.Services.LocalizationService.GetAllLanguages())
{
<li class="flag-item">
<a class="flag @language.CultureInfo.TwoLetterISOLanguageName" href="@Url.Action("ChangeLanguage","LanguageSurface", new{ language.Id })"></a>
</li>
}
var service = ApplicationContext.Current.Services.LocalizationService;
var dictionaryItem = service.GetDictionaryItemByKey(dictionaryKey);
var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.Id.Equals(languageId));
return translation.Value;
and
Can you explain some of it?
I am probably gonna do a language selector too. Have you found that easier than relying on the browser only?
Also, if I pick want to do spanish, do I have to create one for every variation of spanish (Us-es, es, es-ES, etc)? That seems like a pain.
public static class MyDictionary
{
public String GetTranslationForLanguage(String dictionaryKey, int languageId) {
var service = ApplicationContext.Current.Services.LocalizationService; //just to point out the service that governs the dictionary/localization
var dictionaryItem = service.GetDictionaryItemByKey(dictionaryKey); //target the dictionary item found in Umbraco/Settings in the backoffice
var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.Id.Equals(languageId)); //choose the translation of the dictionary item by language ID
return translation.Value; //return the value of the translation of the selected language
}
}
You could find the translation by any .Equals() or == function in the SingleOrDefault(), I just chose to match an ID because that matched what I had to search with.
There are a few methods to match the language you need, you could also do something like:
var translation = dictionaryItem.Translations.SingleOrDefault(x => x.Language.CultureName.Equals(....));
You could try making a dot after the language property to check which match would suit your code best!
I'm not sure if you're selecting the language on the website (like a flag picker) or you could get it off something like:
CultureInfo.CurrentCulture
If you did it based on id, you could then call the first static method like:
var dictionaryKey = "exampleKey"; //the name of the dictionary key in the Umbraco Settings tab
var firstLanguage = UmbracoContext.Application.Services.LocalizationService.GetAllLanguages().First(); //Just taking the first language in the list from the Umbraco Settings tabs under the Language node as example
var translatedText = MyDictionary.GetTranslationForLanguage(dictionaryKey, firstLanguage.Id) //now you have the translated value!
You could probably even match the language from .GetAllLanguages() with something like:
var languageIdentifier = CultureInfo.CurrentCulture.? //some property here
var allLanguages = UmbracoContext.Application.Services.LocalizationService.GetAllLanguages();
var currentLanguage = allLanguages.SingleOrDefault(...) //match inside the SingleOrDefault based on your languageIdentifier
Regarding the language selector, I'd definitely recommend it, always nice to give the poor users the choice if they end up in an unwanted language!
I think one of each variety for Spanish would be a bit overkill, "es-ES" is usually good, but of course it depends if your website is about Spanish talking countries or has the specific need to divide the language, I haven't seen that need a lot though!
Regarding the language picker:
The reason I foreach'ed over the language ids in my first post, is because when they click a flag and send the language id to my Controller, I put that id into a session variable like:
Session["lang"] = id;
If you ensure this is always set (or have some null checks and possibly a default language), you can skip sending the id into the "MyDictionary.GetTranslationForLanguage()" method and take the language id from the session, and only send in the key, but that's just one solution!
I had a few issues while testing, but after ensuring null checks on the session + selecting a fallback language if no session is found (English in my case) - it's been working flawlessly since then!
In my specific case, I had a list of links to a Controller method like:
Then when they clicked the a element - which probably should be a button element - I'd set the session in the Controller and return a redirect, something like:
public ActionResult ChangeLanguage(int id)
{
Session["lang"] = id;
//some short code to figure out where to redirect the user / maybe just to the same page
return Redirect(url);
}
Pass culture to GetDictionaryValue
I have
english
andspanish
setup on my website.When calling
@umbraco.GetDictionaryValue
, is it possible to pass the culture or do I have to create different domains. I currently havesctflash.com
, but I don't want to have to createsctflash.com/en
,sctflash.com/es
, etc. What are my options?I tried changing my browser language, but that had no effect.
You can do something like:
You can probably make it prettier, this code snippet is somewhat old!
Extra (useless info):
The reason I personally used the "language.Id" to match, was because I was using a language selector based on the avaliable languages in the language settings - something like:
HI Daniel,
Where would I put this code:
and
Can you explain some of it?
I am probably gonna do a language selector too. Have you found that easier than relying on the browser only?
Also, if I pick want to do spanish, do I have to create one for every variation of spanish (Us-es, es, es-ES, etc)? That seems like a pain.
Thanks for the wonderful help, Saied
Sure!
You could do a class like:
You could find the translation by any .Equals() or == function in the SingleOrDefault(), I just chose to match an ID because that matched what I had to search with.
There are a few methods to match the language you need, you could also do something like:
You could try making a dot after the language property to check which match would suit your code best!
I'm not sure if you're selecting the language on the website (like a flag picker) or you could get it off something like:
If you did it based on id, you could then call the first static method like:
You could probably even match the language from .GetAllLanguages() with something like:
Regarding the language selector, I'd definitely recommend it, always nice to give the poor users the choice if they end up in an unwanted language!
I think one of each variety for Spanish would be a bit overkill, "es-ES" is usually good, but of course it depends if your website is about Spanish talking countries or has the specific need to divide the language, I haven't seen that need a lot though!
Regarding the language picker:
The reason I foreach'ed over the language ids in my first post, is because when they click a flag and send the language id to my Controller, I put that id into a session variable like:
If you ensure this is always set (or have some null checks and possibly a default language), you can skip sending the id into the "MyDictionary.GetTranslationForLanguage()" method and take the language id from the session, and only send in the key, but that's just one solution!
Daniel,
Thanks for the great info.
.
I had a few issues while testing, but after ensuring null checks on the session + selecting a fallback language if no session is found (English in my case) - it's been working flawlessly since then!
In my specific case, I had a list of links to a Controller method like:
Styling the flag icon something like:
Then when they clicked the a element - which probably should be a button element - I'd set the session in the Controller and return a redirect, something like:
is working on a reply...