Ko Laravel he tono i hangaia kia noho rohe, kia urutau ki nga momo reo me nga tikanga. Ko te whakangao i nga tono mo te ao ki tetahi reo motuhake ma te whakamaoritanga.
localhost
. Ki te kore, whakakapi localhost
me to ake ingoa rohe, wahitau IP ranei (i runga i to whakaurunga).I roto i a Laravel, pera i etahi atu anga, ka taea e taatau te penapena whakamaoritanga mo nga reo rereke i roto i nga konae motuhake. E rua nga huarahi hei whakarite i nga konae whakamaori Laravel:
resources/lang/{en,fr,ru}/{myfile.php}
;resources/lang/{fr.json, ru.json}
;Mo nga reo rereke i te rohe, me whakaingoatia e koe directory/file
o te reo e ai ki te ISO 15897. Hei tauira, mo te UK English ka whakamahia e koe en_GB
Tuhinga o mua en-gb
. I roto i tenei tuhinga, ka aro tatou ki te huarahi tuarua, engari he pera ano mo te tuatahi (haunga mo te whakaingoatanga me te tiki mai o nga taviri whakamaori).
Na, me haere tatou ki te resources/views/welcome.blade.php
kōnae ka whakakapi i nga ihirangi o te body
tohu ki a maatau, penei:
<body class="antialiased">
<div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center py-4 sm:pt-0">
<div class="max-w-6xl mx-auto sm:px-6 lg:px-8">
<div class="flex justify-center pt-8 sm:justify-start sm:pt-0">
Welcome to our website
</div>
</div>
</div>
</body>
Ka timata tatou ma te whakarite i ta maatau panui powhiri, he tino ngawari ki Laravel. Ko nga mea katoa ka mahia e koe ko te whakakapi i te tuhinga "Nau mai ki to maatau paetukutuku" me te waehere e whai ake nei: {{ __('Welcome to our website') }}
. Ma tenei ka ako a Laravel ki te whakaatu "Nau mai ki to maatau paetukutuku" ma te taunoadefime te titiro mo nga whakamaoritanga o tenei aho mena ka whakatauhia he reo ke atu i te reo Ingarihi (ka tae ki tera waa). Ka whakatauhia te reo Ingarihi hei reo taunoadefinish o to tatou taupānga, na i te tautuhinga taunoadefiI te mutunga ka whakaatu noa matou i te kupu "Nau mai ki to maatau paetukutuku". Mena he rereke te waahi, ka ngana taatau ki te rapu i te whakamaoritanga e rite ana ka hangahia i roto i te waa.
Engari me pehea e mohio ai a Laravel ko wai te reo o naianei, ko nga reo ranei e waatea ana i roto i te tono? Ka mahia tenei ma te titiro ki te whirihoranga rohe i roto i te taupānga config/app.php
. Whakatuwheratia tenei konae ka rapu i enei taviri hurangi hono e rua:
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
'locale' => 'en',
/*
|--------------------------------------------------------------------------
| Application Fallback Locale
|--------------------------------------------------------------------------
|
| The fallback locale determines the locale to use when the current one
| is not available. You may change the value to correspond to any of
| the language folders that are provided through your application.
|
*/
'fallback_locale' => 'en',
Ko nga whakaahuatanga e whakaatuhia ana i runga ake o nga taviri me whakamarama ake, engari i te poto, ko te matua locale
kei roto i te mua rohedefite mutunga o to tono (i te iti rawa, mena karekau he waahi kua tohua ki roto i te waehere). Na te fallback_locale
ka whakahohehia mena ka whakatauhia e matou he waahi kore i roto i ta maatau tono.
I a matou e tuwhera ana tenei konae, me taapiri he taviri hou mo to maatau whakararangi i nga waahi katoa ka tautokohia e to maatau tono. Ka whakamahia e matou i muri mai ka taapirihia he kaihuri rohe. Heoi, he mahi whiriwhiri tenei na te mea kaore a Laravel e tono kia mahia e matou.
/*
|--------------------------------------------------------------------------
| Available locales
|--------------------------------------------------------------------------
|
| List all locales that your application works with
|
*/
'available_locales' => [
'English' => 'en',
'Italian' => 'it',
'French' => 'fr',
],
Inaianei kei te tautoko ta maatau tono i nga reo e toru: Ingarihi, Itari me Wīwī.
Inaianei kua whakatauhia e matou nga waahi katoa ka mahi tahi matou, ka taea e matou te haere whakamua ki te whakamaori i ta matou karere powhiri.defiahiahi.
Me timata ma te taapiri i nga konae waahi hou ki te kōpaki resources/lang
. Tuatahi, hanga he kōnae resources/lang/it.json
me te taapiri i nga whakamaoritanga e rite ana, penei:
{
"Welcome to our website": "Benvenuto nel nostro sito web"
}
Muri iho, hanga he konae resources/lang/fr.json
:
{
"Nau mai ki to maatau paetukutuku": “Nau mai ki to maatau pae”
}
Ka kite koe, ka korero tonu matou ki te panui o muadefinito i tapiritia e matou ki te konae welcome.blade.php
(i ko {{ __('Welcome to our website') }}
). Ko te take kaore matou e whai ki te hanga i tetahi konae en.json
na te mea kua mohio a Laravel ko ehea nga karere ka tukuna e tatou i mua i te whakatakotorangadefioti i te mahi __()
mo to tatou rohe muadefitenei en.
I tenei wa, kare a Laravel e mohio ki te whakarereke i nga waahi, no reira mo tenei wa, me mahi nga whakamaoritanga ki roto tonu i te huarahi. Whakarerekehia te ara powhiri i muadefikua oti te whakaatu i raro nei:
Route::get('/{locale?}', function ($locale = null) {
if (isset($locale) && in_array($locale, config('app.available_locales'))) {
app()->setLocale($locale);
}
return view('welcome');
});
Ka taea e taatau te toro atu ki ta maatau paetukutuku, me te tohu i tetahi o nga reo e waatea ana hei waahanga ara tuatahi: hei tauira, localhost/ru
o localhost/fr
. Me kite koe i nga ihirangi rohe. Mena ka tohua e koe he waahi kaore i te tautokohia, karekau ranei e tohu he waahi, ka whakamahia e Laravel en
na te taunoadefinita.
Ko te huri i te waahi mo ia hononga pae ehara pea i te mea e hiahia ana koe, kare pea i te ahua ma. No reira ka mahia e matou te whakatakotoranga reo na roto i te whakawhiti reo motuhake me te whakamahi i te waahi kaiwhakamahi ki te whakaatu i nga ihirangi kua whakamaoritia. Na reira, hanga he taputapu waenga hou i roto i te app/Http/Middleware/Localization.php
kōnae ma te rere ranei artisan make:middleware Localization
.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
class Localization
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (Session::has('locale')) {
App::setLocale(Session::get('locale'));
}
return $next($request);
}
}
Ma tenei taputapu waenga ka ako a Laravel ki te whakamahi i te waahi kua tohua e te kaiwhakamahi mena kei te waahi tenei whiringa i roto i te waahi.
I te mea e hiahia ana matou kia mahia tenei i runga i nga tono katoa, me taapiri ano ki te tapae o mua o waengadefioti i roto app/http/Kernel.php
ia ia web
rōpū takawaenga:
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Localization::class, /* <--- add this */
],
Whai muri, me taapiri he ara hei huri i te waahi. Kei te whakamahi matou i te ara kati, engari ka taea e koe te whakamahi i te waehere rite tonu i roto i to kaiwhakahaere ki te pai koe:
Route::get('language/{locale}', function ($locale) {
app()->setLocale($locale);
session()->put('locale', $locale);
return redirect()->back();
});
Waihoki, kaua e wareware ki te tango i te takahuri rohe i tapirihia i mua i to maatau ara powhiridefiahiahi:
Route::get('/', function () {
return view('welcome');
});
Kia oti tenei, ko te huarahi anake mo te kaiwhakamahi ki te whakarereke i te reo kua whakaritea inaianei ko te whakauru localhost/language/{locale}
. Ko te locale
Ko te kowhiringa ka penapena i roto i te huihuinga ka tukuna ano nga kaiwhakamahi ki te wahi i ahu mai ai ratou (tirohia te Localization
takawaenga). Hei whakamatau, haere ki localhost/language/ru
(i te mea kei te noho to pihikete wahanga i to kaitirotiro) ka kite koe i nga mea kua whakamaoritia. Ka taea e koe te neke haere noa i te paetukutuku, ka ngana ranei ki te whakahou i te wharangi ka kite kei te mau tonu te reo kua tohua.
Inaianei me hanga e matou tetahi mea ka taea e te kaiwhakamahi te patene ki te huri i te reo, kaua ki te whakauru a-ringa i nga waehere rohe ki te URL. Hei mahi i tenei, ka taapirihia he kaitaki reo tino ngawari. Na reira, hanga he hou resources/views/partials/language_switcher.blade.php
kōnae me te waehere e whai ake nei:
<div class="flex justify-center pt-8 sm:justify-start sm:pt-0">
@foreach($available_locales as $locale_name => $available_locale)
@if($available_locale === $current_locale)
<span class="ml-2 mr-2 text-gray-700">{{ $locale_name }}</span>
@else
<a class="ml-1 underline ml-2 mr-2" href="language/{{ $available_locale }}">
<span>{{ $locale_name }}</span>
</a>
@endif
@endforeach
</div>
Whakauruhia te kaitahuri hou i hangaia ki te tirohanga "nau mai":
<body class="antialiased">
<div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center py-4 sm:pt-0">
<div class="max-w-6xl mx-auto sm:px-6 lg:px-8">
@include('partials/language_switcher')
<div class="flex justify-center pt-8 sm:justify-start sm:pt-0">
{{ __('Welcome to our website') }}
</div>
</div>
</div>
</body>
Whakatuwheratia te app/Providers/AppServiceProvider.php
konae me te taapiri i te waehere hei tiri i te wa e titohia ai to tatou kaiwhakawhiti reo. Inaa, ka tohatohahia e matou te waahi o naianei ka taea te uru hei konae {{ $current_locale }}
.
Ka mahi tahi tatou me resources/views/welcome.blade.php
, no reira me puta nga mea katoa i roto i ta maatau tirohanga ki te kore e tohua.
Hei tauira, me oha atu ki to tatou kaiwhakamahi pohewa (Amanda) kaua e whakaatu noa i te karere whanui:
{{ __('Welcome to our website, :Name', ['name' => 'caroline']) }}
Kia mahara i whakamahia e matou te ingoa me te reta tuatahi i roto i te puriki iti, engari ko te waahi me te reta tuatahi kei roto i te pumatua. Ma tenei ara, ka taea e Laravel te awhina i a koe ki te whakahuahua aunoa i te kupu tuturu. Ka tupu tenei ki te timata te kaipupuri waahi ki te reta matua, hei tauira, :Name
ka whakaputa i te "Caroline" he kupu matua ranei, :NAME
, ka whakaputa "CAROLINE".
Ka whakahou ano i a maatau konae whakamaori resources/lang/fr.json
e resources/lang/it.json
, i tenei wa ka kite noa tatou i te putanga reo Ingarihi ki nga waahi katoa na te mea kaore nga kii whakamaori e taurite ki nga whakamaoritanga.
Wīwī:
{
"Welcome to our website, :Name": "Bienvenue sur notre site, :Name"
}
Italiana:
{
"Welcome to our website, :Name": "Benvenuto sul nostro sito web, :Name"
}
Hei kite i te mahi maha, me taapiri he kowae kuputuhi hou.
Hei mahi maha, me whakamahi koe i te mahi trans_choice
Tuhinga o mua __()
, hei tauira:
{{ __('Welcome to our website, :Name', ['name' => 'caroline']) }}
<br>
{{ trans_choice('There is one apple|There are many apples', 2) }}
Ka taea e koe te kite, ka wehea nga ahua maha ki te a |
.
Inaianei, he aha mena ka hiahia tatou ki nga momo maha?
Ka taea hoki tenei:
{{ trans_choice('{0} There :form no apples|{1} There :form just :count apple|[2,19] There :form :count apples', 24) }}
I tenei take, ka whakaaetia e matou nga nama 0
, 1
Tuhinga ka whai mai 2
a 19
, ka mutu mai i te 20 haere ake. Ko te tikanga, ka taea e koe te taapiri i nga ture maha e hiahia ana koe.
Ka pehea mena kei te pirangi tatou ki nga kaipupuri waahi ki a tatou ahua maha?
{{ trans_choice('{0} There :form no apples|{1} There :form just :count apple|[2,19] There :form :count apples', 24, ['form' => 'is']) }}
Ka taea hoki te whakamahi i te tatau i tukuna i roto i te `trans_choice` ki te hiahiatia ma te whakamahi i tetahi waahi :count
motuhake:
{{ trans_choice('{0} There :form no apples|{1} There :form just :count apple|[2,19] There :form :count apples', 1, ['form' => 'is']) }}
Ka mutu, kaua e wareware ki te whakahōu i ō kōnae whakamāori me ngā huringa i mahia e koe ki te whakamāoritanga taketake.
Italiana:
{
"Welcome to our website, :Name": "Benvenuto nel nostro sito, :Name",
"{0} There :form no apples|{1} There :form just :count apple|[2,19] There :form :count apples": "{0} Nessuna mela|{1} C'è:count mela|[2,19] Ci sono :count mele"
}
Wīwī:
{
"Welcome to our website, :Name": "Bienvenue sur notre site, :Name",
"{0} There :form no apples|{1} There :form just :count apple|[2,19] There :form :count apples": "{0} Il n'y a pas de pommes|{1} Il n'y :form :count pomme|[2,19] Il y :form :count pommes"
}
Hei kimi i nga ra, ka whakamahia e matou te mana o waro , ka tae mai me Laravel ma te taunoadefinita. Tirohia te Tuhinga waro ; ka taea e koe te mahi i te maha o nga mea whakamere. Hei tauira, ka taea e tatou te whakarite i to tatou rohe me nga ture ra me te wa.
Mo ta maatau tauira ngawari, ka whakaatuhia e matou te ra o naianei kua tohua mo te reo kua tohua. I roto i to tatou routes/web.php
, ka whakahouhia e matou te ara wharangi powhiri me te tuku i te karere ra kua tohua ki a matou view
nau mai:
<?php
Route::get('/', function () {
$today = \Carbon\Carbon::now()
->settings(
[
'locale' => app()->getLocale(),
]
);
// LL is macro placeholder for MMMM D, YYYY (you could write same as dddd, MMMM D, YYYY)
$dateMessage = $today->isoFormat('dddd, LL');
return view('welcome', [
'date_message' => $dateMessage
]);
});
Kia whakahou ake resources/views/welcome.blade.php
te taapiri i te whakaaturanga ra, penei:
{{ __('Welcome to our website, :Name', ['name' => 'amanda']) }}
<br>
{{ trans_choice('{0} There :form :count apples|{1} There :form just :count apple|[2,19] There :form :count apples', 1, ['form' => 'is']) }}
<br>
{{ $date_message }}
E ngana ana ki te huri i te reo i te wharangi kainga o localhost
, ka kite tatou kua roherohia nga ra inaianei, hei tauira:
I nga whenua rereke, ka whakamahi nga tangata i nga whakatakotoranga rereke hei tohu i nga tau, hei tauira:
Na reira, ki te whakaata i enei rereketanga i roto i to taupānga Laravel, ka taea e koe te whakamahi Tauwhakaahua i roto i te ara e whai ake nei:
<?php
$num = NumberFormatter::create('en_US', NumberFormatter::DECIMAL);
$num2 = NumberFormatter::create('fr', NumberFormatter::DECIMAL);
Ka taea hoki e koe te tuhi i te nama ki tetahi reo me te whakaatu i tetahi mea penei "kotahi rau rua tekau ma toru mano kotahi rau rua tekau ma toru ira kotahi rua":
<?php
$num = NumberFormatter::create('en_US', NumberFormatter::SPELLOUT);
$num2 = NumberFormatter::create('fr', NumberFormatter::SPELLOUT);
I tua atu, ka taea e NumberFormatter te kimi i nga moni, hei tauira:
<?php
$currency1 = NumberFormatter::create('fr', NumberFormatter::CURRENCY);
$currency2 = NumberFormatter::create('en_US', NumberFormatter::CURRENCY);
Na mo fr
ka kite koe i te Euro, i te mea mo en_US
ka noho te moni ki nga taara US.
Ercole Palmeri
I te Mane kua hipa, i panuitia e te Financial Times tetahi mahi me OpenAI. Ka raihana a FT i tana kairipoata o te ao…
E hia miriona nga taangata e utu ana mo nga ratonga rerema, e utu ana i nga utu ohaurunga ia marama. Ko te whakaaro noa ko koe…
Ka whakarato tonu a Coveware na Veeam i nga ratonga whakautu mai i nga aitua. Ka tukuna e Coveware nga mahi whakawai me te whakatikatika…
Ko te tiaki matapae kei te huri haere i te waahanga hinu me te hau, me te huarahi auaha me te kaha ki te whakahaere tipu.…