简体   繁体   English

Laravel 编程中的字符串资源?

[英]String Resource in laravel programming?

is there any way that instead of hard coding strings in the blade you use something like a string resource(or string table) so if you change a string every blade will be changed .有什么方法可以代替刀片中的硬编码字符串,而是使用字符串资源(或字符串表)之类的东西,因此如果您更改字符串,则每个刀片都会更改。 For example instead of hoard coding "product" in the blade I use a variable called "string1" so if one day I change string1 value to "service" every blade will be affected and fine.例如,我使用一个名为“string1”的变量而不是在刀片中囤积编码“产品”,因此如果有一天我将 string1 值更改为“服务”,每个刀片都会受到影响并且很好。

为此,您可以(错误地)使用这样的翻译系统。

I am new to laravel but here is a idea I have.我是 laravel 的新手,但我有一个想法。

create myString.php which contains your variables创建包含变量的 myString.php

example:例子:

<?php
 $string1 = "products";
?>

in your files在你的文件中

<?php
 inlude_once("myString.php");
 {{ $string1 }}
?>

I hope this helped you to come up with a better solution.我希望这可以帮助您提出更好的解决方案。

good luck.祝你好运。

maybe use localization with language files, so you can use也许对语言文件使用本地化,所以你可以使用

{{ trans('string1') }}

in a blade view.在刀片视图中。 more info: https://laravel.com/docs/5.0/localization更多信息: https : //laravel.com/docs/5.0/localization

and these 2 functions和这两个功能

trans
Translate a given language line. Alias of Lang::get.

$value = trans('validation.required'):


trans_choice
Translate a given language line with inflection. Alias of Lang::choice.

$value = trans_choice('foo.bar', $count);

As of Laravel 5.4, you can use the double underscore:从 Laravel 5.4 开始,您可以使用双下划线:

{{ __('Register') }}

What is nice about this is that you don't need to do anything else for it to output (in this case) “Register”.这样做的好处是你不需要做任何其他事情来输出(在这种情况下)“注册”。

You can change what value is displayed either in the appropriate messages.php file or by creating a json file, as described in the docs .您可以更改在相应的 messages.php 文件中或通过创建 json 文件中显示的值,如docs中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM