Example #1
0
        public static Language GetMostSimilarTo(string code)
        {
            // Look for an exact match
            code = code.ToLowerInvariant();
            var exactPath = "languages/" + code + ".lang";

            if (Assets.Exists <Language>(exactPath))
            {
                return(Language.Get(exactPath));
            }

            int underscoreIndex = code.IndexOf('_');

            if (underscoreIndex >= 0)
            {
                // Look for a root match on the language part (ie: en_GB -> en)
                var langPart     = (underscoreIndex > 0) ? code.Substring(0, underscoreIndex) : code;
                var langPartPath = "languages/" + langPart + ".lang";
                if (Assets.Exists <Language>(langPartPath))
                {
                    return(Language.Get(langPartPath));
                }

                // Look for a similar match on the language part (ie: en_GB -> en_US)
                foreach (var otherLanguage in Assets.List <Language>("languages"))
                {
                    var otherCode = otherLanguage.Code;
                    if (otherCode.StartsWith(langPart + "_", StringComparison.InvariantCulture))
                    {
                        return(otherLanguage);
                    }
                }
            }

            // If there was nothing simular, use english
            if (Assets.Exists <Language>("languages/en.lang"))
            {
                return(Language.Get("languages/en.lang"));
            }

            // If english isn't loaded yet, use debug
            return(Language.Get("languages/debug.lang"));
        }
Example #2
0
 public static IEnumerable <Language> GetAll()
 {
     return(Assets.List <Language>("languages"));
 }