What is a good data structure for cascading objects?

I wondered what types of data structures and design patterns are used when implementing something like CSS, where you can specify formatting or some other property at different levels of detail.

One specific example I'm currently working on is internationalizing an application.

First of all, English is the default language, but the application will be used in two different regions, in America and Europe. In most cases, different fragments of text and labels will be the same between the two regions, but in some cases the technical terms will differ depending on the region. When translating text into another language, some retain the original English text for the region.

Thus, the text search for the shortcut will work as follows. Look for a specific combination of region and language. If there is nothing, look only at the language. If nothing, look at the combination of English and the region. If nothing, look only at the English language.

I am looking for the best ways to store this type of data in a database, as well as in a data structure in code. Either in this particular case, or in general for these types of situations.

+3
source share
5 answers

hierarchical of course!

Database:

create table Element 
(
    Id int not null,      --primary key
    ParentId int null,    --parent element foreign key
    tag varchar(64)       --etc
)
create table ElementProperty
(
    Id int not null,          --primary key
    ElementId int not null,   --owning element
    PropertyName varchar(64) not null,
    PropertyValue varchar(512) null
)

an object:

public class Element
{
    public string Tag;    //use a property instead though
    public Element ParentElement;
    public IDictionary<string,string> CssProperties;
    public string GetCssPropertyValue(string propertyName)
    {
        if (CssProperties.HasKey(propertyName))
        {
            return CssProperties[propertyName];
        }
        if (ParentElement != null)
        {
            return ParentElement.GetCssPropertyValue(propertyName);
        }
        return null;
    }
}
+1
source

- . - - , . -, - , - , , , -.

Lua , . metatable . /.

+1

, - , "en" "en_US" "en_UK" .

, , . , Java ResourceBundle, : javadoc java.util.ResourceBundle

, GNU gettext, : GNU gettext.

, API i18n/l10n.

+1

RFC 4646 ( , , "DE-AT" ), .

, , , / (, ) ; "DE-AT", "DE", "(= > " EN"). .

SQL, .

/ , . - - .

+1

:

string lookup(key, language, region) {
    string result = datasearch(key, language, region)
    if result == null {
        result = datasearch(key, language, "")
    }
    if result == null {
        result = datasearch(key, "EN", region)
    }
    if result == null {
        result = datasearch(key, "EN", "")
    }
    return result
}

:

  • lookup, .

  • .

  • , , "" ( ), .

+1

All Articles