Skip to content

Instantly share code, notes, and snippets.

@dommmel
Created September 18, 2012 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dommmel/3743178 to your computer and use it in GitHub Desktop.
Save dommmel/3743178 to your computer and use it in GitHub Desktop.
Localize your greeting message with javascript
##################################################################
# #
# Copyright (C) 2012 Dommmel <dommmel@gmail.com> #
# This code is released under WTFPL, version 2.0. #
# #
##################################################################
# Usage:
# <span id="sayhi">привет</span>
# <script>
# replace_text_with_greeting("#sayhi");
# </script>
# country codes should have the ISO 3166 format.
# See http://en.wikipedia.org/wiki/ISO_3166-2
greeting_for =
"default" : "Hi"
"DE" :
"default" : "Hallo"
"Baden-Wurttemberg" : "Grüß Gottle"
"Bayern" : "Servus"
"Hamburg" : "Moin"
"CH" : "Grüezi"
"AT" : "Habe die Ehre"
"FR" : "Salut"
"US" : "Hello"
replace_text_with_greeting = (selector) ->
$.getJSON "http://freegeoip.net/json/?callback=?", (resp) ->
$(selector).text(
if greeting_for[resp.country_code]?
greeting_for[resp.country_code][resp.region_name] or greeting_for[resp.country_code]["default"] or greeting_for[resp.country_code]
else
greeting_for["default"]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment