Ejemplo n.º 1
0
        private void AddResourceTokens(Web web, LocalizationCollection localizations, FileConnectorBase connector)
        {
            if (localizations != null && localizations.Any())
            {
                //https://github.com/SharePoint/PnP-Provisioning-Schema/issues/301
                //fixing issue to allow multiple resx files in the template. i.e:
                //<pnp:Localization LCID="1033" Name="core" ResourceFile="core.en-us.resx" />
                //<pnp:Localization LCID="3082" Name="core" ResourceFile="core.es-es.resx" />
                //<pnp:Localization LCID="1033" Name="intranet" ResourceFile="intranet.en-us.resx" />
                //<pnp:Localization LCID="3082" Name="intranet" ResourceFile="intranet.es-es.resx" />
                var resourcesFilesCount = localizations.GroupBy(l => l.Name).Count();

                // Read all resource keys in a list
                List <Tuple <string, uint, string> > resourceEntries = new List <Tuple <string, uint, string> >();
                foreach (var localizationEntry in localizations)
                {
                    var filePath = localizationEntry.ResourceFile;
                    using (var stream = connector.GetFileStream(filePath))
                    {
                        if (stream != null)
                        {
#if !NETSTANDARD2_0
                            using (ResXResourceReader resxReader = new ResXResourceReader(stream))
                            {
                                foreach (DictionaryEntry entry in resxReader)
                                {
                                    // One can have multiple resource files in a single file, by adding tokens with resource file name and without we allow both scenarios to resolve
                                    resourceEntries.Add(new Tuple <string, uint, string>($"{localizationEntry.Name}:{entry.Key}", (uint)localizationEntry.LCID, entry.Value.ToString().Replace("\"", "&quot;")));
                                    resourceEntries.Add(new Tuple <string, uint, string>(entry.Key.ToString(), (uint)localizationEntry.LCID, entry.Value.ToString().Replace("\"", "&quot;")));
                                }
                            }
#else
                            var xElement = XElement.Load(stream);
                            foreach (var dataElement in xElement.Descendants("data"))
                            {
                                var key   = dataElement.Attribute("name").Value;
                                var value = dataElement.Value;
                                resourceEntries.Add(new Tuple <string, uint, string>($"{localizationEntry.Name}:{key}", (uint)localizationEntry.LCID, value.ToString().Replace("\"", "&quot;")));
                                resourceEntries.Add(new Tuple <string, uint, string>(key.ToString(), (uint)localizationEntry.LCID, value.ToString().Replace("\"", "&quot;")));
                            }
#endif
                        }
                    }
                }

                var uniqueKeys = resourceEntries.Select(k => k.Item1).Distinct();
                foreach (var key in uniqueKeys)
                {
                    var matches = resourceEntries.Where(k => k.Item1 == key);
                    var entries = matches.Select(k => new ResourceEntry()
                    {
                        LCID = k.Item2, Value = k.Item3
                    }).ToList();
                    LocalizationToken token = new LocalizationToken(web, key, entries);

                    _tokens.Add(token);
                }
            }
        }
Ejemplo n.º 2
0
        private void AddResourceTokens(Web web, LocalizationCollection localizations, FileConnectorBase connector)
        {
            if (localizations != null && localizations.Any())
            {
                // Read all resource keys in a list
                List <Tuple <string, uint, string> > resourceEntries = new List <Tuple <string, uint, string> >();
                foreach (var localizationEntry in localizations)
                {
                    var filePath = localizationEntry.ResourceFile;
                    using (var stream = connector.GetFileStream(filePath))
                    {
                        if (stream != null)
                        {
#if !NETSTANDARD2_0
                            using (ResXResourceReader resxReader = new ResXResourceReader(stream))
#else
                            using (ResourceReader resxReader = new ResourceReader(stream))
#endif
                            {
                                foreach (DictionaryEntry entry in resxReader)
                                {
                                    resourceEntries.Add(new Tuple <string, uint, string>(entry.Key.ToString(), (uint)localizationEntry.LCID, entry.Value.ToString().Replace("\"", "&quot;")));
                                }
                            }
                        }
                    }
                }

                var uniqueKeys = resourceEntries.Select(k => k.Item1).Distinct();
                foreach (var key in uniqueKeys)
                {
                    var matches = resourceEntries.Where(k => k.Item1 == key);
                    var entries = matches.Select(k => new ResourceEntry()
                    {
                        LCID = k.Item2, Value = k.Item3
                    }).ToList();
                    LocalizationToken token = new LocalizationToken(web, key, entries);

                    _tokens.Add(token);
                }
            }
        }