Exemplo n.º 1
0
        private void setMultilineAddressField(PwEntry pwEntry, string sectionTitle, AddressSectionField addressSectionField)
        {
            string addressLocale = addressSectionField.v.country;

            if (string.IsNullOrEmpty(addressLocale))
            {
                addressLocale = "us";
            }

            // Find the address format used in the country where this address is located
            // Locale IDs can contain dashes (-) which are illegal characters in resource files, so they're replaced with underscores (_)
            string addressFormatString = Properties.MultilineAddressFormat.ResourceManager.GetString(string.Join("_", new string[] { "Country", addressLocale.Replace('-', '_') }));

            // If we couldn't find a specific format for the country, use a generic one
            if (string.IsNullOrEmpty(addressFormatString))
            {
                addressFormatString = Properties.MultilineAddressFormat.Country_us;
            }

            List <string> components = this.getTokenizedAddressComponents(addressFormatString, addressSectionField);

            if (components.Count > 0)
            {
                string fieldValue = string.Join(Environment.NewLine, components.ToArray());
                string fieldLabel = addressSectionField.t;

                // If the field is in a named section, prefix its name to avoid collisions
                if (!string.IsNullOrEmpty(sectionTitle))
                {
                    fieldLabel = string.Concat(sectionTitle, " - ", addressSectionField.t);
                }

                pwEntry.Strings.Set(fieldLabel, new ProtectedString(false, fieldValue));
            }
        }
Exemplo n.º 2
0
        private void setExpandedAddressField(PwEntry pwEntry, string sectionTitle, AddressSectionField addressSectionField)
        {
            string addressLocale = addressSectionField.v.country;

            if (string.IsNullOrEmpty(addressLocale))
            {
                addressLocale = "us";
            }

            foreach (PropertyInfo propertyInfo in addressSectionField.v.GetType().GetProperties())
            {
                List <string> fieldLabelParts = new List <string>();
                string        fieldLabel      = null;
                string        fieldValue      = propertyInfo.GetValue(addressSectionField.v, null) as string;

                // Skip the field if it's empty
                if (string.IsNullOrEmpty(fieldValue))
                {
                    continue;
                }

                // Find how this address part is called in the country where this address is located
                string addressPartName = Properties.ExpandedAddressParts.ResourceManager.GetString(string.Join("_", new string[] { "Address", addressLocale.Replace('-', '_'), propertyInfo.Name }));

                // Find the localized address part name
                if (!string.IsNullOrEmpty(addressPartName))
                {
                    fieldLabel = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "Address", addressPartName }));
                }

                // If no localized version of the address part name is found, use the generic field name
                if (string.IsNullOrEmpty(fieldLabel))
                {
                    fieldLabel = CultureInfo.CurrentUICulture.TextInfo.ToTitleCase(propertyInfo.Name.ToLower());
                }

                // If the field is in a named section, prefix its name to avoid collisions
                if (!string.IsNullOrEmpty(sectionTitle))
                {
                    fieldLabelParts.Add(sectionTitle);
                }

                // Then add the field title, if it has one
                if (!string.IsNullOrEmpty(addressSectionField.t))
                {
                    fieldLabelParts.Add(addressSectionField.t);
                }

                // And finally the address part name
                fieldLabelParts.Add(fieldLabel);

                // Join all parts together
                fieldLabel = string.Join(" - ", fieldLabelParts.ToArray());

                // Find the localized country name
                if (propertyInfo.Name.Equals("country"))
                {
                    // Locale IDs can contain dashes (-) which are illegal characters in resource files, so they're replaced with underscores (_)
                    fieldValue = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "Menu", "country", fieldValue.Replace('-', '_') }));

                    // If no localized country name is found, use the ISO country code
                    if (string.IsNullOrEmpty(fieldValue))
                    {
                        fieldValue = fieldValue.ToUpper();
                    }
                }
                else if (propertyInfo.Name.Equals("street"))
                {
                    // Older format had two address lines, which may be joined with a new line
                    fieldValue = StringExt.ReplaceNewLines(fieldValue, " ");
                }

                pwEntry.Strings.Set(fieldLabel, new ProtectedString(false, fieldValue));
            }
        }
Exemplo n.º 3
0
        private List <string> getTokenizedAddressComponents(string addressFormatString, AddressSectionField addressSectionField)
        {
            string[]      tokens     = addressFormatString.Split(new char[] { '|' });
            List <string> components = new List <string>();

            foreach (string token in tokens)
            {
                string[]      subtokens     = token.Split(new char[] { ' ' });
                List <string> subcomponents = new List <string>();

                foreach (string subtoken in subtokens)
                {
                    PropertyInfo propertyInfo = addressSectionField.v.GetType().GetProperty(subtoken);
                    if (propertyInfo != null)
                    {
                        string tokenValue = propertyInfo.GetValue(addressSectionField.v, null) as string;

                        if (string.IsNullOrEmpty(tokenValue))
                        {
                            continue;
                        }

                        // Find the localized country name
                        if (subtoken.Equals("country"))
                        {
                            // Locale IDs can contain dashes (-) which are illegal characters in resource files, so they're replaced with underscores (_)
                            tokenValue = Properties.Strings.ResourceManager.GetString(string.Join("_", new string[] { "Menu", "country", tokenValue.Replace('-', '_') }));

                            // If no localized country name is found, use the ISO country code
                            if (string.IsNullOrEmpty(tokenValue))
                            {
                                tokenValue = tokenValue.ToUpper().Replace('_', ' ');
                            }
                        }
                        else if (subtoken.Equals("street"))
                        {
                            // Older format had two address lines, which may be joined with a new line
                            tokenValue = StringExt.ReplaceNewLines(tokenValue, " ");
                        }

                        subcomponents.Add(tokenValue);
                    }
                }

                if (subcomponents.Count > 0)
                {
                    components.Add(string.Join(" ", subcomponents.ToArray()));
                }
            }

            return(components);
        }