Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel多言語化対応

 Laravel多言語化対応

क्या यह महसूस कर रही है? सबसे पहले यह थोड़ा प्रयास कर सकता है, लेकिन एक बार जब आप इसे किसी भी तरह इस्तेमाल करते हैं, तो मुझे नहीं लगता कि ऐसा करने में समय लगेगा

kasikasikasi

October 05, 2017
Tweet

More Decks by kasikasikasi

Other Decks in Technology

Transcript

  1. σʔλ σʔλΛొ࿥͢Δͱɺ͜ͷΑ͏ͳσʔλঢ়ଶʹͳΔ id user_id news_id 2 4 5 news_user news_id

    5 news id detail news_id language 6 ͜Μʹͪ͸! 5 ja 7 Hello! 5 en news_lang
  2. σʔλ Ծʹ΋͏Ұݴޠ૿΍ͯ͠ཉ͍͠ͱ͓ئ͍͞Εͯ΋ɾɾɾ id user_id news_id 2 4 5 news_user id

    detail news_id language 6 ͜Μʹͪ͸! 5 ja 7 Hello! 5 en 8 㟬޷! 5 ch news_lang news_id 5 news
  3. σʔλ ϨίʔυΛ௥Ճ͢Δ͚ͩͰෳ਺ݴޠʹରԠ͢Δ͜ͱ͕Ͱ͖Δ id user_id news_id 2 4 5 news_user id

    detail news_id language 6 ͜Μʹͪ͸! 5 ja 7 Hello! 5 en 8 㟬޷! 5 ch ɿ 25 नमस्ते! 5 hi news_lang news_id 5 news
  4. example.com/ja΍/enͷ࣮૷ prefix()Λ௥Ճ͢Δ͚ͩɻ RouterServiceProvider.php protected function mapWebRoutes($request) { $locale = $request->segment(1);

    $this->app->setLocale($locale); Route::middleware('web') ->namespace($this->namespace) ->prefix($locale) ->group(base_path('routes/web.php')); }
  5. example.com/ja΍/enͷ࣮૷ mddlewareͰࢦఆ͞Ε͍ͯͳ͍ݴޠΛϑΟϧλʔ͠Α͏ɻ middleware public function handle($request, Closure $next) { $locale

    = $request->segment(1); if ( ! array_key_exists($locale, $this->app->config->get('app.locales'))) { $segments = $request->segments(); $segments[0] = $this->app->config->get('app.fallback_locale'); return $this->redirector->to(implode('/', $segments)); } $this->app->setLocale($locale); return $next($request); }
  6. example.com/ja΍/enͷ࣮૷ ϦΫΤετΛॻ͖׵͑Δɻͦͯ͠ϦμΠϨΫτɻ LocaleController.php /* ϦΫΤετΛॻ͖׵͍͑ͯΔ͚ͩɻ * ྫ) example.com/en/page -> example.com/ja/page

    */ public function locale(Request $request) { $segments = explode('/', redirect()->back()->getTargetUrl()); $segments[3] = $request->lang; return redirect(implode(‘/',$segments)); }