dongyou7292 2014-12-20 11:04
浏览 68
已采纳

如何使我的CMS多语言

I have made a CMS using PHP and MYSQL for the back-end. The problem is, I'd like to have the site available in multiple languages; So I've already translated the CMS itself and put it in files like:

  • en.ini
  • nl.ini

Which works fine for the CMS' contents itself, like the Administration etc. The actual problem occurs when I try to translate the website further: The blogposts and pages are stored in a database, so they're dynamic. My pages table's structure looks like this:

urlname | name | id | content | position | hidden | redirect | type | permission

So, if I were to translate a page into, for example, Dutch, I don't want to create a new table called pages_nl for example, because most of the information would be the same as the other table.
I could add a row which contains the page's available languages, and make php read the array and parse if the current $lang matches one of the page's available languages, and then read the content_nl and name_nl row as an example.
The same problem would occur with my blog posts, and I have searched for a solution, but most results were for specific CMSs and just plugins.

I'm asking what would be the best way (and database structure) to store multilingual pages/posts, where languages could be added dynamically and have a fallback on the original content if there's no translated version available.

  • 写回答

1条回答 默认 最新

  • douqiang6448 2014-12-20 11:24
    关注

    Basically you add a lang-key to your database

    urlname | name | id | content | position | hidden | redirect | type | permission | lang
    

    Now lets say a request comes for a page with 5 content elements in it. Your CMS first search for the entries with the specific lang-code, for example nl. If it dont find an entry with the language code it falls back to the default language and take that entry.

    Let say you have translated Content 1, 3 and 5:

    Content 1       Content 1 nl
    Content 2
    Content 3       Content 3 nl
    Content 4
    Content 5       Content 5 nl
    
    Result:
    
    Content 1 nl
    Content 2
    Content 3 nl
    Content 4
    Content 5 nl
    

    Another way would be to do the translation in the template files (like magento does it). In magento you have something like that:

    "Hello, this is the " . $this->__("English") . " page"
    

    And translation files. For example lang.nl:

    "English", "Dutch"
    

    In this case you will have only 1 Content Element, but you will wrap every element that shall be translated. This also works with images and everything you want. You can also abstract this wrapper to work in a backend RTE-Editor. For example, you will write Hello, this is the English page in your backend then mark Englishand click the translation button. This will add a Tag around English and when you render the content you scan for this tags and replace it if you find any translation for this string in the requested language.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵