This project is read-only.

Custom IFilterProvider for Localization

Topics: Localization, Writing modules
Nov 28, 2014 at 12:41 AM
When I filter a query for a projection using the built-in "For current culture" filter, items which do not have any translations set (but which are localizable) do not show up as a result. Therefore I'm trying to create a custom filter.

I went on looking for the filterprovider implementation and created a new one based on that one in which I added a an Or() statement which should also include items without a CultureId (where it is null). However my HQL adventure seems to have failed. Can anyone provide some guidance into how this could be solved?
            var query = (IHqlQuery)context.Query;

            if (currentCulture == "en-US")
                context.Query = query.Where(x => x.ContentPartRecord<LocalizationPartRecord>(),
                    x => x.Or(y => y.IsNull("CultureId"), z => z.Eq("CultureId", currentCultureId)));
                context.Query = query.Where(x => x.ContentPartRecord<LocalizationPartRecord>(), x => x.Eq("CultureId", currentCultureId));
Nov 28, 2014 at 11:13 AM
So you are saying that you want the master item to show up if no child exists?
Nov 28, 2014 at 12:34 PM
Looking at how the LocalizationPart works I guess that's correct. However only when the default language ("en-US" in this case) is set as current culture.
Nov 29, 2014 at 3:43 PM
Does this make any sense? Or should I take another approach?
Feb 1, 2015 at 3:22 AM
Edited Feb 1, 2015 at 5:05 AM
Just tried it on a 1.8.x version.

@RobertWebcare, when the culture isn't already assigned the "CultureId" isn't null, it's equal to "0". So, if the current culture is equal to your specific "default" culture (here en-US), then you can do your specific query. But, in place of using this
y => y.IsNull("CultureId")
Try to use this
y => y.Eq("CultureId", 0)
Maybe here we would need a "real default culture", not only a "current default culture". And we could assume that a culture not already assigned is of "real default culture". Then, in the culture filter we can test if default = current, then include or not the items where CultureId = 0

Another solution would be to use the Id of this new default culture when a new row is added to the LocalizationPartRecord and when CultureId = 0

That's was when you first add the "LocalizationPart" (for the Page type in my test) and you work with items already created. Then, when I created a new Page, all the CultureId equal to 0 were assigned to the current site culture, but not for the last item created!

In fact, when the CultureId = 0, the item.LocalizationPart.Culture = null, but (for this item) the corresponding row in the LocalizationPartRecord exists with CultureId = 0. That's why a query that target the LocalizationPartRecord and test if CultureId = 0 can work