Skip to content

Instantly share code, notes, and snippets.

Created August 6, 2013 18:13
Show Gist options
  • Save anonymous/6167044 to your computer and use it in GitHub Desktop.
Save anonymous/6167044 to your computer and use it in GitHub Desktop.
Localization fall through for SQL
CREATE TABLE Content (ContentID varchar(50) NOT NULL, Locale varchar(5) NULL, Value nvarchar(255) NOT NULL)
GO
insert into Content (ContentID, Locale, Content) VALUES ('Welcome','en-us','Welcome to the page. Go USA!')
GO
insert into Content (ContentID, Locale, Content) VALUES ('FirstName','en-us','First Name')
GO
insert into Content (ContentID, Locale, Content) VALUES ('LastName','en-us','Last Name')
GO
insert into Content (ContentID, Locale, Content) VALUES ('Welcome','en-ca','Welcome to the page, eh.')
GO
insert into Content (ContentID, Locale, Content) VALUES ('FirstName','en-ca','First Name (toque)')
GO
insert into Content (ContentID, Locale, Content) VALUES ('LastName','en-ca','Hockey Jersey Name')
GO
insert into Content (ContentID, Locale, Content) VALUES ('Welcome','en','Welcome to the page.')
GO
insert into Content (ContentID, Locale, Content) VALUES ('FirstName','en','First Name')
GO
insert into Content (ContentID, Locale, Content) VALUES ('LastName','en','Last Name')
GO
insert into Content (ContentID, Locale, Content) VALUES ('Welcome',null,'Welcome to the page.')
GO
insert into Content (ContentID, Locale, Content) VALUES ('FirstName',null,'First Name')
GO
insert into Content (ContentID, Locale, Content) VALUES ('LastName',null,'Last Name')
GO
-- The actual query, the first row returned is the best localized value for the given locale (if available), language (if available) or the neutral
SELECT Content FROM Content where ContentID = $ContentID and Locale = $Locale
UNION ALL SELECT Content from Content WHERE ContentID = $ContentID AND Locale = $Language
UNION ALL SELECT Content from Content WHERE ContentID = $ContentID and Locale IS NULL
@kendallmiller
Copy link

I assume the Insert statements should have "Value" in their third field? And the Select should be SELECT VALUE ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment