private void GenerateBamlStream(Stream input, Stream output, Dictionaries curDictionaries) { string commentFile = Path.ChangeExtension(options.Input, "loc"); TextReader commentStream = null; try { if (File.Exists(commentFile)) { commentStream = new StreamReader(commentFile); } // create a localizabilty resolver based on reflection var localizabilityReflector = new BamlLocalizabilityByReflection(options.Assemblies); // create baml localizer var mgr = new BamlLocalizer( input, localizabilityReflector, commentStream ); // get the resources var source = mgr.ExtractResources(); var translations = new BamlLocalizationDictionary(); foreach (DictionaryEntry entry in source) { var key = (BamlLocalizableResourceKey)entry.Key; var translatedResource = curDictionaries.FindCorrespondence(key); if (translatedResource != null) { string translatedContent = translatedResource.Content; if (!String.IsNullOrEmpty(translatedContent)) { var curResource = (BamlLocalizableResource)entry.Value; if (curResource.Content != translatedContent) { translations.Add(key, translatedResource); } } } } // update baml mgr.UpdateBaml(output, translations); } finally { if (commentStream != null) { commentStream.Close(); } } }
private static string GenerateBamlStream(Stream input, Stream output, BamlLocalizationDictionary dictionary, LocBamlOptions options) { string commentFile = Path.ChangeExtension(options.Input, "loc"); TextReader commentStream = null; try { if (File.Exists(commentFile)) { commentStream = new StreamReader(commentFile); } // create a localizabilty resolver based on reflection BamlLocalizabilityByReflection localizabilityReflector = new BamlLocalizabilityByReflection(options.Assemblies); // create baml localizer BamlLocalizer mgr = new BamlLocalizer( input, localizabilityReflector, commentStream ); // get the resources BamlLocalizationDictionary source = mgr.ExtractResources(); BamlLocalizationDictionary translations = new BamlLocalizationDictionary(); foreach (DictionaryEntry entry in dictionary) { BamlLocalizableResourceKey key = (BamlLocalizableResourceKey)entry.Key; // filter out unchanged items if (!source.Contains(key) || entry.Value == null || source[key].Content != ((BamlLocalizableResource)entry.Value).Content) { translations.Add(key, (BamlLocalizableResource)entry.Value); } } // update baml mgr.UpdateBaml(output, translations); } catch (Exception ex) { return(ex.Message); } finally { if (commentStream != null) { commentStream.Close(); } } return(null); }
//-------------------------------------- // Private methods //-------------------------------------- // construct the maps for enumeration internal void EnsureMap() { if (_localizableResources != null) { return; // map is already created. } // create the table based on the treesize passed in // the hashtable is for look-up during update _resolver.InitLocalizabilityCache(); _keyToBamlNodeIndexMap = new Hashtable(_tree.Size); _uidToBamlNodeIndexMap = new Hashtable(_tree.Size / 2); _localizableResources = new BamlLocalizationDictionary(); for (int i = 0; i < _tree.Size; i++) { BamlTreeNode currentNode = _tree[i]; // a node may be marked as unidentifiable if it or its parent has a duplicate uid. if (currentNode.Unidentifiable) { continue; // skip unidentifiable nodes } if (currentNode.NodeType == BamlNodeType.StartElement) { // remember classes encountered in this baml BamlStartElementNode elementNode = (BamlStartElementNode)currentNode; _resolver.AddClassAndAssembly(elementNode.TypeFullName, elementNode.AssemblyName); } // find the Uid of the current node BamlLocalizableResourceKey key = GetKey(currentNode); if (key != null) { if (currentNode.NodeType == BamlNodeType.StartElement) { // store uid mapping to the corresponding element node if (_uidToBamlNodeIndexMap.ContainsKey(key.Uid)) { _resolver.RaiseErrorNotifyEvent( new BamlLocalizerErrorNotifyEventArgs( key, BamlLocalizerError.DuplicateUid ) ); // Mark this element and its properties unidentifiable. currentNode.Unidentifiable = true; if (currentNode.Children != null) { foreach (BamlTreeNode child in currentNode.Children) { if (child.NodeType != BamlNodeType.StartElement) { child.Unidentifiable = true; } } } continue; // skip the duplicate node } else { _uidToBamlNodeIndexMap.Add(key.Uid, i); } } _keyToBamlNodeIndexMap.Add(key, i); if (_localizableResources.RootElementKey == null && currentNode.NodeType == BamlNodeType.StartElement && currentNode.Parent != null && currentNode.Parent.NodeType == BamlNodeType.StartDocument) { // remember the key to the root element so that // users can further add modifications to the root that would have a global impact. // such as FlowDirection or CultureInfo _localizableResources.SetRootElementKey(key); } // create the resource and add to the dictionary BamlLocalizableResource resource = _localizableResourceBuilder.BuildFromNode(key, currentNode); if (resource != null) { _localizableResources.Add(key, resource); } } } _resolver.ReleaseLocalizabilityCache(); }