如何在以后添加第二语言进行国际化?

时间:2016-04-19 20:58:20

标签: php internationalization propel silex

我有以下架构:

<!-- language: lang-php -->
public function addBlogPost(Application $app) {

    $data = array (
        'title' => $app['request']->get('Title'),
        'language' => $app['request']->get('Language'),
        'short-link' => $this->createShortLink($app, $app['request']->get('Title')),
        'description' => $app['request']->get('Description'),
        'content' => $app['request']->get('Content')
        );

    $blog = new Blog();

    $blog->setLocale($data['language']);
    $blog->setTitle($data['title']);
    $blog->setShortLink($data['short-link']);
    $blog->setDescription($data['description']);
    $blog->setContent($data['content']);

    try {
        $blog->save();
        $app['session']->getFlashBag()->add(
            'success',
            'success_add_blog_text'
           );
            return $app->redirect($app['url_generator']->generate('adminBlog'));
    } catch (PropelException $pe) {
        $app['session']->getFlashBag()->add(
            'error',
            'error_add_blog_text'
            );
        return $app->redirect($app['url_generator']->generate('adminBlog'));
    }
}

当我添加新帖子时,它会添加一种语言(主应用语言),其目的是在以后为更多语言留下选项。

这是添加帖子的代码:

<!-- language: lang-php -->
public function editBlogPost(Application $app) {
    $id = $app['request']->get('id');
    $lang = $app['request']->get('language');
    if(!is_numeric($id)) {
        $app['session']->getFlashBag()->add(
                'error',
                'blog_doesnt_exists_text'
            );
        return $app->redirect($app['url_generator']->generate('adminBlog'));
    }

    $data = array (
        'id' => $app['request']->get('id'),
        'language' => $app['request']->get('language'),
        'title' => $app['request']->get('title'),
        'short-link' => $app['request']->get('short-link'),
        'description' => $app['request']->get('description'),
        'content' => $app['request']->get('content')
        );

    $post = BlogQuery::create();

    $post->setLocale($data['language']);
    $post->setTitle($data['title']);
    $post->setShortLink($data['short-link']);
    $post->setDescription($data['description']);
    $post->setContent($data['content']);

    try {
        $post->save();
        $app['session']->getFlashBag()->add(
            'success',
            'success_edit_blog_text'
            );
        return $app->redirect($app['url_generator']->generate('adminBlog'));
    } catch (PropelException $pe) {
        $app['session']->getFlashBag()->add(
            'error',
            'error_edit_blog_text'
            );
        return $app->redirect($app['url_generator']->generate('adminBlog'));
    }
}

直到这里所有工作都按照预期的方式工作,但当我尝试执行编辑时,无论使用何种语言,它都无法正常工作。

{% for description in descriptions %}
     <td>
        {{ description.productDesciption }}
     </td>
{% else %}
     <td> 
        <a href = "{{ path('description') }}">Create a Description for this Product</a>
    </td>
{% endfor %}
  

错误500。

     

未定义的方法ModelCriteria :: setLocale()

是我得到的。我应该使用Blogl18nQuery进行编辑吗?或者我从一开始就做错了吗?

1 个答案:

答案 0 :(得分:0)

经过一些测试后确定(由于缺乏执行复杂搜索以获得答案的意愿,我不得不承认)

这就是我所做的并且它有效(它可能不是最好的解决方案,但现在可以使用)。

<!-- language: lang-php -->
public function editBlogPost(Application $app) {
    $id = $app['request']->get('id');
    $lang = $app['request']->get('language');
    if(!is_numeric($id)) {
        $app['session']->getFlashBag()->add(
                'error',
                'blog_doesnt_exists_text'
            );
        return $app->redirect($app['url_generator']->generate('adminBlog'));
    }

    // check if the item exists, if it doesnt, error.
    $blog = BlogQuery::create()
        ->filterById($id)
        ->count();
    if($blog<1) {
        $app['session']->getFlashBag()->add(
            'error',
            'blog_doesnt_exists_text'
            );
        return $app->redirect($app['url_generator']->generate('adminBlog'));
    }

    $data = array (
        'id' => $app['request']->get('id'),
        'language' => $app['request']->get('language'),
        'title' => $app['request']->get('Title'),
        'short-link' => $app['request']->get('ShortLink'),
        'description' => $app['request']->get('Description'),
        'content' => $app['request']->get('Content')
        );

    $post = new Blogi18n();
    $post->setId($data['id']);
    $post->setLocale($data['language']);
    $post->setTitle($data['title']);
    $post->setShortLink($data['short-link']);
    $post->setDescription($data['description']);
    $post->setContent($data['content']);

    try {
        $post->save();
        $app['session']->getFlashBag()->add(
            'success',
            'success_edit_blog_text'
            );
        return $app->redirect($app['url_generator']->generate('adminBlog'));
    } catch (PropelException $pe) {
        $app['session']->getFlashBag()->add(
            'error',
            'error_edit_blog_text'
            );
        return $app->redirect($app['url_generator']->generate('adminBlog'));
    }

}

我选择使用Blogi18n,这就是诀窍。