Bridging the Language Barrier for Gamers

Using PubSub and Translation API’s

Jon Foust
4 min readJan 14, 2020

After creating the chat app described in my previous post, I decided to continue adding features to it. In that post, I dive into using Cloud PubSub and creating a simple app by navigating the Cloud Console. I’ve been playing more multiplayer games and once again was inspired to add some new cool things to the chat app. We saw that Google Cloud makes it easy to add one-to-one chat to a multiplayer game… but you won’t want to stop there. You’re used to the finer things in life and you’re not going to be satisfied unless we go further. So let’s push past possible to awesome! We will focus on connecting the chat with thousands of other users (and you won’t be talking to yourself any more) and utilizing ML to make things a bit more universal.

Now that we’ve got chat rolling by following along with my previous post, it’s time to add more subscribers! In that post, we had one subscriber listening to one topic. If you want to add additional subscribers or players, instead of subscribing to the same subscription for the topic, each subscriber must create a subscription for the topic. This is a good option. Otherwise, load balancing happens and subscribers will essentially get messages as they come. What promptly happens when you add more subscribers to the same subscription is that each subscriber receives a subset of the messages — definitely not ideal.This is the bad option.Players will receive scattered messages that seem to not make sense and only one teammate will potentially know the enemy is on the left.

Here, subscriber Y and Z both have separate subscriptions to Topic C. If they had not, they would receive only a subset of the messages. To avoid that, we have to create a new subscription programmatically in each client to the topic. You can do that in various ways but here is an example:

(Assuming you have a username to reference)

// Append “UGC_Sub” to username to create new subscription name
string subName = username + “UGC_Sub”;
// Subscribe to the topic.
SubscriberServiceApiClient subscriber = SubscriberServiceApiClient.Create();
SubscriptionName subscriptionName = new SubscriptionName(“univgamechat”, subName); // Second Addition
try
{
subscriber.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 60);
Console.WriteLine($”Subscription {subscriptionName.SubscriptionId} created.”);
}
catch (Grpc.Core.RpcException e)
when (e.Status.StatusCode == Grpc.Core.StatusCode.AlreadyExists)
{
Console.WriteLine($”Subscription {subscriptionName} already exists.”);
}

Output to be expected:

Great! You’ve made your chat available to a larger audience and each user will be able to receive all messages but what if someone doesn’t speak your language? What if they’re afraid of speaking another language in fear of mistakenly insulting someone or being insulted themselves? What if you just want everyone to feel comfortable? Fear not. I will show you how technology has advanced so quickly that we’re on the brink of living in a sci-fi movie.

PubsubMessage message = new PubsubMessage
{
// The data is any arbitrary ByteString. Here, we’re using text.
Data = ByteString.CopyFromUtf8(“Hello, Pubsub”),
// The attributes provide metadata in a string-to-string dictionary.
Attributes =
{
{ “User”, username }
}
};// Create Translation Client
var translationClient = TranslationClient.Create();
// Translate to Spanish
Console.WriteLine($”Received message {msg.MessageId} published at {msg.PublishTime.ToDateTime()} from {msg.Attributes[“User”]} translated to Spanish”);
Console.WriteLine($”{msg.Attributes[“User”]}: ‘{translationClient.TranslateText(msg.Data.ToStringUtf8(), “es”).TranslatedText}’”);

Thanks to these five (bolded) lines of code(!), your game is now richer and you have a huge global community that can enjoy an inclusive chat experience. Let that sink in for a moment. We don’t take enough time to appreciate how easy today’s tools are. If your expectations are based on the days when mobile phones were large enough to count as exercise equipment, you still expect to put in a lot of fuss for a bit of dazzle. But as the developer community grows and builds on one another’s work, the universe of what’s possible expands quickly. Expect things to be a little easier than you think. Never be scared to dive in!

--

--

Jon Foust

Developer Advocate @ Google. Gamer. Maker. Trying to make things easier for others by doing the hard stuff myself. Opinions are my own.