private void ParseFile(string projectDirectory, string filePath, ConcurrentDictionary <string, TemplateItem> templateItems) { var referencePath = (projectDirectory != null) && filePath.StartsWith(projectDirectory, StringComparison.OrdinalIgnoreCase) ? filePath.Substring(projectDirectory.Length + 1) : filePath; DebugHelpers.WriteLine("FileNuggetFinder.ParseFile -- {0}", filePath); // Lookup any/all nuggets in the file and for each add a new template item. using (var fs = File.OpenText(filePath)) { var extension = Path.GetExtension(filePath); _nuggetParser.ParseString(fs.ReadToEnd(), delegate(string nuggetString, int pos, Nugget nugget, string i_entity) { AddNewTemplateItem( referencePath, i_entity.LineFromPos(pos), nugget, templateItems); // Done. return(null); // null means we are not modifying the entity. }, extension); } }
public string ProcessNuggets(string entity, LanguageItem[] languages) { // Lookup any/all msgid nuggets in the entity and replace with any translated message. string entityOut = _nuggetParser.ParseString(entity, delegate(string nuggetString, int pos, Nugget nugget, string i_entity) { // Formatted nuggets: // // A formatted nugget will be encountered here like this, say: // // [[[Enter between %0 and %1 characters|||100|||6]]] // // while the original string in the code for this may have been: // // [[[Enter between %0 and %1 characters|||{1}|||{2}]]] // // The canonical msgid part is that between the opening [[[ and the first |||: // // Enter between %0 and %1 characters // // We use that for the lookup. // LanguageTag lt; string message; // Check for unit-test caller. if (_textLocalizer == null) { return("test.message"); } // Lookup resource using canonical msgid. message = _textLocalizer.GetText( true, // true = try lookup with HtmlDecoded-msgid if lookup with raw msgid fails. nugget.MsgId, nugget.Comment, languages, out lt); // if (nugget.IsFormatted) { // Convert any identifies in a formatted nugget: %0 -> {0} message = ConvertIdentifiersInMsgId(message); // Format the message. var formatItems = new List <string>(nugget.FormatItems); try { // translate nuggets in parameters for (int i = 0; i < formatItems.Count; i++) { // if formatItem (parameter) is null or does not contain NuggetParameterBegintoken then continue if (formatItems[i] == null || !formatItems[i].Contains(_settings.NuggetParameterBeginToken)) { continue; } // replace parameter tokens with nugget tokens var fItem = formatItems[i].Replace(_settings.NuggetParameterBeginToken, _settings.NuggetBeginToken).Replace(_settings.NuggetParameterEndToken, _settings.NuggetEndToken); // and process nugget formatItems[i] = ProcessNuggets(fItem, languages); } message = string.Format(message, formatItems.ToArray()); } catch (FormatException /*e*/) { //message += string.Format(" [FORMAT EXCEPTION: {0}]", e.Message); message += "[FORMAT EXCEPTION]"; } } // Output modified message (to be subsituted for original in the source entity). DebugHelpers.WriteLine("I18N.NuggetLocalizer.ProcessNuggets -- msgid: {0,35}, message: {1}", nugget.MsgId, message); // if (_settings.VisualizeMessages) { string languageToken = string.Empty; if (!string.IsNullOrWhiteSpace(_settings.VisualizeLanguageSeparator)) { languageToken = lt.ToString() + _settings.VisualizeLanguageSeparator; } string endToken = _settings.NuggetVisualizeToken; if (!string.IsNullOrWhiteSpace(_settings.NuggetVisualizeEndToken)) { endToken = _settings.NuggetVisualizeEndToken; } message = string.Format("{0}{1}{2}{3}", _settings.NuggetVisualizeToken, languageToken, message, endToken); } return(message); // NB: this was originally returning HttpUtility.HtmlEncode(message). // Ref #105 and #202 as to why changed back to returning message as is. }); // Return modified entity. return(entityOut); }