timodw/laravel-vue-translation

一个用于在VueJs中实现Laravel翻译功能的包

1.0.1 2021-09-04 09:33 UTC

This package is auto-updated.

Last update: 2024-09-04 16:15:28 UTC


README

Vue 3包,用于在Vue模板中使用Laravel翻译。

开始使用

通过composer安装包

composer require timodw-nl/laravel-vue-translation

通过运行命令发布包资源

php artisan vendor:publish --provider="Timodw\Translation\TranslationServiceProvider"

这将发布位于 resources/js/plugin/translations 目录中的 translations.js 文件

运行artisan命令

php artisan translation:generate

这将编译位于 resources/lang 目录中的所有翻译文件,并生成 resources/js/plugins/translations/translations.json 文件。

此命令在本地环境中每次启动应用程序时也会自动运行。

添加到Vue

import {createApp} from "vue";
import translationsPlugin from "./plugins/translations/translations";

const app = createApp(App);
app.use(translationsPlugin);
app.mount('#app');

如何切换语言?

这将从文档的 lang 属性中获取当前语言

<html lang="en">

如何使用?

想象这是 resources/lang 的目录结构

|--en
   |--auth.php
   |--pagination.php
   |--passwords.php
   |--validation.php
   |--messages.php
|--nl
   |--auth.php
   |--pagination.php
   |--passwords.php
   |--validation.php
   |--messages.php  

messages.php 文件可能如下所示

return [
    'foo' => [
        'bar' => 'Some message'
    ]
];

你可以通过调用 $t 指令来获取值

$t('messages.foo.bar')

使用备用区域设置

要像Laravel中的trans()函数一样交互,请在布局中插入

<meta name="fallback_locale" content="{{ config('app.fallback_locale') }}">

替换属性

不建议使用此包来显示验证错误,但如果需要,你可以通过传递对象作为第二个参数来替换 :attribute, :size 等。

$t('validation.required',{
    attribute:$t('validation.attributes.title')
});

注意:就像在laravel中一样,如果找不到您传递的键的值,它将返回确切的键