{"id":8381,"date":"2022-07-18T06:16:59","date_gmt":"2022-07-17T21:16:59","guid":{"rendered":"https:\/\/yumishin.com\/?p=8381"},"modified":"2022-07-18T06:16:59","modified_gmt":"2022-07-17T21:16:59","slug":"i18n","status":"publish","type":"post","link":"https:\/\/yumishin.com\/?p=8381","title":{"rendered":"Rails\u3067i18n\u306e\u591a\u8a00\u8a9e\u5bfe\u5fdc\uff08\u30e6\u30fc\u30b6\u30fc\u304c\u9078\u629e\u3057\u305f\u8a00\u8a9e\u3092\u53cd\u6620\uff09"},"content":{"rendered":"<h2 id=\"i-0\">Rails\u3067i18n\u306e\u591a\u8a00\u8a9e\u5bfe\u5fdc\uff08\u30e6\u30fc\u30b6\u30fc\u304c\u9078\u629e\u3057\u305f\u8a00\u8a9e\u3092\u53cd\u6620\uff09<\/h2>\n<h3 id=\"i-1\">\u7d50\u8ad6<\/h3>\n<p>Rails\u30ac\u30a4\u30c9\u306b\u57fa\u672c\u306f\u5168\u3066\u66f8\u3044\u3066\u3042\u308b\u306e\u3067\u3001\u305d\u306e\u901a\u308a\u4f5c\u6210\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u5168\u4f53\u50cf\u3068\u3057\u3066\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u30e6\u30fc\u30b6\u30fc\u306blangage\u3092\u4fdd\u5b58\u3057\u3066\u304a\u304f\u30ab\u30e9\u30e0\u3092\u6301\u305f\u305b\u3066\u304a\u304f\u3002<\/li>\n<li>application.rb\u306b\u5fc5\u8981\u306a\u8a18\u8f09\u3092\u66f8\u304f\u3002<\/li>\n<li>application_controller.rb\u306b\u5fc5\u8981\u306a\u8a18\u8f09\u3092\u66f8\u304f\u3002<\/li>\n<\/ul>\n<h3 id=\"i-2\">\u624b\u9806<\/h3>\n<h4 id=\"i-3\">\u30e6\u30fc\u30b6\u30fc\u306blangage\u3092\u4fdd\u5b58\u3057\u3066\u304a\u304f\u30ab\u30e9\u30e0\u3092\u6301\u305f\u305b\u3066\u304a\u304f<\/h4>\n<p>\u307e\u305a\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u9078\u629e\u3057\u305f\u8a00\u8a9e\u3092\u53cd\u6620\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u306b\u8a00\u8a9e\u8a2d\u5b9a\u3092\u6301\u305f\u305b\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u65e5\u672c\u8a9e\u306a\u3089ja\u3001\u82f1\u8a9e\u306a\u3089en\u306a\u3069\u3067\u3059\u306d\u3002<\/p>\n<p>\u30e6\u30fc\u30b6\u30fc\u30c6\u30fc\u30d6\u30eb\u306e\u30ab\u30e9\u30e0\u306blangage\u306a\u3069\u306e\u30ab\u30e9\u30e0\u3092\u7528\u610f\u3057\u3066\u3001\u305d\u3053\u306b\u8a00\u8a9e\u8a2d\u5b9a\u3092\u4fdd\u5b58\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<h4 id=\"i-4\">application.rb\u306b\u5fc5\u8981\u306a\u8a18\u8f09\u3092\u66f8\u304f<\/h4>\n<p>\u6b21\u306bapplication.rb\u306b\u5fc5\u8981\u306a\u8a18\u8f09\u3092\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> application.rb<\/span><\/p>\n<pre><code>\n    # \u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u4f7f\u7528\u3059\u308b\u8a00\u8a9e\u3092\u6307\u5b9a\n    config.i18n.default_locale = :ja\n\n    # \u3069\u3053\u306b\u7ffb\u8a33\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u308b\u304b\u3068\u3044\u3046\u306e\u3092\u3001I18n\u306b\u6559\u3048\u3066\u3042\u3052\u308b\n    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]\n    # \u8a00\u8a9e\u306f\u3053\u308c\u3060\u3051\u3060\u3088\u3068\u3044\u3046\u306e\u3092\u3001I18n\u306b\u6559\u3048\u3066\u3042\u3052\u308b\uff08\u30db\u30ef\u30a4\u30c8\u30ea\u30b9\u30c8\u7684\u306a\u3082\u306e\uff09\n    config.i18n.available_locales = %i(ja en)\n\n    # \u30db\u30ef\u30a4\u30c8\u30ea\u30b9\u30c8\u306b\u306a\u3044\u8a00\u8a9e\u8a2d\u5b9a\u3055\u308c\u305f\u3089\u3001\u30a8\u30e9\u30fc\u306b\u3059\u308b\n    config.i18n.enforce_available_locales = true<\/code><\/pre>\n<\/div>\n<h4 id=\"i-5\">application_controller.rb\u306b\u5fc5\u8981\u306a\u8a18\u8f09\u3092\u66f8\u304f<\/h4>\n<p>\u6700\u5f8c\u306bapplication_controller.rb\u306b\u5fc5\u8981\u306a\u8a18\u8f09\u3092\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002<\/p>\n<div class=\"pre_tag\"><span><i class=\"fa fa-code\"><\/i> application_controller.rb<\/span><\/p>\n<pre><code>around_action :switch_locale # before action\u3067\u306f\u3046\u307e\u304f\u52d5\u304d\u307e\u305b\u3093\uff01\n\ndef switch_locale(&amp;action)\n  locale = current_user.try(:langage) || I18n.default_locale # \u73fe\u5728\u306e\u30e6\u30fc\u30b6\u30fc\u306elangage\u30ab\u30e9\u30e0\u306e\u5024\u3092\u4f7f\u3063\u3066locale\u3092\u8a2d\u5b9a\u3059\u308b\u3068\u3044\u3046\u8a18\u8ff0\n  I18n.with_locale(locale, &amp;action) # with_locale\u3092\u4f7f\u3046\u3068locale\u7ba1\u7406\u3092\u3088\u3057\u306a\u306b\u3084\u3063\u3066\u304f\u308c\u308b\nend<\/code><\/pre>\n<\/div>\n<p>\u3055\u3059\u304cRails\u306e\u30ec\u30fc\u30eb\u306b\u306e\u3063\u305f\u3060\u3051\u3042\u3063\u3066\u3001\u3001\u3001\u7c21\u5358\u3067\u3059\u3002\u3002\u3002<\/p>\n<p>\u3084\u3063\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3001\u3067\u306f\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rails\u3067i18n\u306e\u591a\u8a00\u8a9e\u5bfe\u5fdc\uff08\u30e6\u30fc\u30b6\u30fc\u304c\u9078\u629e\u3057\u305f\u8a00\u8a9e\u3092\u53cd\u6620\uff09 \u7d50\u8ad6 Rails\u30ac\u30a4\u30c9\u306b\u57fa\u672c\u306f\u5168\u3066\u66f8\u3044\u3066\u3042\u308b\u306e\u3067\u3001\u305d\u306e\u901a\u308a\u4f5c\u6210\u3057\u3066\u3044\u304d\u307e\u3059\u3002 \u5168\u4f53\u50cf\u3068\u3057\u3066\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002 \u30e6\u30fc\u30b6\u30fc\u306blangage\u3092\u4fdd\u5b58\u3057\u3066\u304a\u304f\u30ab\u30e9\u30e0 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":8382,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[256],"tags":[],"class_list":{"0":"post-8381","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-it","8":"entry"},"_links":{"self":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8381"}],"version-history":[{"count":1,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8381\/revisions"}],"predecessor-version":[{"id":8383,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/posts\/8381\/revisions\/8383"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=\/wp\/v2\/media\/8382"}],"wp:attachment":[{"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yumishin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}