2

I have a number of PDFs in English language. I have web pages in English and German lang.

If in a German Page I want to display PDF of English lang, it is not possible as that German version PDF is not available, so I tried to do fallback for media library item, even then no help.

So can someone please tell me any alternative for this.

NOTE: I don't want to upload english document in German version, as there are other languages available and customers cannot upload those many times in all lang.

I need to upload a document in Only English but display in all other Languages irrespective of that document is there in that lang or not.

It's ok even if I need to make changes through code.

Thanks in advance

4

3 回答 3

1

您使用哪个模板上传 PDF?如果您正在使用,/sitecore/templates/System/Media/Unversioned/Pdf则 this 继承自/sitecore/templates/System/Media/Unversioned/File,并且此 blob 字段仍被标记为共享:

文件模板

共享字段跨语言版本共享,因此如果您上传英文 PDF 并从德语项目链接到同一媒体项目,那么它将链接到原始英文 PDF。

在 Sitecore 中,当向模板添加字段时,有一个名为“共享”的复选框。这是为了什么?

于 2013-06-10T19:03:02.700 回答
0

有几种方法可以做到这一点。您可以在渲染代码中执行此操作,也可以使用Sitecore 市场中的语言后备模块。

要在代码中执行此操作,您需要创建一个新的 MediaProvider。创建一个继承Sitecore.Resources.Media.MediaProvider并覆盖该protected virtual MediaData GetMediaData(MediaUri mediaUri)方法的类。

此方法获取上下文语言或 Uri 中语言的站点核心项。所以你可以在这里实现回退:

public class MediaProviderWithFallback : Sitecore.Resources.Media.MediaProvider
{
    protected override Sitecore.Resources.Media.MediaData GetMediaData(Sitecore.Resources.Media.MediaUri mediaUri)
    {
        Assert.ArgumentNotNull((object)mediaUri, "mediaUri");
        Database database = mediaUri.Database;
        if (database == null)
        {
            return null;
        }
        string mediaPath = mediaUri.MediaPath;
        if (string.IsNullOrEmpty(mediaPath))
        {
            return null;
        }
        Language language = mediaUri.Language;
        if (language == null)
        {
            language = Context.Language;
        }
        Sitecore.Data.Version version = mediaUri.Version;
        if (version == null)
        {
            version = Sitecore.Data.Version.Latest;
        }
        Sitecore.Data.Items.Item mediaItem = database.GetItem(mediaPath, language, version);
        if (mediaItem == null)
        {
            return (MediaData)null;
        }
        // Check for language fallback
        if (mediaItem.Versions.Count == 0)
        {
            // Workout your language fallback here from config or sitecore settings etc...
            language = Language.Parse("en");
            // Try and get the media item in the fallback language
            mediaItem = database.GetItem(mediaPath, language, version);
            if (mediaItem == null)
            {
                return null;
            }
        }
        return MediaManager.Config.ConstructMediaDataInstance(mediaItem);
    }
}

请注意 - 这是未经测试的代码。您应该将您的回退存储在配置中或修改 sitecore 中的语言模板。

一旦你有了那个类,你需要更新你的 web.config 以使用你的提供者而不是 Sitecores。因此,在 web.config 中找到此部分并将类型更改为您的类和汇编:

    <!-- MEDIA PATH -->
<mediaPath defaultProvider="default">
  <providers>
    <clear />
    <add name="default" type="Sitecore.Resources.Media.MediaPathProvider, Sitecore.Kernel" />
  </providers>
</mediaPath>
于 2013-06-10T13:44:05.097 回答
0

尝试确定媒体是否需要提前进行版本控制是最佳实践。如果您知道媒体需要根据语言进行版本控制,则应确保在 web.config 中更新以下属性:

<!--By default, Media items are not versionable and the below setting is set to false in the web.config.  
    If you upload an image in one language, it will persist across all language versions.
    If you change this to true, then versioning will apply and you would have to set the media item into all language versions, 
    or enable fallback, but if enforce version presence is turned on and media template guids are included in EnforceVersionPresenceTemplates, 
    then you'll have to make sure all language versions at least exist-->
  <setting name="Media.UploadAsVersionableByDefault">
    <patch:attribute name="value">true</patch:attribute>
  </setting>

Alex Shyba 的 Partial Language Fallback 模块将成功处理此问题。我建议确保不要在任何媒体模板上强制执行版本存在(不想强制管理员必须创建空白语言版本)。然后他们可以创建英文版本,然后只在需要覆盖它时创建一个语言版本。

在使用部分语言回退的情况下,您需要确保在媒体版本化模板字段上选中启用回退复选框。

我还建议更新媒体提供程序,以便将语言嵌入到媒体 url 中,这样缓存就不会发挥作用。EG:如果你创建一个名为 Directions.pdf 的 pdf 文件,它会在 www.site.com/media/Directions.pdf 加载,当你在语言之间切换时,它很可能会缓存它。因此,您需要更新媒体提供程序以使用上下文语言对媒体 url 进行编码。

你可以在这里看到一个演示: https ://github.com/Verndale-Corp/Sitecore-Fallback-FullDemo

public class CustomMediaProvider : MediaProvider
{
    public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
    {
        Assert.ArgumentNotNull((object)item, "item");
        Assert.ArgumentNotNull((object)options, "options");

        string result = base.GetMediaUrl(item, options);

        // Added by Verndale, check if language should be embedded
        UrlOptions urlOptions = UrlOptions.DefaultOptions;
        urlOptions = LanguageHelper.CheckOverrideLanguageEmbedding(urlOptions);
        if (urlOptions.LanguageEmbedding == LanguageEmbedding.Always && options.UseItemPath)
        {
            result = "/" + Sitecore.Context.Language.Name.ToLowerInvariant() + Sitecore.StringUtil.EnsurePrefix('/', result);
        }

        return result;
    }

    public static UrlOptions CheckOverrideLanguageEmbedding(UrlOptions urlOptions)
    {
        var thisSite = Sitecore.Context.Site;

        if (urlOptions.Site != null)
            thisSite = urlOptions.Site;

        if (!String.IsNullOrEmpty(thisSite.SiteInfo.Properties["languageEmbedding"]))
        {
            if (thisSite.SiteInfo.Properties["languageEmbedding"].ToLower() == "never")
                urlOptions.LanguageEmbedding = LanguageEmbedding.Never;
            else if (thisSite.SiteInfo.Properties["languageEmbedding"].ToLower() == "always")
                urlOptions.LanguageEmbedding = LanguageEmbedding.Always;
            else if (thisSite.SiteInfo.Properties["languageEmbedding"].ToLower() == "asneeded")
                urlOptions.LanguageEmbedding = LanguageEmbedding.AsNeeded;
        }

        return urlOptions;
     }
 }
于 2014-04-30T20:17:16.903 回答