Game1.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Content;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using System.Globalization;
  6. namespace LocalizationSample
  7. {
  8. /// <summary>
  9. /// This is the main type for your game.
  10. /// </summary>
  11. public class LocalizationGame : Game
  12. {
  13. #region Fields
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16. SpriteFont LocalizedFont;
  17. SpriteFont WPFFont;
  18. Texture2D currentFlag;
  19. #endregion
  20. #region Initialization
  21. public LocalizationGame()
  22. {
  23. Content.RootDirectory = "Content";
  24. graphics = new GraphicsDeviceManager(this);
  25. // Tell the resource manager what language to use when loading strings.
  26. Strings.Culture = CultureInfo.CurrentCulture;
  27. }
  28. /// <summary>
  29. /// Load your graphics content.
  30. /// </summary>
  31. protected override void LoadContent()
  32. {
  33. spriteBatch = new SpriteBatch(GraphicsDevice);
  34. LocalizedFont = Content.Load<SpriteFont>("LocalizedFont");
  35. WPFFont = Content.Load<SpriteFont>("WPFFont");
  36. currentFlag = LoadLocalizedAsset<Texture2D>("Flag");
  37. }
  38. /// <summary>
  39. /// Helper for loading a .xnb asset which can have multiple localized
  40. /// versions for different countries. This allows you localize data such
  41. /// as textures, models, and sound effects.
  42. ///
  43. /// This uses a simple naming convention. If you have a default asset named
  44. /// "Foo", you can provide a specialized French version by calling it
  45. /// "Foo.fr", and a Japanese version called "Foo.ja". You can specialize even
  46. /// further by country as well as language, so if you wanted different assets
  47. /// for the United States vs. United Kingdom, you would add "Foo.en-US" and
  48. /// "Foo.en-GB".
  49. ///
  50. /// This function looks first for the most specialized version of the asset,
  51. /// which includes both language and country. If that does not exist, it looks
  52. /// for a version that only specifies the language. If that still does not
  53. /// exist, it falls back to the original non-localized asset name.
  54. /// </summary>
  55. T LoadLocalizedAsset<T>(string assetName)
  56. {
  57. string[] cultureNames =
  58. {
  59. CultureInfo.CurrentCulture.Name, // eg. "en-US"
  60. CultureInfo.CurrentCulture.TwoLetterISOLanguageName // eg. "en"
  61. };
  62. // Look first for a specialized language-country version of the asset,
  63. // then if that fails, loop back around to see if we can find one that
  64. // specifies just the language without the country part.
  65. foreach (string cultureName in cultureNames)
  66. {
  67. string localizedAssetName = assetName + '.' + cultureName;
  68. try
  69. {
  70. return Content.Load<T>(localizedAssetName);
  71. }
  72. catch (ContentLoadException) { }
  73. }
  74. // If we didn't find any localized asset, fall back to the default name.
  75. return Content.Load<T>(assetName);
  76. }
  77. #endregion
  78. #region Update and Draw
  79. /// <summary>
  80. /// Allows the game to run logic.
  81. /// </summary>
  82. protected override void Update(GameTime gameTime)
  83. {
  84. HandleInput();
  85. base.Update(gameTime);
  86. }
  87. /// <summary>
  88. /// This is called when the game should draw itself.
  89. /// </summary>
  90. protected override void Draw(GameTime gameTime)
  91. {
  92. string string1 = Strings.Welcome;
  93. string string2 = string.Format(Strings.CurrentLocale,
  94. CultureInfo.CurrentCulture.EnglishName,
  95. CultureInfo.CurrentCulture);
  96. string string3 = Strings.HowToChange;
  97. GraphicsDevice.Clear(Color.CornflowerBlue);
  98. spriteBatch.Begin();
  99. spriteBatch.DrawString(LocalizedFont, "Default font Drawing", new Vector2(100, 70), Color.White);
  100. spriteBatch.DrawString(LocalizedFont, string1, new Vector2(100, 100), Color.White);
  101. spriteBatch.DrawString(LocalizedFont, string2, new Vector2(100, 130), Color.White);
  102. spriteBatch.DrawString(LocalizedFont, string3, new Vector2(100, 160), Color.White);
  103. spriteBatch.Draw(currentFlag, new Vector2(100, 210), Color.White);
  104. spriteBatch.DrawString(WPFFont, "WPF font Drawing", new Vector2(100, 330), Color.White);
  105. spriteBatch.DrawString(WPFFont, string1, new Vector2(100, 360), Color.White);
  106. spriteBatch.DrawString(WPFFont, string2, new Vector2(100, 390), Color.White);
  107. spriteBatch.DrawString(WPFFont, string3, new Vector2(100, 420), Color.White);
  108. spriteBatch.End();
  109. base.Draw(gameTime);
  110. }
  111. #endregion
  112. #region Handle Input
  113. /// <summary>
  114. /// Handles input for quitting the game.
  115. /// </summary>
  116. private void HandleInput()
  117. {
  118. KeyboardState currentKeyboardState = Keyboard.GetState();
  119. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  120. // Check for exit.
  121. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  122. currentGamePadState.IsButtonDown(Buttons.Back))
  123. {
  124. Exit();
  125. }
  126. }
  127. #endregion
  128. }
  129. }