Search code examples
c#asp.net.netlocalizationc1-cms

Generate localized URL for a translated page


According to How localization works I can translate pages and give them a different URL for every language.

Example from the tutorial:

  • http://butterflysite.co.uk/en-GB/Home/contact/Newoffice.aspx (english)
  • http://vlindersite.nl/nl-NL/thuis/contact/NieuwKantoor.aspx (dutch)

My question is now: Assuming I know which language I am currently in, how do I find out what the URL title for my page is in C#?

What is the best way to use the C1 API to create the correct link to the page in the current language?


Solution

  • Variant No1

        public string GetPageUrl(Guid pageId, CultureInfo locale)
        {
            using(var conn = new DataConnection(PublicationScope.Published, locale))
            {
                var pageNode = new SitemapNavigator(conn).GetPageNodeById(pageId);
                return pageNode != null ? pageNode.Url : null;
            }
        }
    

    Variant No2

        public string GetPageUrl(Guid pageId, CultureInfo locale)
        {
            var pageUrlData = new PageUrlData(pageId, PublicationScope.Published, locale);
            return PageUrls.BuildUrl(pageUrlData, UrlKind.Public, new UrlSpace());
        }