{"id":398,"date":"2018-03-31T00:38:01","date_gmt":"2018-03-30T15:38:01","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=398"},"modified":"2018-03-31T00:38:01","modified_gmt":"2018-03-30T15:38:01","slug":"%e3%82%b7%e3%83%b3%e3%83%97%e3%83%ab%ef%bc%81flask%e3%82%92%e5%a4%9a%e8%a8%80%e8%aa%9e%e5%8c%96%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=398","title":{"rendered":"\u30b7\u30f3\u30d7\u30eb\uff01flask\u3092\u591a\u8a00\u8a9e\u5316\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u306e\u524d\u300c<a href=\"https:\/\/blog.capilano-fw.com\/?p=374\">\u610f\u5916\u3068\u7c21\u5358\uff01Flask\u3092HTTPS\u5bfe\u5fdc\u3059\u308b\u65b9\u6cd5<\/a>\u300d\u3068\u3044\u3046\u8a18\u4e8b\u3092\u6295\u7a3f\u3057\u305f\u3068\u304a\u308a\u3001\u73fe\u5728<strong>python\u306e\u8efd\u91cf\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30fbflask<\/strong>\u3067\u30b5\u30a4\u30c8\u958b\u767a\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3059\u3067\u306b\u5fc5\u8981\u306a\u6a5f\u80fd\u306e\u958b\u767a\u306f\u307b\u307c\u5b8c\u4e86\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u6700\u5f8c\u306b\u6b8b\u3063\u305f\u300c\u591a\u8a00\u8a9e\u5316\u300d\u3092\u3057\u3088\u3046\u3068\u30cd\u30c3\u30c8\u4e0a\u3092\u63a2\u3057\u3066\u307f\u305f\u3068\u3053\u308d\u300cFlask-Babel\u300d\u3092\u4f7f\u3048\u3070\u3044\u3044\u3068\u306e\u3053\u3068\u3060\u3063\u305f\u3093\u3067\u3001pip\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u305f\u3068\u3053\u308d\u3001<\/p>\n<pre><strong>flask.ext.babel is deprecated, use flask_babel instead.<\/strong><\/pre>\n<p>\u3068\u3044\u3046\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3002\u5185\u5bb9\u306f\u3001<\/p>\n<p><strong style=\"font-size: 35px;\">flask.ext.babel\u306f\u3082\u3046\u4f7f\u308f\u308c\u3066\u306d\u3047\u3093\u3060\u3088\u30fc\u3002\u4ee3\u308f\u308a\u306bflask_babel\u3092\u4f7f\u3044\u306a\uff01<\/strong><\/p>\n<p>\u3068\u8a00\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u4ed5\u65b9\u306a\u304f\u3001flask_babel\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u307f\u308b\u3068\u3001\u4eca\u5ea6\u306f<\/p>\n<p><strong style=\"font-size: 35px;\">\u305d\u3093\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u306d\u3047\u3088\uff01<\/strong><\/p>\n<p>\u3068\u8a00\u308f\u308c\u308b\u59cb\u672b\u3002\u305d\u306e\u5f8c\u3044\u308d\u3044\u308d\u3068\u30c1\u30a7\u30c3\u30af\u3057\u3066\u307f\u305f\u3093\u3067\u3059\u3051\u3069\u3001\u6642\u9593\u3060\u3051\u304c\u904e\u304e\u3066\u3057\u307e\u3063\u3066\u3001\u3064\u3044\u306b\u3001<\/p>\n<p><strong style=\"font-size: 35px;\">\u300c\u3044\u3084\u3044\u3084\u3001\u591a\u8a00\u8a9e\u5316\u306a\u3093\u3066json\u4f5c\u3063\u3066\u8a72\u5f53\u30c7\u30fc\u30bf\u547c\u3073\u51fa\u3059\u3060\u3051\u3058\u3083\u3093\u30fb\u30fb\u30fb\u3082\u3046\u81ea\u5206\u3067\u4f5c\u308b\u308f\uff08\u3077\u3093\u3077\u3093\uff01\uff09\u300d<\/strong><\/p>\n<p>\u3063\u3066\u306a\u3063\u305f\u3093\u3067\u3001\u4eca\u56de\u306f\u300c<strong>\u30b7\u30f3\u30d7\u30eb\u306bflask\u306e\u591a\u8a00\u8a9e\u5316\u3059\u308b\u65b9\u6cd5<\/strong>\u300d\u3092\u307e\u3068\u3081\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u65b9\u6cd5\u3068\u3057\u3066\u306f\u3001Laravel\u306e\u3088\u3046\u306b<strong>lang\/ja\/messages.js<\/strong>\u3068\u3044\u3046\u5f62\u3067\u30c6\u30ad\u30b9\u30c8\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u30fb\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3057\u3066\u304a\u304d\u3001\u4ee5\u4e0b\u306e\u5f62\u3067\u547c\u3073\u51fa\u3059\u3068\u3044\u3046\u30b7\u30f3\u30d7\u30eb\u306a\u3082\u306e\u3067\u3059\u3002<\/p>\n<pre><strong>gettext('messages.app_name')<\/strong><\/pre>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306e\u4f5c\u696d\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h1>\u8a00\u8a9e\u306e\u53d6\u5f97\u30b3\u30fc\u30c9<\/h1>\n<p>\u3068\u3044\u3063\u3066\u3082\u3001\u96e3\u3057\u3044\u3053\u3068\u3092\u3059\u308b\u308f\u3051\u3058\u3083\u306a\u304f\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u30fb\u30d8\u30c3\u30c0\u30fc\u306e\u300cAccept-Language\u300d\u3092\u5206\u89e3\u3057\u3066\u8a00\u8a9e\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3060\u3051\u3002<\/p>\n<p>\u203b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u82f1\u8a9e\u3001\u65e5\u672c\u8a9e\u4ee5\u5916\u306e\u5834\u5408\u306f\u3059\u3079\u3066\u82f1\u8a9e\u306b\u306a\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u3082\u3063\u3068\u8a00\u8a9e\u3092\u5897\u3084\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u5404\u81ea\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<pre><strong>def get_locale():<\/strong>\r\n<strong>    locale = 'en'<\/strong>\r\n<strong>    languages = request.headers.get('Accept-Language').split(',')<\/strong>\r\n<strong>    for language in languages:<\/strong>\r\n<strong>        locale_long = language.split(';')[0]<\/strong>\r\n<strong>        locale = locale_long.split('-')[0]<\/strong>\r\n<strong>        break<\/strong>\r\n<strong>    if locale not in ['ja', 'en']:<\/strong>\r\n<strong>        locale = 'en'<\/strong>\r\n<strong>    return locale.lower()<\/strong><\/pre>\n<h1>\u8a00\u8a9e\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b\u30b3\u30fc\u30c9\u3092\u66f8\u304f<\/h1>\n<pre><strong>def get_text(key):<\/strong>\r\n<strong>    text = ''<\/strong>\r\n<strong>    locale = get_locale()<\/strong>\r\n<strong>    keys = key.split('.')<\/strong>\r\n<strong>    path = APP_DIR +'\/lang\/'+ locale +'\/'+ '\/'.join(keys[:-1]) +'.json'<\/strong>\r\n<strong>    try:<\/strong>\r\n<strong>        with open(path) as f:<\/strong>\r\n<strong>            data = json.load(f)<\/strong>\r\n<strong>            text = data[keys[-1]]<\/strong>\r\n<strong>    except:<\/strong>\r\n<strong>        pass<\/strong>\r\n<strong>    return unicode(text)<\/strong><\/pre>\n<p>\u6b21\u306bjson\u304b\u3089\u8a72\u5f53\u3059\u308b\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002<br \/>\nLaravel\u306e\u3088\u3046\u306b\u300c<strong>messages.app_name<\/strong>\u300d\u3068\u3044\u3046\u5f62\u3067\u30c6\u30ad\u30b9\u30c8\u3092\u547c\u3073\u3060\u305b\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002\uff08\u3068\u3044\u3063\u3066\u3082\u3001\u6587\u5b57\u5217\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u306e\u4ee3\u5165\u306f\u4f7f\u3048\u306a\u3044\u3067\u3059\u3057\u3001\u591a\u91cd\u914d\u5217\u306b\u306f\u5bfe\u5fdc\u3057\u3066\u306f\u3044\u307e\u305b\u3093\u3002\u3042\u304f\u307e\u3067\u30b7\u30f3\u30d7\u30eb\u3067\u3044\u304d\u307e\u3059\uff09<\/p>\n<p>\u6c17\u3092\u3064\u3051\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u3044\u3068\u3053\u308d\u306f\u3001<strong>APP_DIR<\/strong>\u306e\u90e8\u5206\u3067\u3059\u3002\u524d\u306b\u3082\u7d39\u4ecb\u3057\u305f<a href=\"https:\/\/blog.capilano-fw.com\/?p=369\">mod_wsgi<\/a>\u3067\u5b9f\u884c\u3059\u308b\u3068\u76f8\u5bfe\u30d1\u30b9\u304c\u304a\u304b\u3057\u306a\u3053\u3068\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3093\u3067\u3001\u7d76\u5bfe\u30d1\u30b9\u3067\u6307\u5b9a\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3042\u3068\u306f\u3001<strong>return<\/strong>\u306e\u90e8\u5206\u3067\u3059\u3002<br \/>\n\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3060\u3068\u3001\u6587\u5b57\u30b3\u30fc\u30c9\u3067\u30a8\u30e9\u30fc\u306f\u51fa\u306a\u304b\u3063\u305f\u306e\u306b\u3001\u30b5\u30fc\u30d0\u30fc\u306e\u65b9\u3067\u306funicode()\u3092\u304b\u307e\u3057\u3066\u306a\u3044\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<\/p>\n<pre><strong>'ascii' codec can't encode ...<\/strong><\/pre>\n<p>\u3084\u306f\u308apython\u3067\u65e5\u672c\u8a9e\u3092\u4f7f\u3046\u3068\u304d\u306f\u5c11\u3057\u6c17\u3092\u3064\u3051\u306a\u3044\u3068\u30c0\u30e1\u3067\u3059\u306d\uff08\u7b11\uff09<\/p>\n<h1>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304b\u3089\u30c7\u30fc\u30bf\u3092\u547c\u3073\u51fa\u305b\u308b\u3088\u3046\u306b\u3059\u308b<\/h1>\n<p>flask\u3067\u306f\u3001<strong>context_processor<\/strong>\u3092\u4f7f\u3046\u3068\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304b\u3089\u95a2\u6570\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><strong>@app.context_processor<\/strong>\r\n<strong>def utility_processor():<\/strong>\r\n<strong>    return dict(get_text=get_text, get_locale=get_locale)<\/strong><\/pre>\n<p>dict\u306e\u4e2d\u3067\u3001\u547c\u3073\u51fa\u3059\u30ad\u30fc\u3068\u95a2\u6570\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u4e0a\u306e\u4f8b\u3067\u8a00\u3046\u3068\u3001<\/p>\n<pre><strong>&lt;html lang=\"{{ get_locale() }}\"&gt;<\/strong><\/pre>\n<p>\u3068\u3044\u3046\u5f62\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u304c\u3067\u304d\u307e\u3059\u3057\u3001\u809d\u5fc3\u306e\u8a00\u8a9e\u30c6\u30ad\u30b9\u30c8\u3082\u3001<\/p>\n<pre><strong>{{ get_text('messages.app_name') }}<\/strong><\/pre>\n<p>\u3068\u3001\u30b7\u30f3\u30d7\u30eb\u306a\u5f62\u3067\u7f6e\u304d\u63db\u3048\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u4eca\u56de\u306f\u308f\u304b\u308a\u3084\u3059\u304f\u8aac\u660e\u3059\u308b\u305f\u3081\u306bget_text()\u3068\u3044\u3046\u540d\u524d\u306b\u3057\u307e\u3057\u305f\u304c\u3001\u5b9f\u969b\u306b\u306f\u6b21\u306e\u3088\u3046\u306b\u30a2\u30f3\u30c0\u30fc\u30d0\u30fc\u3060\u3051\u306e\u95a2\u6570\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre><strong>{{ _('messages.app_name') }}<\/strong><\/pre>\n<p>\u305d\u306e\u5834\u5408\u306f\u3001context_processor\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>@app.context_processor\r\ndef utility_processor():\r\n    return dict(<strong>_=get_text<\/strong>, get_locale=get_locale)<\/pre>\n<h1>\u8a00\u8a9e\u30de\u30c3\u30d4\u30f3\u30b0json\u3092\u4f5c\u308b<\/h1>\n<p>\u3068\u3044\u3063\u3066\u3082\u3001\u3053\u308c\u3082\u7c21\u5358\u3067\u3001\u4eca\u56de\u306e\u4f8b\u3067\u306f\u3001<\/p>\n<ul>\n<li>lang\/<strong>ja<\/strong>\/messages.js<\/li>\n<li>lang\/<strong>en<\/strong>\/messages.js<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u30d5\u30a9\u30eb\u30c0\uff06\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u3001\u7ffb\u8a33\u30c6\u30ad\u30b9\u30c8\u3092\u66f8\u304d\u8fbc\u3080\u3060\u3051\u3067OK\u3067\u3059\u3002<\/p>\n<pre><strong>{<\/strong>\r\n<strong>    \"app_name\": \"\u30b5\u30a4\u30c8\u540d\",<\/strong>\r\n<strong>    \"top_page\": \"\u30c8\u30c3\u30d7\u30da\u30fc\u30b8\"<\/strong>\r\n<strong>}<\/strong><\/pre>\n<p>\u306a\u306e\u3067\u3001\u3082\u3057\u3082\u3001<\/p>\n<pre><strong>{{ _('errors.requied') }}<\/strong><\/pre>\n<p>\u3068\u3044\u3046\u5f62\u3067\u30c6\u30ad\u30b9\u30c8\u3092\u8868\u793a\u3057\u305f\u3044\u5834\u5408\u306f\u3001<\/p>\n<ul>\n<li>lang\/<strong>ja<\/strong>\/errors.js<\/li>\n<li>lang\/<strong>en<\/strong>\/errors.js<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u540c\u3058\u3088\u3046\u306bjson\u5185\u306b\u30c6\u30ad\u30b9\u30c8\u3092\u304b\u304d\u8fbc\u3093\u3067\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u30b7\u30f3\u30d7\u30eb\u3067\u3057\u305f\u3051\u3069flask\u306e\u591a\u8a00\u8a9e\u5316\u3092\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002\u3082\u3057Laravel\u3060\u3063\u305f\u3089\u4eca\u5f8c\u306e\u3053\u3068\u3092\u8003\u3048\u3066\u30d1\u30c3\u30b1\u30fc\u30b8\u5316\u3057\u3066\u304a\u304f\u3068\u3053\u308d\u306a\u3093\u3067\u3059\u3051\u3069\u3001flask\u306f\u4eca\u5f8c\u305d\u3053\u307e\u3067\u5229\u7528\u983b\u5ea6\u304c\u9ad8\u304f\u306a\u3055\u305d\u3046\u306a\u306e\u3067\u3001\u4eca\u56de\u306f\u3053\u3053\u3067\u7d42\u4e86\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002\uff08Laravel\u306b\u306f\u3082\u3046\u3059\u3067\u306b\u7d20\u6674\u3089\u3057\u3044\u591a\u8a00\u8a9e\u5316\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3057\u306d^^\uff09<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u306e\u524d\u300c\u610f\u5916\u3068\u7c21\u5358\uff01Flask\u3092HTTPS\u5bfe\u5fdc\u3059\u308b\u65b9\u6cd5\u300d\u3068\u3044\u3046\u8a18\u4e8b\u3092\u6295\u7a3f\u3057\u305f\u3068\u304a\u308a\u3001\u73fe\u5728python\u306e\u8efd\u91cf\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u30fbflask\u3067\u30b5\u30a4\u30c8\u958b\u767a\u3092\u884c\u3063 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=398\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u30b7\u30f3\u30d7\u30eb\uff01flask\u3092\u591a\u8a00\u8a9e\u5316\u3059\u308b\u65b9\u6cd5&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":399,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[51],"class_list":["post-398","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-flask"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398"}],"version-history":[{"count":2,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":401,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions\/401"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/399"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}