Bloom.Api.I18NHandler.HandleRequest C# (CSharp) Method

HandleRequest() public static method

public static HandleRequest ( string localPath, IRequestInfo info, CollectionSettings currentCollectionSettings ) : bool
localPath string
info IRequestInfo
currentCollectionSettings Bloom.Collection.CollectionSettings
return bool
        public static bool HandleRequest(string localPath, IRequestInfo info, CollectionSettings currentCollectionSettings)
        {
            var lastSep = localPath.IndexOf("/", System.StringComparison.Ordinal);
            var lastSegment = (lastSep > -1) ? localPath.Substring(lastSep + 1) : localPath;

            switch (lastSegment)
            {
                case "loadStrings":

                    while (_localizing)
                    {
                        Thread.Sleep(0);
                    }

                    try
                    {
                        _localizing = true;

                        var d = new Dictionary<string, string>();
                        var post = info.GetPostDataWhenFormEncoded();

                        if (post != null)
                        {
                            foreach (string key in post.Keys)
                            {
                                try
                                {
                                    if (!d.ContainsKey(key))
                                    {
                                        var translation = GetTranslationDefaultMayNotBeEnglish(key, post[key]);
                                        d.Add(key, translation);
                                    }
                                }
                                catch (Exception error)
                                {
                                    Debug.Fail("Debug Only:" +error.Message+Environment.NewLine+"A bug reported at this location is BL-923");
                                    //Until BL-923 is fixed (hard... it's a race condition, it's better to swallow this for users
                                }
                            }
                        }

                        info.ContentType = "application/json";
                        info.WriteCompleteOutput(JsonConvert.SerializeObject(d));
                        return true;
                    }
                    finally
                    {
                        _localizing = false;
                    }
                    break;

                case "translate":
                    var parameters = info.GetQueryParameters();
                    string id = parameters["key"];
                    string englishText = parameters["englishText"];
                    string langId = parameters["langId"];
                    langId = langId.Replace("V", currentCollectionSettings.Language1Iso639Code);
                    langId = langId.Replace("N1", currentCollectionSettings.Language2Iso639Code);
                    langId = langId.Replace("N2", currentCollectionSettings.Language3Iso639Code);
                    langId = langId.Replace("UI", LocalizationManager.UILanguageId);
                    if (LocalizationManager.GetIsStringAvailableForLangId(id, langId))
                    {
                        info.ContentType = "text/plain";
                        info.WriteCompleteOutput(LocalizationManager.GetDynamicStringOrEnglish("Bloom", id, englishText, null, langId));
                        return true;
                    }
                    else
                    {
                        // Don't report missing strings if they are numbers
                        // Enhance: We might get the Javascript to do locale specific numbers someday
                        // The C# side doesn't currently have the smarts to do DigitSubstitution
                        // See Remark at https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.digitsubstitution(v=vs.110).aspx
                        if (IsInteger(id))
                        {
                            englishText = id;
                        }
                        else
                        {
                            // it's ok if we don't have a translation, but if the string isn't even in the list of things that need translating,
                            // then we want to remind the developer to add it to the english tmx file.
                            if (!LocalizationManager.GetIsStringAvailableForLangId(id, "en"))
                            {
                                ReportL10NMissingString(id, englishText);
                            }
                            else
                            {
                                //ok, so we don't have it translated yet. Make sure it's at least listed in the things that can be translated.
                                // And return the English string, which is what we would do the next time anyway.  (BL-3374)
                                LocalizationManager.GetDynamicString("Bloom", id, englishText);
                            }
                        }
                        info.ContentType = "text/plain";
                        info.WriteCompleteOutput(englishText);
                        return true;
                    }
                    break;
            }

            return false;
        }

Usage Example

Example #1
0
 private bool ProcessI18N(string localPath, IRequestInfo info)
 {
     lock (I18NLock)
     {
         return(I18NHandler.HandleRequest(localPath, info, CurrentCollectionSettings));
     }
 }